Fix a couple of issues I noticed
authorIan Cordasco <graffatcolmingov@gmail.com>
Tue, 30 Sep 2014 21:03:31 +0000 (16:03 -0500)
committerIan Cordasco <graffatcolmingov@gmail.com>
Tue, 30 Sep 2014 21:03:31 +0000 (16:03 -0500)
- Don't _ prefix json in prepare_body
- Don't initialize json to []
- Don't initialize json to {}
- Reorder parameters to PreparedRequest.prepare
- Remove extra parentheses
- Update docstring

requests/models.py
requests/sessions.py

index a96fc4e68b87c0e4b855ae8190cd7f26dc783c7d..c1f7f561e8710ace24e2c1ce9e4bb9b93ccb106e 100644 (file)
@@ -46,6 +46,8 @@ DEFAULT_REDIRECT_LIMIT = 30
 CONTENT_CHUNK_SIZE = 10 * 1024
 ITER_CHUNK_SIZE = 512
 
+json_dumps = json.dumps
+
 
 class RequestEncodingMixin(object):
     @property
@@ -189,8 +191,8 @@ class Request(RequestHooksMixin):
     :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.
@@ -218,7 +220,6 @@ class Request(RequestHooksMixin):
 
         # 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
@@ -294,7 +295,8 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
         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)
@@ -402,7 +404,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
         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.
@@ -413,9 +415,9 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
         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__'),
@@ -442,7 +444,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
             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
@@ -452,7 +454,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
             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
index 7942447fd5a1ac8b27db43b1de1c6ba65ef96315..c5ad0060ecb8d64974cdd0792c1fe2d98ee1943d 100644 (file)
@@ -430,7 +430,7 @@ class Session(SessionRedirectMixin):
             headers = headers,
             files = files,
             data = data or {},
-            json = json or {},
+            json = json,
             params = params or {},
             auth = auth,
             cookies = cookies,