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
Provenance
- Commit
39a5c53- Tree
ebe8fa7- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent 1
d7f2478- Parent 2
e91c69d
10 files changed+89 added−48 removed
modified:httpcore/adapters/redirects.py+1 added-1 removed
63 unmodified lines64 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)69 70 def redirect_method(self, request: Request, response: Response) -> str:71 """72 When being redirected we may want to change the method of the request58 unmodified linesmodified:httpcore/backends/sync.py+22 added-11 removed
12 unmodified lines13 TimeoutConfig,14 )15 from ..models import (16 URL,17- ByteOrByteStream,17 Headers,18 HeaderTypes,19+ QueryParamTypes,20 Request,21+ RequestData,22 Response,23 URLTypes,24 )25 74 unmodified lines100 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 lines125 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 lines146 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 lines167 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 lines188 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 line199 ) -> 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 lines211 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 line222 ) -> 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 lines234 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 line245 ) -> 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 lines257 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 line268 ) -> 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 linesmodified:httpcore/client.py+11 added-11 removed
15 unmodified lines16 )17 from .dispatch.connection_pool import ConnectionPool18 from .models import (19 URL,20- ByteOrByteStream,20 HeaderTypes,21 QueryParamTypes,22 Request,23+ RequestData,24 Response,25 URLTypes,26 )27 20 unmodified lines48 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 lines139 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 lines150 ) -> 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 lines163 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 lines174 ) -> 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 lines187 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 lines198 ) -> 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 lines211 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 lines222 ) -> 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 linesmodified:httpcore/interfaces.py+14 added-3 removed
1 import typing2 from types import TracebackType3 4 from .config import TimeoutConfig5-from .models import URL, ByteOrByteStream, HeaderTypes, Request, Response, URLTypes5+from .models import (6+ URL,7+ HeaderTypes,8+ QueryParamTypes,9+ Request,10+ RequestData,11+ Response,12+ URLTypes,13+)14 15 OptionalTimeout = typing.Optional[TimeoutConfig]16 17 10 unmodified lines28 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 linesmodified:httpcore/models.py+15 added-8 removed
42 unmodified lines43 typing.Dict[typing.AnyStr, typing.AnyStr],44 typing.List[typing.Tuple[typing.AnyStr, typing.AnyStr]],45 ]46 47-ByteOrByteStream = typing.Union[bytes, typing.AsyncIterator[bytes]]47+RequestData = typing.Union[dict, bytes, typing.AsyncIterator[bytes]]48 49+ResponseContent = typing.Union[bytes, typing.AsyncIterator[bytes]]50 51+52 class URL:53 def __init__(54 self,55 url: URLTypes,142 unmodified lines198 def __hash__(self) -> int:199 return hash((self.is_ssl, self.host, self.port))200 201 200-class QueryParams(typing.Mapping):202+class QueryParams(typing.Mapping[str, str]):203 """204 URL query parameters, as a multi-dict.205 """206 250 unmodified lines457 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)468+465- 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 = data479 480 async def read(self) -> bytes:481 """482 Read and return the response content.55 unmodified lines538 *,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 linesmodified:tests/adapters/test_redirects.py+3 added-3 removed
220 unmodified lines221 @pytest.mark.asyncio222 async 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 lines237 async def streaming_body():238 yield b"Example request body"239 240 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 lines91 @pytest.mark.asyncio92 async 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 linesmodified:tests/models/test_requests.py+19 added-7 removed
15 unmodified lines16 )17 18 19 def 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 lines27 ]28 )29 30 31+def 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"43+44+45 def test_transfer_encoding_header():46 async def streaming_body(data):47 yield data # pragma: nocover48 35- content = streaming_body(b"test 123")49+ data = streaming_body(b"test 123")50 37- 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 lines82 def test_override_content_length_header():83 async def streaming_body(data):84 yield data # pragma: nocover85 72- content = streaming_body(b"test 123")86+ data = streaming_body(b"test 123")87 headers = [(b"content-length", b"8")]88 75- 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 linesmodified:tests/test_client.py+2 added-2 removed
14 unmodified lines15 @pytest.mark.asyncio16 async 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 == 20021 22 23 @pytest.mark.asyncio22 unmodified lines46 yield b"world!"47 48 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 == 20053 54 13 unmodified linesmodified:tests/test_sync.py+1 added-1 removed
33 unmodified lines34 35 @threadpool36 def 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"41 42 29 unmodified lines