1
1
import requests
2
2
import platform
3
+ import urllib .parse
3
4
4
5
from .tasks import Task
5
6
from .batches import Batch
@@ -63,11 +64,11 @@ class Batchlist(Paginator):
63
64
64
65
65
66
class ScaleClient (object ):
66
- def __init__ (self , api_key ):
67
+ def __init__ (self , api_key , user_agent_extension = None ):
67
68
self .api_key = api_key
68
69
self ._headers = {
69
70
"Content-Type" : "application/json" ,
70
- "User-Agent" : _generate_useragent ()
71
+ "User-Agent" : _generate_useragent (user_agent_extension )
71
72
}
72
73
73
74
def _getrequest (self , endpoint , params = None ):
@@ -171,15 +172,15 @@ def create_batch(self, project, batch_name, callback):
171
172
return Batch (batchdata , self )
172
173
173
174
def finalize_batch (self , batch_name ):
174
- batchdata = self ._postrequest ('batches/%s/finalize' % batch_name )
175
+ batchdata = self ._postrequest ('batches/%s/finalize' % quote_string ( batch_name ) )
175
176
return Batch (batchdata , self )
176
177
177
178
def batch_status (self , batch_name ):
178
- batchdata = self ._getrequest ('batches/%s/status' % batch_name )
179
- return Batch . get_status ( self )
179
+ status_data = self ._getrequest ('batches/%s/status' % quote_string ( batch_name ) )
180
+ return status_data
180
181
181
182
def get_batch (self , batch_name ):
182
- batchdata = self ._getrequest ('batches/%s' % batch_name )
183
+ batchdata = self ._getrequest ('batches/%s' % quote_string ( batch_name ) )
183
184
return Batch (batchdata , self )
184
185
185
186
def list_batches (self , ** kwargs ):
@@ -202,7 +203,7 @@ def create_project(self, project_name, type, params):
202
203
return Project (projectdata , self )
203
204
204
205
def get_project (self , project_name ):
205
- projectdata = self ._getrequest ('projects/%s' % project_name )
206
+ projectdata = self ._getrequest ('projects/%s' % quote_string ( project_name ) )
206
207
return Project (projectdata , self )
207
208
208
209
def projects (self ):
@@ -215,19 +216,38 @@ def update_project(self, project_name, **kwargs):
215
216
if key not in allowed_kwargs :
216
217
raise ScaleInvalidRequest ('Illegal parameter %s for ScaleClient.update_project()'
217
218
% key , None )
218
- projectdata = self ._postrequest ('projects/%s/setParams' % project_name , payload = kwargs )
219
+ projectdata = self ._postrequest ('projects/%s/setParams' % quote_string ( project_name ) , payload = kwargs )
219
220
return projectdata
220
221
221
- def _generate_useragent ():
222
+ def _generate_useragent (extension = None ):
222
223
try :
223
224
python_version = platform .python_version ()
224
225
os_platform = platform .platform ()
225
226
226
- user_agent = '%s/%s Python/%s OS/%s' % (__name__ , __version__ , python_version , os_platform )
227
+ user_agent = " " .join (
228
+ filter (
229
+ None ,
230
+ [
231
+ "{}/{}" .format (__name__ , __version__ ),
232
+ "Python/{}" .format (python_version ),
233
+ "OS/{}" .format (os_platform ),
234
+ extension ,
235
+ ],
236
+ )
237
+ )
227
238
return user_agent
228
- except :
239
+
240
+ except Exception :
229
241
return "scaleapi-python-client"
230
242
243
+ def quote_string (text ):
244
+ """`quote_string('a bc/def')` -> `a%20bc%2Fdef`
245
+ Project and Batch names can be a part of URL, which causes an error
246
+ in case of a special character used. Quotation assures
247
+ the right object to be retrieved from API.
248
+ """
249
+ return urllib .parse .quote (text , safe = "" )
250
+
231
251
def _AddTaskTypeCreator (task_type ):
232
252
def create_task_wrapper (self , ** kwargs ):
233
253
return self .create_task (task_type , ** kwargs )
0 commit comments