1
1
'Algorithmia Algorithm API Client (python)'
2
2
3
- import base64
4
3
import json
5
4
import re
6
5
from Algorithmia .async_response import AsyncResponse
7
6
from Algorithmia .algo_response import AlgoResponse
8
- from Algorithmia .errors import ApiError , ApiInternalError , raiseAlgoApiError
7
+ from Algorithmia .errors import ApiError , ApiInternalError , raiseAlgoApiError , AlgorithmException
9
8
from enum import Enum
10
9
from algorithmia_api_client .rest import ApiException
11
10
from 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
40
39
return self
41
40
42
41
# 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
57
49
58
50
# 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
73
57
74
58
# 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 = {}):
77
60
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 )
81
64
return api_response
82
- # except ApiException as e:
83
- # error_message = json.loads(e.body)
84
- # raise raiseAlgoApiError(error_message)
85
65
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
97
71
98
72
def get_build (self , build_id ):
99
73
# Get the build object for a given build_id
100
74
# 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
107
78
108
79
def get_build_logs (self , build_id ):
109
80
# 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 )
120
83
return response
121
84
122
85
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
129
89
130
90
# Get info on an algorithm
131
91
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 ):
132
102
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
142
109
143
110
# Get all versions of the algorithm, with the given filters
144
111
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):
154
121
if callable :
155
122
c = callable
156
123
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
164
128
165
129
# Compile an algorithm
166
130
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
174
135
175
136
# Pipe an input into this algorithm
176
137
def pipe (self , input1 ):
0 commit comments