Add json interfaces
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by Tom Christie · parent 2116d44 · Browse files · 5 files·+48 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+48 added−7 removed
- tests:+16 added−0 removed
- other:+32 added−7 removed
Provenance
- Commit
5a63d31- Tree
e927418- Author
Tom Christie <tom@tomchristie.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
2116d44
5 files changed+48 added−7 removed
modified:httpcore/client.py+20 added
135 unmodified lines136 self,137 url: URLTypes,138 *,139 data: RequestData = b"",140+ json: typing.Any = None,141 params: QueryParamTypes = None,142 headers: HeaderTypes = None,143 cookies: CookieTypes = None,144 stream: bool = False,5 unmodified lines150 return await self.request(151 "POST",152 url,153 data=data,154+ json=json,155 params=params,156 headers=headers,157 cookies=cookies,158 stream=stream,7 unmodified lines166 self,167 url: URLTypes,168 *,169 data: RequestData = b"",170+ json: typing.Any = None,171 params: QueryParamTypes = None,172 headers: HeaderTypes = None,173 cookies: CookieTypes = None,174 stream: bool = False,5 unmodified lines180 return await self.request(181 "PUT",182 url,183 data=data,184+ json=json,185 params=params,186 headers=headers,187 cookies=cookies,188 stream=stream,7 unmodified lines196 self,197 url: URLTypes,198 *,199 data: RequestData = b"",200+ json: typing.Any = None,201 params: QueryParamTypes = None,202 headers: HeaderTypes = None,203 cookies: CookieTypes = None,204 stream: bool = False,5 unmodified lines210 return await self.request(211 "PATCH",212 url,213 data=data,214+ json=json,215 params=params,216 headers=headers,217 cookies=cookies,218 stream=stream,7 unmodified lines226 self,227 url: URLTypes,228 *,229 data: RequestData = b"",230+ json: typing.Any = None,231 params: QueryParamTypes = None,232 headers: HeaderTypes = None,233 cookies: CookieTypes = None,234 stream: bool = False,5 unmodified lines240 return await self.request(241 "DELETE",242 url,243 data=data,244+ json=json,245 params=params,246 headers=headers,247 cookies=cookies,248 stream=stream,8 unmodified lines257 method: str,258 url: URLTypes,259 *,260 data: RequestData = b"",261+ json: typing.Any = None,262 params: QueryParamTypes = None,263 headers: HeaderTypes = None,264 cookies: CookieTypes = None,265 stream: bool = False,5 unmodified lines271 request = Request(272 method,273 url,274 data=data,275+ json=json,276 params=params,277 headers=headers,278 cookies=self.merge_cookies(cookies),279 )221 unmodified lines501 method: str,502 url: URLTypes,503 *,504 data: RequestData = b"",505+ json: typing.Any = None,506 params: QueryParamTypes = None,507 headers: HeaderTypes = None,508 cookies: CookieTypes = None,509 stream: bool = False,5 unmodified lines515 request = Request(516 method,517 url,518 data=data,519+ json=json,520 params=params,521 headers=headers,522 cookies=self._client.merge_cookies(cookies),523 )87 unmodified lines611 self,612 url: URLTypes,613 *,614 data: RequestData = b"",615+ json: typing.Any = None,616 params: QueryParamTypes = None,617 headers: HeaderTypes = None,618 cookies: CookieTypes = None,619 stream: bool = False,5 unmodified lines625 return self.request(626 "POST",627 url,628 data=data,629+ json=json,630 headers=headers,631 cookies=cookies,632 stream=stream,633 auth=auth,6 unmodified lines640 self,641 url: URLTypes,642 *,643 data: RequestData = b"",644+ json: typing.Any = None,645 params: QueryParamTypes = None,646 headers: HeaderTypes = None,647 cookies: CookieTypes = None,648 stream: bool = False,5 unmodified lines654 return self.request(655 "PUT",656 url,657 data=data,658+ json=json,659 headers=headers,660 cookies=cookies,661 stream=stream,662 auth=auth,6 unmodified lines669 self,670 url: URLTypes,671 *,672 data: RequestData = b"",673+ json: typing.Any = None,674 params: QueryParamTypes = None,675 headers: HeaderTypes = None,676 cookies: CookieTypes = None,677 stream: bool = False,5 unmodified lines683 return self.request(684 "PATCH",685 url,686 data=data,687+ json=json,688 headers=headers,689 cookies=cookies,690 stream=stream,691 auth=auth,6 unmodified lines698 self,699 url: URLTypes,700 *,701 data: RequestData = b"",702+ json: typing.Any = None,703 params: QueryParamTypes = None,704 headers: HeaderTypes = None,705 cookies: CookieTypes = None,706 stream: bool = False,5 unmodified lines712 return self.request(713 "DELETE",714 url,715 data=data,716+ json=json,717 headers=headers,718 cookies=cookies,719 stream=stream,720 auth=auth,43 unmodified linesmodified:httpcore/models.py+7 added-2 removed
1 import asyncio2 import cgi3 import email.message4-import json4+import json as jsonlib5 import typing6 import urllib.request7 from collections.abc import MutableMapping8 from http.cookiejar import Cookie, CookieJar471 unmodified lines480 method: str,481 url: typing.Union[str, URL],482 *,483 data: RequestData = b"",484+ json: typing.Any = None,485 params: QueryParamTypes = None,486 headers: HeaderTypes = None,487 cookies: CookieTypes = None,488 ):3 unmodified lines492 if cookies:493 self._cookies = Cookies(cookies)494 self._cookies.set_cookie_header(self)495 496+ if json is not None:497+ data = jsonlib.dumps(json).encode("utf-8")498+ self.headers["Content-Type"] = "application/json"499+500 if isinstance(data, bytes):501 self.is_streaming = False502 self.content = data503 elif isinstance(data, dict):275 unmodified lines779 if message:780 raise HttpError(message)781 782 def json(self) -> typing.Any:778- return json.loads(self.content.decode("utf-8"))783+ return jsonlib.loads(self.content.decode("utf-8"))784 785 @property786 def cookies(self) -> "Cookies":787 if not hasattr(self, "_cookies"):268 unmodified linesmodified:tests/client/test_async_client.py+8 added
22 unmodified lines23 assert response.status_code == 20024 25 26 @pytest.mark.asyncio27+async def test_post_json(server):28+ url = "http://127.0.0.1:8000/"29+ async with httpcore.AsyncClient() as client:30+ response = await client.post(url, json={"text": "Hello, world!"})31+ assert response.status_code == 20032+33+34+@pytest.mark.asyncio35 async def test_stream_response(server):36 async with httpcore.AsyncClient() as client:37 response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)38 assert response.status_code == 20096 unmodified linesmodified:tests/client/test_client.py+8 added
47 unmodified lines48 assert response.reason_phrase == "OK"49 50 51 @threadpool52+def test_post_json(server):53+ with httpcore.Client() as http:54+ response = http.post("http://127.0.0.1:8000/", json={"text": "Hello, world!"})55+ assert response.status_code == 20056+ assert response.reason_phrase == "OK"57+58+59+@threadpool60 def test_stream_response(server):61 with httpcore.Client() as http:62 response = http.get("http://127.0.0.1:8000/", stream=True)63 assert response.status_code == 20080 unmodified linesmodified:README.md+5 added-5 removed
100 unmodified lines101 * `def .request(method, url, [data], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`102 * `def .get(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`103 * `def .options(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`104 * `def .head(url, [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`105-* `def .post(url, [data], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`106-* `def .put(url, [data], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`107-* `def .patch(url, [data], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`108-* `def .delete(url, [data], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`105+* `def .post(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`106+* `def .put(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`107+* `def .patch(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`108+* `def .delete(url, [data], [json], [params], [headers], [cookies], [auth], [stream], [allow_redirects], [ssl], [timeout])`109 * `def .prepare_request(request)`110 * `def .send(request, [stream], [allow_redirects], [ssl], [timeout])`111 * `def .close()`112 31 unmodified lines144 >>> request = Request("GET", "https://example.org", headers={'host': 'example.org'})145 >>> response = client.send(request)146 ```147 148-* `def __init__(method, url, [params], [data], [headers], [cookies])`148+* `def __init__(method, url, [params], [data], [json], [headers], [cookies])`149 * `.method` - **str**150 * `.url` - **URL**151 * `.content` - **byte** or **byte async iterator**152 * `.headers` - **Headers**98 unmodified lines