Skip to content

Commit 434e13f

Browse files
authored
Modifications for DEV-324 (#104)
* Modifications for DEV-324 * Updated unit tests * Updated unit tests * Updated unit tests * Fixed failing unit test for Python 3 compatibility. * Made adjustment to failing test. * Made adjustment to failing test. * Made adjustment to failing test.
1 parent 1602ffc commit 434e13f

File tree

3 files changed

+204
-117
lines changed

3 files changed

+204
-117
lines changed

Algorithmia/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
usage = """CLI for interaction with Algorithmia\n
1818
Usage:\n
1919
algo [<cmd>] [options] [<args>...]\n
20-
algo[<cmd>] [--help | --version]\n\n
20+
algo [<cmd>] [--help | --version]\n\n
2121
2222
General commands include:\n
2323
auth configure authentication\n\n

Algorithmia/client.py

Lines changed: 92 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616

1717
class Client(object):
1818
'Algorithmia Common Library'
19-
2019

21-
handle, ca_cert = None,None
20+
handle, ca_cert = None, None
2221
apiKey = None
2322
apiAddress = None
2423
requestSession = None
2524

26-
27-
def __init__(self, apiKey = None, apiAddress = None, caCert = None):
25+
def __init__(self, apiKey=None, apiAddress=None, caCert=None):
2826
# Override apiKey with environment variable
2927
config = None
3028
self.requestSession = requests.Session()
@@ -46,16 +44,17 @@ def __init__(self, apiKey = None, apiAddress = None, caCert = None):
4644
self.catCerts(caCert)
4745
self.requestSession.verify = self.ca_cert
4846
elif caCert is not None and 'REQUESTS_CA_BUNDLE' in os.environ:
49-
#if both are available, use the one supplied in the constructor. I assume that a user supplying a cert in initialization wants to use that one.
47+
# if both are available, use the one supplied in the constructor. I assume that a user supplying a cert in initialization wants to use that one.
5048
self.catCerts(caCert)
5149
self.requestSession.verify = self.ca_cert
5250

5351
if not config:
5452
config = Configuration()
53+
5554
config.api_key['Authorization'] = self.apiKey
5655
config.host = "{}/v1".format(self.apiAddress)
5756
self.manageApi = DefaultApi(ApiClient(config))
58-
57+
5958
def algo(self, algoRef):
6059
return Algorithm(self, algoRef)
6160

@@ -76,57 +75,56 @@ def dir(self, dataUrl):
7675
return AdvancedDataDirectory(self, dataUrl)
7776

7877
def create_user(self, requestString):
79-
url = "/v1/users"
80-
response = self.postJsonHelper(url,input_object=requestString)
78+
url = "/v1/users"
79+
response = self.postJsonHelper(url, input_object=requestString)
8180
return response
8281

8382
def get_org_types(self):
8483
url = "/v1/organization/types"
8584
response = self.getHelper(url)
8685
return json.loads(response.content.decode("utf-8"))
8786

88-
def create_org(self,requestString):
87+
def create_org(self, requestString):
8988
url = "/v1/organizations"
9089
type = requestString["type_id"]
9190

92-
id,error = self.convert_type_id(type)
91+
id, error = self.convert_type_id(type)
9392
requestString["type_id"] = id
94-
95-
response = self.postJsonHelper(url=url,input_object=requestString)
93+
94+
response = self.postJsonHelper(url=url, input_object=requestString)
9695
if (error != "") and (response["error"] is not None):
9796
response["error"]["message"] = error
9897

9998
return response
100-
101-
def get_org(self,org_name):
102-
url = "/v1/organizations/"+org_name
99+
100+
def get_org(self, org_name):
101+
url = "/v1/organizations/" + org_name
103102
response = self.getHelper(url)
104103
return json.loads(response.content.decode("utf-8"))
105104

106-
def edit_org(self,org_name,requestString):
107-
url = "/v1/organizations/"+org_name
105+
def edit_org(self, org_name, requestString):
106+
url = "/v1/organizations/" + org_name
108107
type = requestString["type_id"]
109108

110-
id,error = self.convert_type_id(type)
109+
id, error = self.convert_type_id(type)
111110
requestString["type_id"] = id
112111

113112
data = json.dumps(requestString).encode('utf-8')
114-
response = self.putHelper(url,data)
113+
response = self.putHelper(url, data)
115114

116115
if (error != "") and (response["error"] is not None):
117116
response["error"]["message"] = error
118117

119118
return response
120119

121-
def invite_to_org(self,orgname,username):
122-
url = "/v1/organizations/"+orgname+"/members/"+username
123-
response = self.putHelper(url,data={})
120+
def invite_to_org(self, orgname, username):
121+
url = "/v1/organizations/" + orgname + "/members/" + username
122+
response = self.putHelper(url, data={})
124123
return response
125124

126-
127-
def get_template(self,envid,dest,save_tar=False):
128-
url = "/v1/algorithm-environments/edge/environment-specifications/"+envid+"/template"
129-
filename="template.tar.gz"
125+
def get_template(self, envid, dest, save_tar=False):
126+
url = "/v1/algorithm-environments/edge/environment-specifications/" + envid + "/template"
127+
filename = "template.tar.gz"
130128

131129
if not os.path.exists(dest):
132130
os.makedirs(dest)
@@ -152,26 +150,68 @@ def get_template(self,envid,dest,save_tar=False):
152150
except OSError as e:
153151
print(e)
154152
return response
155-
else:
153+
else:
156154
return json.loads(response.content.decode("utf-8"))
157155

158-
def get_environment(self,language):
159-
url = "/v1/algorithm-environments/edge/languages/"+language+"/environments"
156+
def get_environment(self, language):
157+
url = "/v1/algorithm-environments/edge/languages/" + language + "/environments"
160158
response = self.getHelper(url)
161159
return response.json()
162160

163161
def get_supported_languages(self):
164-
url ="/v1/algorithm-environments/edge/languages"
162+
url = "/v1/algorithm-environments/edge/languages"
165163
response = self.getHelper(url)
166164
return response.json()
167165

166+
def get_organization_errors(self, org_name):
167+
"""Gets the errors for the organization.
168+
169+
Args:
170+
self (Client): The instance of the Client class.
171+
org_name (str): The identifier for the organization.
172+
173+
Returns:
174+
Any: A JSON-encoded response from the API.
175+
"""
176+
177+
url = '/v1/organizations/%s/errors' % org_name
178+
response = self.getHelper(url)
179+
return response.json()
180+
181+
def get_user_errors(self, user_id):
182+
"""Gets the errors for a specific user.
183+
184+
Args:
185+
self (Client): The instance of the Client class.
186+
user_id (str): The identifier for the user.
168187
188+
Returns:
189+
Any: A JSON-encoded response from the API.
190+
"""
191+
192+
url = '/v1/users/%s/errors' % user_id
193+
response = self.getHelper(url)
194+
return response.json()
195+
196+
def get_algorithm_errors(self, algorithm_id):
197+
"""Gets the errors for a specific algorithm.
198+
199+
Args:
200+
self (Client): The instance of the Client class.
201+
algorithm_id (str): The identifier for the algorithm.
202+
203+
Returns:
204+
Any: A JSON-encoded response from the API.
205+
"""
206+
207+
url = '/v1/algorithms/%s/errors' % algorithm_id
208+
response = self.getHelper(url)
209+
return response.json()
169210

170211
# Used to send insight data to Algorithm Queue Reader in cluster
171212
def report_insights(self, insights):
172213
return Insights(insights)
173214

174-
175215
# Used internally to post json to the api and parse json response
176216
def postJsonHelper(self, url, input_object, parse_response_as_json=True, **query_parameters):
177217
headers = {}
@@ -192,7 +232,8 @@ def postJsonHelper(self, url, input_object, parse_response_as_json=True, **query
192232
input_json = json.dumps(input_object).encode('utf-8')
193233
headers['Content-Type'] = 'application/json'
194234

195-
response = self.requestSession.post(self.apiAddress + url, data=input_json, headers=headers, params=query_parameters)
235+
response = self.requestSession.post(self.apiAddress + url, data=input_json, headers=headers,
236+
params=query_parameters)
196237

197238
if parse_response_as_json and response.status_code == 200:
198239
return response.json()
@@ -224,7 +265,6 @@ def headHelper(self, url):
224265
headers['Authorization'] = self.apiKey
225266
return self.requestSession.head(self.apiAddress + url, headers=headers)
226267

227-
228268
# Used internally to http put a file
229269
def putHelper(self, url, data):
230270
headers = {}
@@ -249,44 +289,44 @@ def deleteHelper(self, url):
249289
return response.json()
250290

251291
# Used internally to concatonate given custom cert with built in certificate store.
252-
def catCerts(self,customCert):
253-
self.handle, self.ca_cert = mkstemp(suffix = ".pem")
254-
#wrapped all in the with context handler to prevent unclosed files
255-
with open(customCert,'r') as custom_cert, \
256-
open(self.ca_cert,'w') as ca,\
257-
open(certifi.where(),'r') as cert:
258-
new_cert = custom_cert.read() + cert.read()
259-
ca.write(new_cert)
292+
def catCerts(self, customCert):
293+
self.handle, self.ca_cert = mkstemp(suffix=".pem")
294+
# wrapped all in the with context handler to prevent unclosed files
295+
with open(customCert, 'r') as custom_cert, \
296+
open(self.ca_cert, 'w') as ca, \
297+
open(certifi.where(), 'r') as cert:
298+
new_cert = custom_cert.read() + cert.read()
299+
ca.write(new_cert)
260300
atexit.register(self.exit_handler)
261-
262-
#User internally to convert type id name to uuid
263-
def convert_type_id(self,type):
264-
id=""
265-
error=""
301+
302+
# User internally to convert type id name to uuid
303+
def convert_type_id(self, type):
304+
id = ""
305+
error = ""
266306
types = self.get_org_types()
267307
for enumtype in types:
268308
if type == enumtype["name"]:
269309
id = enumtype["id"]
270-
error=""
310+
error = ""
271311
break
272312
else:
273313
error = "invalid type_id"
274-
275-
return(id,error)
276314

315+
return (id, error)
277316

278317
# Used internally to clean up temporary files
279318
def exit_handler(self):
280319
try:
281320
os.close(self.handle)
282321
os.unlink(self.ca_cert)
283322
except OSError as e:
284-
print(e)
323+
print(e)
324+
285325

286326
def isJson(myjson):
287327
try:
288328
json_object = json.loads(myjson)
289-
except (ValueError,TypeError) as e:
329+
except (ValueError, TypeError) as e:
290330
return False
291331

292-
return True
332+
return True

0 commit comments

Comments
 (0)