Skip to content

[ALERT] Fix for breaking changes introduced by Bearer Token work #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 22, 2021
43 changes: 22 additions & 21 deletions Algorithmia/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@ class Client(object):
requestSession = None
bearerToken = None


def __init__(self, apiKey = None, apiAddress = None, caCert = None, bearerToken=None):
def __init__(self, apiKey=None, apiAddress=None, caCert=None, bearerToken=None):
# Override apiKey with environment variable
config = None
self.requestSession = requests.Session()
if apiKey is None and 'ALGORITHMIA_API_KEY' in os.environ:
apiKey = os.environ['ALGORITHMIA_API_KEY']
if apiKey is None:
if bearerToken is None and 'ALGORITHMIA_BEARER_TOKEN' in os.environ:
bearerToken = os.environ['ALGORITHMIA_BEARER_TOKEN']
self.bearerToken = bearerToken
elif bearerToken is None and 'ALGORITHMIA_BEARER_TOKEN' in os.environ:
bearerToken = os.environ['ALGORITHMIA_BEARER_TOKEN']

self.bearerToken = bearerToken
self.apiKey = apiKey
if apiAddress is not None:
self.apiAddress = apiAddress
Expand Down Expand Up @@ -225,8 +223,8 @@ def postJsonHelper(self, url, input_object, parse_response_as_json=True, **query
headers = {}
if self.apiKey is not None:
headers['Authorization'] = self.apiKey
else:
headers['Authorization'] = "Bearer "+ self.bearerToken
elif self.bearerToken is not None:
headers['Authorization'] = 'Bearer ' + self.bearerToken

input_json = None
if input_object is None:
Expand Down Expand Up @@ -254,42 +252,44 @@ def getHelper(self, url, **query_parameters):
headers = {}
if self.apiKey is not None:
headers['Authorization'] = self.apiKey
else:
headers['Authorization'] = 'Bearer '+ self.bearerToken
elif self.bearerToken is not None:
headers['Authorization'] = 'Bearer ' + self.bearerToken
else:
raise Exception("No authentication provided")
return self.requestSession.get(self.apiAddress + url, headers=headers, params=query_parameters)

def getStreamHelper(self, url, **query_parameters):
headers = {}
if self.apiKey is not None:
headers['Authorization'] = self.apiKey
else:
headers['Authorization'] = 'Bearer '+ self.bearerToken
elif self.bearerToken is not None:
headers['Authorization'] = 'Bearer ' + self.bearerToken
return self.requestSession.get(self.apiAddress + url, headers=headers, params=query_parameters, stream=True)

def patchHelper(self, url, params):
headers = {'content-type': 'application/json'}
if self.apiKey is not None:
headers['Authorization'] = self.apiKey
else:
headers['Authorization'] = 'Bearer '+ self.bearerToken
elif self.bearerToken is not None:
headers['Authorization'] = 'Bearer ' + self.bearerToken
return self.requestSession.patch(self.apiAddress + url, headers=headers, data=json.dumps(params))

# Used internally to get http head result
def headHelper(self, url):
headers = {}
if self.apiKey is not None:
headers['Authorization'] = self.apiKey
else:
headers['Authorization'] = 'Bearer '+ self.bearerToken
elif self.bearerToken is not None:
headers['Authorization'] = 'Bearer ' + self.bearerToken
return self.requestSession.head(self.apiAddress + url, headers=headers)

# Used internally to http put a file
def putHelper(self, url, data):
headers = {}
if self.apiKey is not None:
headers['Authorization'] = self.apiKey
else:
headers['Authorization'] = 'Bearer '+ self.bearerToken
elif self.bearerToken is not None:
headers['Authorization'] = 'Bearer ' + self.bearerToken
if isJson(data):
headers['Content-Type'] = 'application/json'

Expand All @@ -303,8 +303,8 @@ def deleteHelper(self, url):
headers = {}
if self.apiKey is not None:
headers['Authorization'] = self.apiKey
else:
headers['Authorization'] = 'Bearer '+ self.bearerToken
elif self.bearerToken is not None:
headers['Authorization'] = 'Bearer ' + self.bearerToken
response = self.requestSession.delete(self.apiAddress + url, headers=headers)
if response.reason == "No Content":
return response
Expand Down Expand Up @@ -364,11 +364,12 @@ def freeze(self, manifest_path, manifest_output_dir="."):
required_files[i]['md5_checksum'] = md5_checksum
lock_md5_checksum = md5_for_str(str(manifest_file))
manifest_file['lock_checksum'] = lock_md5_checksum
with open(manifest_output_dir+'/'+'model_manifest.json.freeze', 'w') as f:
with open(manifest_output_dir + '/' + 'model_manifest.json.freeze', 'w') as f:
json.dump(manifest_file, f)
else:
print("Expected to find a model_manifest.json file, none was discovered in working directory")


def isJson(myjson):
try:
json_object = json.loads(myjson)
Expand Down