Last active
March 20, 2019 14:28
-
-
Save micahwalter/e4cb990a1691d33476e5f3995dbd3855 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### code to make rest calls to the Tessitura API | |
import requests | |
import json | |
import base64 | |
import os | |
## Tessitura API | |
hostName = os.environ['TESSITURA_API'] | |
## The full credential string like "user:group:location:password" | |
credentials = os.environ['REST_CREDENTIALS'] | |
def api_call(method, *args, **kwargs): | |
params = kwargs.get('params', None) | |
data = kwargs.get('data', None) | |
## call the rest API | |
rsp = api_rest_call(method, args[0], params=params, data=data) | |
return rsp | |
def api_rest_call(method, *args, **kwargs): | |
params = kwargs.get('params', None) | |
data = kwargs.get('data', None) | |
rest_endpoint = 'TessituraService/' | |
url = hostName + rest_endpoint + method | |
r = {} | |
headers = { | |
'Accept': "application/json", | |
'Content-Type': "application/json", | |
'Authorization': api_prepare_auth_string() | |
} | |
if args[0] == "GET": | |
r = requests.get(url, params=params, headers=headers) | |
elif args[0] == "POST": | |
r = requests.request("POST", url, data=str(data), headers=headers) | |
## return parsed response | |
j = r.json() | |
return j | |
def api_prepare_auth_string(): | |
encoded = base64.b64encode(str.encode(credentials)) | |
encoded = str(encoded.decode()) | |
authHeader = 'Basic ' + encoded | |
return authHeader | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment