Skip to content
Myr

Merge pull request #44 from encode/request-data

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Request data

authored by Tom Christie · committed by GitHub · parents d7f2478, e91c69d · Browse files · 10 files+89 added−48 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

10 files+89 added−48 removed

  • tests:+26 added−14 removed
  • other:+63 added−34 removed

10 files+89 added−48 removed

modified:httpcore/adapters/redirects.py+1 added-1 removed
63 unmodified lines
64 method = self.redirect_method(request, response)65 url = self.redirect_url(request, response)66 headers = self.redirect_headers(request, url)67 content = self.redirect_content(request, method)68 return Request(method=method, url=url, headers=headers, content=content)68 return Request(method=method, url=url, headers=headers, data=content)6970 def redirect_method(self, request: Request, response: Response) -> str:71 """72 When being redirected we may want to change the method of the request
58 unmodified lines
modified:httpcore/backends/sync.py+22 added-11 removed
12 unmodified lines
13 TimeoutConfig,14)15from ..models import (16 URL,17 ByteOrByteStream,17 Headers,18 HeaderTypes,19 QueryParamTypes,20 Request,21 RequestData,22 Response,23 URLTypes,24)25
74 unmodified lines
100 self,101 method: str,102 url: URLTypes,103 *,103 content: ByteOrByteStream = b"",104 data: RequestData = b"",105 query_params: QueryParamTypes = None,106 headers: HeaderTypes = None,107 stream: bool = False,108 allow_redirects: bool = True,109 ssl: SSLConfig = None,110 timeout: TimeoutConfig = None,111 ) -> SyncResponse:110 request = Request(method, url, headers=headers, content=content)112 request = Request(113 method, url, data=data, query_params=query_params, headers=headers114 )115 self.prepare_request(request)116 response = self.send(117 request,118 stream=stream,
6 unmodified lines
125 def get(126 self,127 url: URLTypes,128 *,129 query_params: QueryParamTypes = None,130 headers: HeaderTypes = None,131 stream: bool = False,132 allow_redirects: bool = True,133 ssl: SSLConfig = None,
12 unmodified lines
146 def options(147 self,148 url: URLTypes,149 *,150 query_params: QueryParamTypes = None,151 headers: HeaderTypes = None,152 stream: bool = False,153 allow_redirects: bool = True,154 ssl: SSLConfig = None,
12 unmodified lines
167 def head(168 self,169 url: URLTypes,170 *,171 query_params: QueryParamTypes = None,172 headers: HeaderTypes = None,173 stream: bool = False,174 allow_redirects: bool = False, #  Note: Differs to usual default.175 ssl: SSLConfig = None,
12 unmodified lines
188 def post(189 self,190 url: URLTypes,191 *,185 content: ByteOrByteStream = b"",192 data: RequestData = b"",193 query_params: QueryParamTypes = None,194 headers: HeaderTypes = None,195 stream: bool = False,196 allow_redirects: bool = True,197 ssl: SSLConfig = None,
1 unmodified line
199 ) -> SyncResponse:200 return self.request(201 "POST",202 url,195 content=content,203 data=data,204 headers=headers,205 stream=stream,206 allow_redirects=allow_redirects,207 ssl=ssl,
3 unmodified lines
211 def put(212 self,213 url: URLTypes,214 *,207 content: ByteOrByteStream = b"",215 data: RequestData = b"",216 query_params: QueryParamTypes = None,217 headers: HeaderTypes = None,218 stream: bool = False,219 allow_redirects: bool = True,220 ssl: SSLConfig = None,
1 unmodified line
222 ) -> SyncResponse:223 return self.request(224 "PUT",225 url,217 content=content,226 data=data,227 headers=headers,228 stream=stream,229 allow_redirects=allow_redirects,230 ssl=ssl,
3 unmodified lines
234 def patch(235 self,236 url: URLTypes,237 *,229 content: ByteOrByteStream = b"",238 data: RequestData = b"",239 query_params: QueryParamTypes = None,240 headers: HeaderTypes = None,241 stream: bool = False,242 allow_redirects: bool = True,243 ssl: SSLConfig = None,
1 unmodified line
245 ) -> SyncResponse:246 return self.request(247 "PATCH",248 url,239 content=content,249 data=data,250 headers=headers,251 stream=stream,252 allow_redirects=allow_redirects,253 ssl=ssl,
3 unmodified lines
257 def delete(258 self,259 url: URLTypes,260 *,251 content: ByteOrByteStream = b"",261 data: RequestData = b"",262 query_params: QueryParamTypes = None,263 headers: HeaderTypes = None,264 stream: bool = False,265 allow_redirects: bool = True,266 ssl: SSLConfig = None,
1 unmodified line
268 ) -> SyncResponse:269 return self.request(270 "DELETE",271 url,261 content=content,272 data=data,273 headers=headers,274 stream=stream,275 allow_redirects=allow_redirects,276 ssl=ssl,
39 unmodified lines
modified:httpcore/client.py+11 added-11 removed
15 unmodified lines
16)17from .dispatch.connection_pool import ConnectionPool18from .models import (19 URL,20 ByteOrByteStream,20 HeaderTypes,21 QueryParamTypes,22 Request,23 RequestData,24 Response,25 URLTypes,26)27
20 unmodified lines
48 self,49 method: str,50 url: URLTypes,51 *,52 content: ByteOrByteStream = b"",52 data: RequestData = b"",53 query_params: QueryParamTypes = None,54 headers: HeaderTypes = None,55 stream: bool = False,56 allow_redirects: bool = True,57 ssl: SSLConfig = None,58 timeout: TimeoutConfig = None,59 ) -> Response:60 request = Request(61 method, url, query_params=query_params, headers=headers, content=content61 method, url, data=data, query_params=query_params, headers=headers62 )63 self.prepare_request(request)64 response = await self.send(65 request,
73 unmodified lines
139 async def post(140 self,141 url: URLTypes,142 *,143 content: ByteOrByteStream = b"",143 data: RequestData = b"",144 query_params: QueryParamTypes = None,145 headers: HeaderTypes = None,146 stream: bool = False,147 allow_redirects: bool = True,
2 unmodified lines
150 ) -> Response:151 return await self.request(152 "POST",153 url,154 content=content,154 data=data,155 query_params=query_params,156 headers=headers,157 stream=stream,158 allow_redirects=allow_redirects,
4 unmodified lines
163 async def put(164 self,165 url: URLTypes,166 *,167 content: ByteOrByteStream = b"",167 data: RequestData = b"",168 query_params: QueryParamTypes = None,169 headers: HeaderTypes = None,170 stream: bool = False,171 allow_redirects: bool = True,
2 unmodified lines
174 ) -> Response:175 return await self.request(176 "PUT",177 url,178 content=content,178 data=data,179 query_params=query_params,180 headers=headers,181 stream=stream,182 allow_redirects=allow_redirects,
4 unmodified lines
187 async def patch(188 self,189 url: URLTypes,190 *,191 content: ByteOrByteStream = b"",191 data: RequestData = b"",192 query_params: QueryParamTypes = None,193 headers: HeaderTypes = None,194 stream: bool = False,195 allow_redirects: bool = True,
2 unmodified lines
198 ) -> Response:199 return await self.request(200 "PATCH",201 url,202 content=content,202 data=data,203 query_params=query_params,204 headers=headers,205 stream=stream,206 allow_redirects=allow_redirects,
4 unmodified lines
211 async def delete(212 self,213 url: URLTypes,214 *,215 content: ByteOrByteStream = b"",215 data: RequestData = b"",216 query_params: QueryParamTypes = None,217 headers: HeaderTypes = None,218 stream: bool = False,219 allow_redirects: bool = True,
2 unmodified lines
222 ) -> Response:223 return await self.request(224 "DELETE",225 url,226 content=content,226 data=data,227 query_params=query_params,228 headers=headers,229 stream=stream,230 allow_redirects=allow_redirects,
41 unmodified lines
modified:httpcore/interfaces.py+14 added-3 removed
1import typing2from types import TracebackType34from .config import TimeoutConfig5from .models import URL, ByteOrByteStream, HeaderTypes, Request, Response, URLTypes5from .models import (6 URL,7 HeaderTypes,8 QueryParamTypes,9 Request,10 RequestData,11 Response,12 URLTypes,13)1415OptionalTimeout = typing.Optional[TimeoutConfig]1617
10 unmodified lines
28 self,29 method: str,30 url: URLTypes,31 *,32 data: RequestData = b"",33 query_params: QueryParamTypes = None,34 headers: HeaderTypes = None,25 content: ByteOrByteStream = b"",35 **options: typing.Any,36 ) -> Response:28 request = Request(method, url, headers=headers, content=content)37 request = Request(38 method, url, data=data, query_params=query_params, headers=headers39 )40 self.prepare_request(request)41 response = await self.send(request, **options)42 return response43
61 unmodified lines
modified:httpcore/models.py+15 added-8 removed
42 unmodified lines
43 typing.Dict[typing.AnyStr, typing.AnyStr],44 typing.List[typing.Tuple[typing.AnyStr, typing.AnyStr]],45]4647ByteOrByteStream = typing.Union[bytes, typing.AsyncIterator[bytes]]47RequestData = typing.Union[dict, bytes, typing.AsyncIterator[bytes]]4849ResponseContent = typing.Union[bytes, typing.AsyncIterator[bytes]]505152class URL:53 def __init__(54 self,55 url: URLTypes,
142 unmodified lines
198 def __hash__(self) -> int:199 return hash((self.is_ssl, self.host, self.port))200201200class QueryParams(typing.Mapping):202class QueryParams(typing.Mapping[str, str]):203 """204 URL query parameters, as a multi-dict.205 """206
250 unmodified lines
457 self,458 method: str,459 url: typing.Union[str, URL],460 *,461 data: RequestData = b"",462 query_params: QueryParamTypes = None,463 headers: HeaderTypes = None,461 content: ByteOrByteStream = b"",464 ):465 self.method = method.upper()466 self.url = URL(url, query_params=query_params)467 self.headers = Headers(headers)468465 if isinstance(content, bytes):469 if isinstance(data, bytes):470 self.is_streaming = False467 self.content = content471 self.content = data472 elif isinstance(data, dict):473 self.is_streaming = False474 self.content = urlencode(data, doseq=True).encode("utf-8")475 self.headers["Content-Type"] = "application/x-www-form-urlencoded"476 else:477 self.is_streaming = True470 self.content_aiter = content471 self.headers = Headers(headers)478 self.content_aiter = data479480 async def read(self) -> bytes:481 """482 Read and return the response content.
55 unmodified lines
538 *,539 reason_phrase: str = None,540 protocol: str = None,541 headers: HeaderTypes = None,535 content: ByteOrByteStream = b"",542 content: ResponseContent = b"",543 on_close: typing.Callable = None,544 request: Request = None,545 history: typing.List["Response"] = None,546 ):
207 unmodified lines
modified:tests/adapters/test_redirects.py+3 added-3 removed
220 unmodified lines
221@pytest.mark.asyncio222async def test_body_redirect():223 client = RedirectAdapter(MockDispatch())224 url = "https://example.org/redirect_body"225 content = b"Example request body"226 response = await client.request("POST", url, content=content)225 data = b"Example request body"226 response = await client.request("POST", url, data=data)227 data = json.loads(response.content.decode())228 assert response.url == URL("https://example.org/redirect_body_target")229 assert data == {"body": "Example request body"}230
6 unmodified lines
237 async def streaming_body():238 yield b"Example request body"239240 with pytest.raises(RedirectBodyUnavailable):241 await client.request("POST", url, content=streaming_body())241 await client.request("POST", url, data=streaming_body())
modified:tests/dispatch/test_http2.py+1 added-1 removed
90 unmodified lines
91@pytest.mark.asyncio92async def test_http2_post_request():93 server = MockServer()94 async with httpcore.HTTP2Connection(reader=server, writer=server) as conn:95 response = await conn.request("POST", "http://example.org", content=b"<data>")95 response = await conn.request("POST", "http://example.org", data=b"<data>")96 assert response.status_code == 20097 assert json.loads(response.content) == {98 "method": "POST",99 "path": "/",
20 unmodified lines
modified:tests/models/test_requests.py+19 added-7 removed
15 unmodified lines
16 )171819def test_content_length_header():20 request = httpcore.Request("POST", "http://example.org", content=b"test 123")20 request = httpcore.Request("POST", "http://example.org", data=b"test 123")21 request.prepare()22 assert request.headers == httpcore.Headers(23 [24 (b"host", b"example.org"),
2 unmodified lines
27 ]28 )293031def test_url_encoded_data():32 request = httpcore.Request("POST", "http://example.org", data={"test": "123"})33 request.prepare()34 assert request.headers == httpcore.Headers(35 [36 (b"host", b"example.org"),37 (b"content-length", b"8"),38 (b"accept-encoding", b"deflate, gzip, br"),39 (b"content-type", b"application/x-www-form-urlencoded"),40 ]41 )42 assert request.content == b"test=123"434445def test_transfer_encoding_header():46 async def streaming_body(data):47 yield data # pragma: nocover4835 content = streaming_body(b"test 123")49 data = streaming_body(b"test 123")5037 request = httpcore.Request("POST", "http://example.org", content=content)51 request = httpcore.Request("POST", "http://example.org", data=data)52 request.prepare()53 assert request.headers == httpcore.Headers(54 [55 (b"host", b"example.org"),
26 unmodified lines
82def test_override_content_length_header():83 async def streaming_body(data):84 yield data # pragma: nocover8572 content = streaming_body(b"test 123")86 data = streaming_body(b"test 123")87 headers = [(b"content-length", b"8")]8875 request = httpcore.Request(76 "POST", "http://example.org", content=content, headers=headers77 )89 request = httpcore.Request("POST", "http://example.org", data=data, headers=headers)90 request.prepare()91 assert request.headers == httpcore.Headers(92 [93 (b"host", b"example.org"),
27 unmodified lines
modified:tests/test_client.py+2 added-2 removed
14 unmodified lines
15@pytest.mark.asyncio16async def test_post(server):17 url = "http://127.0.0.1:8000/"18 async with httpcore.Client() as client:19 response = await client.post(url, content=b"Hello, world!")19 response = await client.post(url, data=b"Hello, world!")20 assert response.status_code == 200212223@pytest.mark.asyncio
22 unmodified lines
46 yield b"world!"4748 async with httpcore.Client() as client:49 response = await client.request(50 "POST", "http://127.0.0.1:8000/", content=hello_world()50 "POST", "http://127.0.0.1:8000/", data=hello_world()51 )52 assert response.status_code == 2005354
13 unmodified lines
modified:tests/test_sync.py+1 added-1 removed
33 unmodified lines
3435@threadpool36def test_post(server):37 with httpcore.SyncClient() as http:38 response = http.post("http://127.0.0.1:8000/", content=b"Hello, world!")38 response = http.post("http://127.0.0.1:8000/", data=b"Hello, world!")39 assert response.status_code == 20040 assert response.reason_phrase == "OK"4142
29 unmodified lines