11'Algorithmia Algorithm API Client (python)'
22
3- import base64
43import json
54import re
65from Algorithmia .async_response import AsyncResponse
76from Algorithmia .algo_response import AlgoResponse
8- from Algorithmia .errors import ApiError , ApiInternalError , raiseAlgoApiError
7+ from Algorithmia .errors import ApiError , ApiInternalError , raiseAlgoApiError , AlgorithmException
98from enum import Enum
109from algorithmia_api_client .rest import ApiException
1110from algorithmia_api_client import CreateRequest , UpdateRequest , VersionRequest , Details , Settings , SettingsMandatory , \
@@ -40,105 +39,73 @@ def set_options(self, timeout=300, stdout=False, output=OutputType.default, **qu
4039 return self
4140
4241 # Create a new algorithm
43- def create (self , details = {}, settings = {}, version_info = {}):
44- detailsObj = Details (** details )
45- settingsObj = SettingsMandatory (** settings )
46- createRequestVersionInfoObj = CreateRequestVersionInfo (** version_info )
47- create_parameters = {"name" : self .algoname , "details" : detailsObj , "settings" : settingsObj ,
48- "version_info" : createRequestVersionInfoObj }
49- create_request = CreateRequest (** create_parameters )
50- try :
51- # Create Algorithm
52- api_response = self .client .manageApi .create_algorithm (self .username , create_request )
53- return api_response
54- except ApiException as e :
55- error_message = json .loads (e .body )
56- raise raiseAlgoApiError (error_message )
42+ def create (self , details = {}, settings = {}, version_info = {}, source = {}, scmsCredentials = {}):
43+ url = "/v1/algorithms/" + self .username
44+ create_parameters = {"name" : self .algoname , "details" : details , "settings" : settings ,
45+ "version_info" : version_info , "source" : source , "scmsCredentials" : scmsCredentials }
46+
47+ api_response = self .client .postJsonHelper (url , create_parameters , parse_response_as_json = True )
48+ return api_response
5749
5850 # Update the settings in an algorithm
59- def update (self , details = {}, settings = {}, version_info = {}):
60- detailsObj = Details (** details )
61- settingsObj = Settings (** settings )
62- createRequestVersionInfoObj = CreateRequestVersionInfo (** version_info )
63- update_parameters = {"details" : detailsObj , "settings" : settingsObj ,
64- "version_info" : createRequestVersionInfoObj }
65- update_request = UpdateRequest (** update_parameters )
66- try :
67- # Update Algorithm
68- api_response = self .client .manageApi .update_algorithm (self .username , self .algoname , update_request )
69- return api_response
70- except ApiException as e :
71- error_message = json .loads (e .body )
72- raise raiseAlgoApiError (error_message )
51+ def update (self , details = {}, settings = {}, version_info = {}, source = {}, scmsCredentials = {}):
52+ url = "/v1/algorithms/" + self .username + "/" + self .algoname
53+ update_parameters = {"details" : details , "settings" : settings ,
54+ "version_info" : version_info , "source" : source , "scmsCredentials" : scmsCredentials }
55+ api_response = self .client .putHelper (url , update_parameters )
56+ return api_response
7357
7458 # Publish an algorithm
75- def publish (self , details = {}, settings = {}, version_info = {}):
76- publish_parameters = {"details" : details , "settings" : settings , "version_info" : version_info }
59+ def publish (self , details = {}, settings = {}, version_info = {}, source = {}, scmsCredentials = {}):
7760 url = "/v1/algorithms/" + self .username + "/" + self .algoname + "/versions"
78- print ( publish_parameters )
79- api_response = self . client . postJsonHelper ( url , publish_parameters , parse_response_as_json = True ,
80- ** self .query_parameters )
61+ publish_parameters = { "details" : details , "settings" : settings ,
62+ "version_info" : version_info , "source" : source , "scmsCredentials" : scmsCredentials }
63+ api_response = self .client . postJsonHelper ( url , publish_parameters , parse_response_as_json = True )
8164 return api_response
82- # except ApiException as e:
83- # error_message = json.loads(e.body)
84- # raise raiseAlgoApiError(error_message)
8565
86- def builds (self , limit = 56 , marker = None ):
87- try :
88- if marker is not None :
89- api_response = self .client .manageApi .get_algorithm_builds (self .username , self .algoname , limit = limit ,
90- marker = marker )
91- else :
92- api_response = self .client .manageApi .get_algorithm_builds (self .username , self .algoname , limit = limit )
93- return api_response
94- except ApiException as e :
95- error_message = json .loads (e .body )
96- raise raiseAlgoApiError (error_message )
66+ def get_builds (self , limit = 56 , marker = None ):
67+ kwargs = {"limit" : limit , "marker" : marker }
68+ url = "/v1/algorithms/" + self .username + "/" + self .algoname + '/builds'
69+ response = self .client .getJsonHelper (url , ** kwargs )
70+ return response
9771
9872 def get_build (self , build_id ):
9973 # Get the build object for a given build_id
10074 # The build status can have one of the following value: succeeded, failed, in-progress
101- try :
102- api_response = self .client .manageApi .get_algorithm_build_by_id (self .username , self .algoname , build_id )
103- return api_response
104- except ApiException as e :
105- error_message = json .loads (e .body )
106- raise raiseAlgoApiError (error_message )
75+ url = '/v1/algorithms/' + self .username + '/' + self .algoname + '/builds/' + build_id
76+ response = self .client .getJsonHelper (url )
77+ return response
10778
10879 def get_build_logs (self , build_id ):
10980 # Get the algorithm build logs for a given build_id
110- try :
111- api_response = self .client .manageApi .get_algorithm_build_logs (self .username , self .algoname , build_id )
112- return api_response
113- except ApiException as e :
114- error_message = json .loads (e .body )
115- raise raiseAlgoApiError (error_message )
116-
117- def build_logs (self ):
118- url = '/v1/algorithms/' + self .username + '/' + self .algoname + '/builds'
119- response = json .loads (self .client .getHelper (url ).content .decode ('utf-8' ))
81+ url = '/v1/algorithms/' + self .username + '/' + self .algoname + '/builds/' + build_id + '/logs'
82+ response = self .client .getJsonHelper (url )
12083 return response
12184
12285 def get_scm_status (self ):
123- try :
124- api_response = self .client .manageApi .get_algorithm_scm_connection_status (self .username , self .algoname )
125- return api_response
126- except ApiException as e :
127- error_message = json .loads (e .body )
128- raise raiseAlgoApiError (error_message )
86+ url = '/v1/algorithms/' + self .username + '/' + self .algoname + '/scm/status'
87+ response = self .client .getJsonHelper (url )
88+ return response
12989
13090 # Get info on an algorithm
13191 def info (self , algo_hash = None ):
92+ # Get Algorithm
93+ if algo_hash :
94+ url = '/v1/algorithms/' + self .username + '/' + self .algoname + '/versions/' + algo_hash
95+ else :
96+ url = '/v1/algorithms/' + self .username + '/' + self .algoname + '/versions'
97+ response = self .client .getJsonHelper (url )
98+ return response
99+
100+ # Check if an Algorithm exists
101+ def exists (self ):
132102 try :
133- # Get Algorithm
134- if algo_hash :
135- api_response = self .client .manageApi .get_algorithm_hash_version (self .username , self .algoname , algo_hash )
136- else :
137- api_response = self .client .manageApi .get_algorithm (self .username , self .algoname )
138- return api_response
139- except ApiException as e :
140- error_message = json .loads (e .body )
141- raise raiseAlgoApiError (error_message )
103+ url = '/v1/algorithms/' + self .username + '/' + self .algoname
104+ _ = self .client .getJsonHelper (url )
105+ return True
106+ except AlgorithmException as e :
107+ print (e )
108+ return False
142109
143110 # Get all versions of the algorithm, with the given filters
144111 def versions (self , limit = None , marker = None , published = None , callable = None ):
@@ -154,23 +121,17 @@ def versions(self, limit=None, marker=None, published=None, callable=None):
154121 if callable :
155122 c = callable
156123 kwargs ["callable" ] = str (c ).lower () if str (c ) in bools else c
157- try :
158- # Get Algorithm versions
159- api_response = self .client .manageApi .get_algorithm_versions (self .username , self .algoname , ** kwargs )
160- return api_response
161- except ApiException as e :
162- error_message = json .loads (e .body )
163- raise raiseAlgoApiError (error_message )
124+ # Get Algorithm versions
125+ url = '/v1/algorithms/' + self .username + '/' + self .algoname + '/versions'
126+ response = self .client .getJsonHelper (url )
127+ return response
164128
165129 # Compile an algorithm
166130 def compile (self ):
167- try :
168- # Compile algorithm
169- api_response = self .client .manageApi .compile_algorithm (self .username , self .algoname )
170- return api_response
171- except ApiException as e :
172- error_message = json .loads (e .body )
173- raise raiseAlgoApiError (error_message )
131+ # Compile algorithm
132+ url = '/v1/algorithms/' + self .username + '/' + self .algoname + '/compile'
133+ response = self .client .postJsonHelper (url , {}, parse_response_as_json = True )
134+ return response
174135
175136 # Pipe an input into this algorithm
176137 def pipe (self , input1 ):
0 commit comments