From: Tom Christie Date: Fri, 17 May 2019 13:43:22 +0000 (+0100) Subject: Add json interfaces X-Git-Tag: 0.3.1~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5a63d313f0838f81be28f04a2cdf63daadb039c5;p=thirdparty%2Fhttpx.git Add json interfaces --- diff --git a/README.md b/README.md index a7315656..d5333ba7 100644 --- a/README.md +++ b/README.md @@ -102,10 +102,10 @@ inspiration around the lower level networking details. * `def .get(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])` * `def .options(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])` * `def .head(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])` -* `def .post(url, [data], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])` -* `def .put(url, [data], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])` -* `def .patch(url, [data], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])` -* `def .delete(url, [data], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])` +* `def .post(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])` +* `def .put(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])` +* `def .patch(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])` +* `def .delete(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])` * `def .prepare_request(request)` * `def .send(request, [stream], [allow_redirects], [ssl], [timeout])` * `def .close()` @@ -145,7 +145,7 @@ what gets sent over the wire.* >>> response = client.send(request) ``` -* `def __init__(method, url, [params], [data], [headers], [cookies])` +* `def __init__(method, url, [params], [data], [json], [headers], [cookies])` * `.method` - **str** * `.url` - **URL** * `.content` - **byte** or **byte async iterator** diff --git a/httpcore/client.py b/httpcore/client.py index e2a272e0..cc31e844 100644 --- a/httpcore/client.py +++ b/httpcore/client.py @@ -137,6 +137,7 @@ class AsyncClient: url: URLTypes, *, data: RequestData = b"", + json: typing.Any = None, params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, @@ -150,6 +151,7 @@ class AsyncClient: "POST", url, data=data, + json=json, params=params, headers=headers, cookies=cookies, @@ -165,6 +167,7 @@ class AsyncClient: url: URLTypes, *, data: RequestData = b"", + json: typing.Any = None, params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, @@ -178,6 +181,7 @@ class AsyncClient: "PUT", url, data=data, + json=json, params=params, headers=headers, cookies=cookies, @@ -193,6 +197,7 @@ class AsyncClient: url: URLTypes, *, data: RequestData = b"", + json: typing.Any = None, params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, @@ -206,6 +211,7 @@ class AsyncClient: "PATCH", url, data=data, + json=json, params=params, headers=headers, cookies=cookies, @@ -221,6 +227,7 @@ class AsyncClient: url: URLTypes, *, data: RequestData = b"", + json: typing.Any = None, params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, @@ -234,6 +241,7 @@ class AsyncClient: "DELETE", url, data=data, + json=json, params=params, headers=headers, cookies=cookies, @@ -250,6 +258,7 @@ class AsyncClient: url: URLTypes, *, data: RequestData = b"", + json: typing.Any = None, params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, @@ -263,6 +272,7 @@ class AsyncClient: method, url, data=data, + json=json, params=params, headers=headers, cookies=self.merge_cookies(cookies), @@ -492,6 +502,7 @@ class Client: url: URLTypes, *, data: RequestData = b"", + json: typing.Any = None, params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, @@ -505,6 +516,7 @@ class Client: method, url, data=data, + json=json, params=params, headers=headers, cookies=self._client.merge_cookies(cookies), @@ -600,6 +612,7 @@ class Client: url: URLTypes, *, data: RequestData = b"", + json: typing.Any = None, params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, @@ -613,6 +626,7 @@ class Client: "POST", url, data=data, + json=json, headers=headers, cookies=cookies, stream=stream, @@ -627,6 +641,7 @@ class Client: url: URLTypes, *, data: RequestData = b"", + json: typing.Any = None, params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, @@ -640,6 +655,7 @@ class Client: "PUT", url, data=data, + json=json, headers=headers, cookies=cookies, stream=stream, @@ -654,6 +670,7 @@ class Client: url: URLTypes, *, data: RequestData = b"", + json: typing.Any = None, params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, @@ -667,6 +684,7 @@ class Client: "PATCH", url, data=data, + json=json, headers=headers, cookies=cookies, stream=stream, @@ -681,6 +699,7 @@ class Client: url: URLTypes, *, data: RequestData = b"", + json: typing.Any = None, params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, @@ -694,6 +713,7 @@ class Client: "DELETE", url, data=data, + json=json, headers=headers, cookies=cookies, stream=stream, diff --git a/httpcore/models.py b/httpcore/models.py index c36c7a84..69ad6b15 100644 --- a/httpcore/models.py +++ b/httpcore/models.py @@ -1,7 +1,7 @@ import asyncio import cgi import email.message -import json +import json as jsonlib import typing import urllib.request from collections.abc import MutableMapping @@ -481,6 +481,7 @@ class Request: url: typing.Union[str, URL], *, data: RequestData = b"", + json: typing.Any = None, params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, @@ -492,6 +493,10 @@ class Request: self._cookies = Cookies(cookies) self._cookies.set_cookie_header(self) + if json is not None: + data = jsonlib.dumps(json).encode("utf-8") + self.headers["Content-Type"] = "application/json" + if isinstance(data, bytes): self.is_streaming = False self.content = data @@ -775,7 +780,7 @@ class Response: raise HttpError(message) def json(self) -> typing.Any: - return json.loads(self.content.decode("utf-8")) + return jsonlib.loads(self.content.decode("utf-8")) @property def cookies(self) -> "Cookies": diff --git a/tests/client/test_async_client.py b/tests/client/test_async_client.py index a1156545..01f45853 100644 --- a/tests/client/test_async_client.py +++ b/tests/client/test_async_client.py @@ -23,6 +23,14 @@ async def test_post(server): assert response.status_code == 200 +@pytest.mark.asyncio +async def test_post_json(server): + url = "http://127.0.0.1:8000/" + async with httpcore.AsyncClient() as client: + response = await client.post(url, json={"text": "Hello, world!"}) + assert response.status_code == 200 + + @pytest.mark.asyncio async def test_stream_response(server): async with httpcore.AsyncClient() as client: diff --git a/tests/client/test_client.py b/tests/client/test_client.py index 820efec1..f3663f69 100644 --- a/tests/client/test_client.py +++ b/tests/client/test_client.py @@ -48,6 +48,14 @@ def test_post(server): assert response.reason_phrase == "OK" +@threadpool +def test_post_json(server): + with httpcore.Client() as http: + response = http.post("http://127.0.0.1:8000/", json={"text": "Hello, world!"}) + assert response.status_code == 200 + assert response.reason_phrase == "OK" + + @threadpool def test_stream_response(server): with httpcore.Client() as http: