Skip to content

Commit

Permalink
added json_serialize parameter for ClientSession #1726
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Mar 20, 2017
1 parent 19a9e32 commit cd6227b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
12 changes: 8 additions & 4 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import base64
import hashlib
import json
import os
import sys
import traceback
Expand All @@ -12,7 +13,7 @@
from yarl import URL

from . import connector as connector_mod
from . import client_exceptions, client_reqrep, hdrs, http
from . import client_exceptions, client_reqrep, hdrs, http, payload
from .client_exceptions import * # noqa
from .client_exceptions import (ClientError, ClientOSError,
ClientResponseError, ServerTimeoutError,
Expand Down Expand Up @@ -45,8 +46,8 @@ class ClientSession:

def __init__(self, *, connector=None, loop=None, cookies=None,
headers=None, skip_auto_headers=None,
auth=None, request_class=ClientRequest,
response_class=ClientResponse,
auth=None, json_serialize=json.dumps,
request_class=ClientRequest, response_class=ClientResponse,
ws_response_class=ClientWebSocketResponse,
version=http.HttpVersion11,
cookie_jar=None, connector_owner=True, raise_for_status=False,
Expand Down Expand Up @@ -93,6 +94,7 @@ def __init__(self, *, connector=None, loop=None, cookies=None,
self._connector_owner = connector_owner
self._default_auth = auth
self._version = version
self._json_serialize = json_serialize
self._read_timeout = read_timeout
self._conn_timeout = conn_timeout
self._raise_for_status = raise_for_status
Expand Down Expand Up @@ -164,6 +166,8 @@ def _request(self, method, url, *,
if data is not None and json is not None:
raise ValueError(
'data and json parameters can not be used at the same time')
elif json is not None:
data = payload.JsonPayload(json)

This comment has been minimized.

Copy link
@popravich

popravich Mar 20, 2017

Member

dumps=self._json_serialize is missing

This comment has been minimized.

Copy link
@fafhrd91

fafhrd91 Mar 20, 2017

Author Member

oh, thanks


if not isinstance(chunked, bool) and chunked is not None:
warnings.warn(
Expand Down Expand Up @@ -208,7 +212,7 @@ def _request(self, method, url, *,

req = self._request_class(
method, url, params=params, headers=headers,
skip_auto_headers=skip_headers, data=data, json=json,
skip_auto_headers=skip_headers, data=data,
cookies=cookies, auth=auth, version=version,
compress=compress, chunked=chunked,
expect100=expect100, loop=self._loop,
Expand Down
5 changes: 1 addition & 4 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ClientRequest:

def __init__(self, method, url, *,
params=None, headers=None, skip_auto_headers=frozenset(),
data=None, json=None, cookies=None,
data=None, cookies=None,
auth=None, version=http.HttpVersion11, compress=None,
chunked=None, expect100=False,
loop=None, response_class=None,
Expand Down Expand Up @@ -84,9 +84,6 @@ def __init__(self, method, url, *,
if loop.get_debug():
self._source_traceback = traceback.extract_stack(sys._getframe(1))

if json is not None:
data = payload.JsonPayload(json)

self.update_version(version)
self.update_host(url)
self.update_headers(headers)
Expand Down
4 changes: 2 additions & 2 deletions aiohttp/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ class JsonPayload(BytesPayload):

def __init__(self, value,
encoding='utf-8', content_type='application/json',
*args, **kwargs):
dumps=json.dumps, *args, **kwargs):

super().__init__(
json.dumps(value).encode(encoding),
dumps(value).encode(encoding),
content_type=content_type, encoding=encoding, *args, **kwargs)


Expand Down
5 changes: 4 additions & 1 deletion docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ The client session supports the context manager protocol for self closing.

.. class:: ClientSession(*, connector=None, loop=None, cookies=None, \
headers=None, skip_auto_headers=None, \
auth=None, version=aiohttp.HttpVersion11, \
auth=None, json_serialize=json.dumps, \
version=aiohttp.HttpVersion11, \
cookie_jar=None, read_timeout=None, conn_timeout=None, \
raise_for_status=False)

Expand Down Expand Up @@ -100,6 +101,8 @@ The client session supports the context manager protocol for self closing.

.. versionadded:: 0.22

:param callable json_serialize: Json serializer function. (:func:`json.dumps` by default)

:param bool raise_for_status: Automatically call `raise_for_status()` for each response.
(default is False)

Expand Down
2 changes: 1 addition & 1 deletion docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ and :ref:`aiohttp-web-signals` handlers.

Clone itself with replacement some attributes.

Creates and returns a new instance of Request object. If no parameters
Creates and returns a new instance of Request object. If no parameters
are given, an exact copy is returned. If a parameter is not passed, it
will reuse the one from the current request object.

Expand Down

0 comments on commit cd6227b

Please sign in to comment.