20
20
21
21
from __future__ import absolute_import
22
22
23
- from . import models
24
- from . import ws_client
25
- from .rest import RESTClientObject
26
- from .rest import ApiException
27
-
28
- import os
29
- import re
30
23
import json
31
24
import mimetypes
25
+ import os
26
+ import re
32
27
import tempfile
33
28
import threading
34
-
35
- from datetime import datetime
36
- from datetime import date
29
+ from datetime import date , datetime
37
30
38
31
# python 2 and python 3 compatibility library
39
32
from six import PY3 , integer_types , iteritems , text_type
40
33
from six .moves .urllib .parse import quote
41
34
35
+ from . import models , ws_client
42
36
from .configuration import configuration
37
+ from .rest import ApiException , RESTClientObject
43
38
44
39
45
40
class ApiClient (object ):
@@ -59,9 +54,9 @@ class ApiClient(object):
59
54
:param header_name: a header to pass when making calls to the API.
60
55
:param header_value: a header value to pass when making calls to the API.
61
56
"""
57
+
62
58
def __init__ (self , host = None , header_name = None , header_value = None ,
63
59
cookie = None , config = configuration ):
64
-
65
60
"""
66
61
Constructor of the class.
67
62
"""
@@ -99,8 +94,8 @@ def __call_api(self, resource_path, method,
99
94
path_params = None , query_params = None , header_params = None ,
100
95
body = None , post_params = None , files = None ,
101
96
response_type = None , auth_settings = None , callback = None ,
102
- _return_http_data_only = None , collection_formats = None , _preload_content = True ,
103
- _request_timeout = None ):
97
+ _return_http_data_only = None , collection_formats = None ,
98
+ _preload_content = True , _request_timeout = None ):
104
99
105
100
# header parameters
106
101
header_params = header_params or {}
@@ -163,11 +158,16 @@ def __call_api(self, resource_path, method,
163
158
return_data = None
164
159
165
160
if callback :
166
- callback (return_data ) if _return_http_data_only else callback ((return_data , response_data .status , response_data .getheaders ()))
161
+ if _return_http_data_only :
162
+ callback (return_data )
163
+ else :
164
+ callback ((return_data ,
165
+ response_data .status , response_data .getheaders ()))
167
166
elif _return_http_data_only :
168
167
return (return_data )
169
168
else :
170
- return (return_data , response_data .status , response_data .getheaders ())
169
+ return (return_data , response_data .status ,
170
+ response_data .getheaders ())
171
171
172
172
def sanitize_for_serialization (self , obj ):
173
173
"""
@@ -194,7 +194,7 @@ def sanitize_for_serialization(self, obj):
194
194
for sub_obj in obj ]
195
195
elif isinstance (obj , tuple ):
196
196
return tuple (self .sanitize_for_serialization (sub_obj )
197
- for sub_obj in obj )
197
+ for sub_obj in obj )
198
198
elif isinstance (obj , (datetime , date )):
199
199
return obj .isoformat ()
200
200
else :
@@ -248,7 +248,7 @@ def __deserialize(self, data, klass):
248
248
if data is None :
249
249
return None
250
250
251
- if type (klass ) == str :
251
+ if isinstance (klass , str ) :
252
252
if klass .startswith ('list[' ):
253
253
sub_kls = re .match ('list\[(.*)\]' , klass ).group (1 )
254
254
return [self .__deserialize (sub_data , sub_kls )
@@ -285,8 +285,8 @@ def call_api(self, resource_path, method,
285
285
path_params = None , query_params = None , header_params = None ,
286
286
body = None , post_params = None , files = None ,
287
287
response_type = None , auth_settings = None , callback = None ,
288
- _return_http_data_only = None , collection_formats = None , _preload_content = True ,
289
- _request_timeout = None ):
288
+ _return_http_data_only = None , collection_formats = None ,
289
+ _preload_content = True , _request_timeout = None ):
290
290
"""
291
291
Makes the HTTP request (synchronous) and return the deserialized data.
292
292
To make an async request, define a function for callback.
@@ -307,13 +307,18 @@ def call_api(self, resource_path, method,
307
307
:param callback function: Callback function for asynchronous request.
308
308
If provide this parameter,
309
309
the request will be called asynchronously.
310
- :param _return_http_data_only: response data without head status code and headers
310
+ :param _return_http_data_only: response data without head status code
311
+ and headers
311
312
:param collection_formats: dict of collection formats for path, query,
312
313
header, and post parameters.
313
- :param _preload_content: if False, the urllib3.HTTPResponse object will be returned without
314
- reading/decoding response data. Default is True.
315
- :param _request_timeout: timeout setting for this request. If one number provided, it will be total request
316
- timeout. It can also be a pair (tuple) of (connection, read) timeouts.
314
+ :param _preload_content: if False, the urllib3.HTTPResponse object will
315
+ be returned without
316
+ reading/decoding response data.
317
+ Default is True.
318
+ :param _request_timeout: timeout setting for this request. If one
319
+ number provided, it will be total request
320
+ timeout. It can also be a pair (tuple) of
321
+ (connection, read) timeouts.
317
322
:return:
318
323
If provide parameter callback,
319
324
the request will be called asynchronously.
@@ -326,7 +331,8 @@ def call_api(self, resource_path, method,
326
331
path_params , query_params , header_params ,
327
332
body , post_params , files ,
328
333
response_type , auth_settings , callback ,
329
- _return_http_data_only , collection_formats , _preload_content , _request_timeout )
334
+ _return_http_data_only , collection_formats ,
335
+ _preload_content , _request_timeout )
330
336
else :
331
337
thread = threading .Thread (target = self .__call_api ,
332
338
args = (resource_path , method ,
@@ -335,18 +341,22 @@ def call_api(self, resource_path, method,
335
341
post_params , files ,
336
342
response_type , auth_settings ,
337
343
callback , _return_http_data_only ,
338
- collection_formats , _preload_content , _request_timeout ))
344
+ collection_formats ,
345
+ _preload_content ,
346
+ _request_timeout ))
339
347
thread .start ()
340
348
return thread
341
349
342
350
def request (self , method , url , query_params = None , headers = None ,
343
- post_params = None , body = None , _preload_content = True , _request_timeout = None ):
351
+ post_params = None , body = None , _preload_content = True ,
352
+ _request_timeout = None ):
344
353
"""
345
354
Makes the HTTP request using RESTClient.
346
355
"""
347
356
# FIXME(dims) : We need a better way to figure out which
348
357
# calls end up using web sockets
349
- if (url .endswith ('/exec' ) or url .endswith ('/attach' )) and (method == "GET" or method == "POST" ):
358
+ if (url .endswith ('/exec' ) or url .endswith ('/attach' )) and \
359
+ (method == "GET" or method == "POST" ):
350
360
return ws_client .websocket_call (self .config ,
351
361
url ,
352
362
query_params = query_params ,
@@ -458,14 +468,15 @@ def prepare_post_parameters(self, post_params=None, files=None):
458
468
for k , v in iteritems (files ):
459
469
if not v :
460
470
continue
461
- file_names = v if type ( v ) is list else [v ]
471
+ file_names = v if isinstance ( v , list ) else [v ]
462
472
for n in file_names :
463
473
with open (n , 'rb' ) as f :
464
474
filename = os .path .basename (f .name )
465
475
filedata = f .read ()
466
- mimetype = mimetypes .\
467
- guess_type (filename )[0 ] or 'application/octet-stream'
468
- params .append (tuple ([k , tuple ([filename , filedata , mimetype ])]))
476
+ mimetype = (mimetypes .guess_type (filename )[0 ] or
477
+ 'application/octet-stream' )
478
+ params .append (tuple ([k , tuple ([filename , filedata ,
479
+ mimetype ])]))
469
480
470
481
return params
471
482
@@ -543,9 +554,8 @@ def __deserialize_file(self, response):
543
554
544
555
content_disposition = response .getheader ("Content-Disposition" )
545
556
if content_disposition :
546
- filename = re .\
547
- search (r'filename=[\'"]?([^\'"\s]+)[\'"]?' , content_disposition ).\
548
- group (1 )
557
+ filename = re .search (r'filename=[\'"]?([^\'"\s]+)[\'"]?' ,
558
+ content_disposition ).group (1 )
549
559
path = os .path .join (os .path .dirname (path ), filename )
550
560
551
561
with open (path , "w" ) as f :
0 commit comments