CONTENT_CHUNK_SIZE = 10 * 1024
ITER_CHUNK_SIZE = 512
+json_dumps = json.dumps
+
class RequestEncodingMixin(object):
@property
:param url: URL to send.
:param headers: dictionary of headers to send.
:param files: dictionary of {filename: fileobject} files to multipart upload.
- :param data: the body to attach the request. If a dictionary is provided, form-encoding will take place.
- :param json: json for the body to attach the request.
+ :param data: the body to attach to the request. If a dictionary is provided, form-encoding will take place.
+ :param json: json for the body to attach to the request (if data is not specified).
:param params: dictionary of URL parameters to append to the URL.
:param auth: Auth handler or (user, pass) tuple.
:param cookies: dictionary or CookieJar of cookies to attach to this request.
# Default empty dicts for dict params.
data = [] if data is None else data
- json = [] if json is None else json
files = [] if files is None else files
headers = {} if headers is None else headers
params = {} if params is None else params
self.hooks = default_hooks()
def prepare(self, method=None, url=None, headers=None, files=None,
- data=None, json=None, params=None, auth=None, cookies=None, hooks=None):
+ data=None, params=None, auth=None, cookies=None, hooks=None,
+ json=None):
"""Prepares the entire request with the given parameters."""
self.prepare_method(method)
else:
self.headers = CaseInsensitiveDict()
- def prepare_body(self, data, files, _json=None):
+ def prepare_body(self, data, files, json=None):
"""Prepares the given HTTP body data."""
# Check if file, fo, generator, iterator.
content_type = None
length = None
- if _json is not None:
+ if json is not None:
content_type = 'application/json'
- data = json.dumps(_json)
+ body = json_dumps(json)
is_stream = all([
hasattr(data, '__iter__'),
if files:
(body, content_type) = self._encode_files(files, data)
else:
- if data and _json is None:
+ if data and json is None:
body = self._encode_params(data)
if isinstance(data, basestring) or hasattr(data, 'read'):
content_type = None
self.prepare_content_length(body)
# Add content-type if it wasn't explicitly provided.
- if (content_type) and (not 'content-type' in self.headers):
+ if content_type and ('content-type' not in self.headers):
self.headers['Content-Type'] = content_type
self.body = body