1
1
"""HTTP Client library"""
2
2
import json
3
+
3
4
from .exceptions import handle_error
4
5
5
6
try :
@@ -62,6 +63,9 @@ def to_dict(self):
62
63
class Client (object ):
63
64
"""Quickly and easily access any REST or REST-like API."""
64
65
66
+ # These are the supported HTTP verbs
67
+ methods = {'delete' , 'get' , 'patch' , 'post' , 'put' }
68
+
65
69
def __init__ (self ,
66
70
host ,
67
71
request_headers = None ,
@@ -88,8 +92,6 @@ def __init__(self,
88
92
self ._version = version
89
93
# _url_path keeps track of the dynamically built url
90
94
self ._url_path = url_path or []
91
- # These are the supported HTTP verbs
92
- self .methods = ['delete' , 'get' , 'patch' , 'post' , 'put' ]
93
95
# APPEND SLASH set
94
96
self .append_slash = append_slash
95
97
self .timeout = timeout
@@ -211,44 +213,51 @@ def get_version(*args, **kwargs):
211
213
if name in self .methods :
212
214
method = name .upper ()
213
215
214
- def http_request (* _ , ** kwargs ):
216
+ def http_request (
217
+ request_body = None ,
218
+ query_params = None ,
219
+ request_headers = None ,
220
+ timeout = None ,
221
+ ** _ ):
215
222
"""Make the API call
216
- :param args: unused
223
+ :param timeout: HTTP request timeout. Will be propagated to
224
+ urllib client
225
+ :type timeout: float
226
+ :param request_headers: HTTP headers. Will be merged into
227
+ current client object state
228
+ :type request_headers: dict
229
+ :param query_params: HTTP query parameters
230
+ :type query_params: dict
231
+ :param request_body: HTTP request body
232
+ :type request_body: string or json-serializable object
217
233
:param kwargs:
218
- :return: Client object
234
+ :return: Response object
219
235
"""
220
- if 'request_headers' in kwargs :
221
- self ._update_headers (kwargs ['request_headers' ])
222
- if 'request_body' not in kwargs :
236
+ if request_headers :
237
+ self ._update_headers (request_headers )
238
+
239
+ if request_body is None :
223
240
data = None
224
241
else :
225
242
# Don't serialize to a JSON formatted str
226
243
# if we don't have a JSON Content-Type
227
- if 'Content-Type' in self .request_headers :
228
- if self .request_headers ['Content-Type' ] != \
229
- 'application/json' :
230
- data = kwargs ['request_body' ].encode ('utf-8' )
231
- else :
232
- data = json .dumps (
233
- kwargs ['request_body' ]).encode ('utf-8' )
244
+ if 'Content-Type' in self .request_headers and \
245
+ self .request_headers ['Content-Type' ] != \
246
+ 'application/json' :
247
+ data = request_body .encode ('utf-8' )
234
248
else :
235
- data = json .dumps (
236
- kwargs ['request_body' ]).encode ('utf-8' )
237
-
238
- if 'query_params' in kwargs :
239
- params = kwargs ['query_params' ]
240
- else :
241
- params = None
249
+ self .request_headers .setdefault (
250
+ 'Content-Type' , 'application/json' )
251
+ data = json .dumps (request_body ).encode ('utf-8' )
242
252
243
253
opener = urllib .build_opener ()
244
- request = urllib .Request (self ._build_url (params ), data = data )
245
- if self .request_headers :
246
- for key , value in self .request_headers .items ():
247
- request .add_header (key , value )
248
- if data and ('Content-Type' not in self .request_headers ):
249
- request .add_header ('Content-Type' , 'application/json' )
254
+ request = urllib .Request (
255
+ self ._build_url (query_params ),
256
+ headers = self .request_headers ,
257
+ data = data ,
258
+ )
250
259
request .get_method = lambda : method
251
- timeout = kwargs . pop ( 'timeout' , None )
260
+
252
261
return Response (
253
262
self ._make_request (opener , request , timeout = timeout )
254
263
)
0 commit comments