Requests from transport API (#1293)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Refactoring to support instantiating requests from transport API * Minor refactoring
authored by Tom Christie · committed by GitHub · parent 09f94ed · Browse files · 3 files·+25 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+25 added−17 removed
- tests:+3 added−11 removed
- other:+22 added−6 removed
Provenance
- Commit
e1f7791- Tree
f794b7e- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
09f94ed
3 files changed+25 added−17 removed
modified:httpx/_models.py+20 added-6 removed
39 unmodified lines40 CookieTypes,41 HeaderTypes,42 PrimitiveData,43 QueryParamTypes,44+ RawURL,45 RequestContent,46 RequestData,47 RequestFiles,48 ResponseContent,11 unmodified lines60 )61 62 63 class URL:63- def __init__(self, url: URLTypes = "", params: QueryParamTypes = None) -> None:64- if isinstance(url, str):64+ def __init__(65+ self, url: typing.Union["URL", str, RawURL] = "", params: QueryParamTypes = None66+ ) -> None:67+ if isinstance(url, (str, tuple)):68+ if isinstance(url, tuple):69+ raw_scheme, raw_host, port, raw_path = url70+ scheme = raw_scheme.decode("ascii")71+ host = raw_host.decode("ascii")72+ port_str = "" if port is None else f":{port}"73+ path = raw_path.decode("ascii")74+ url = f"{scheme}://{host}{port_str}{path}"75+76 try:77 self._uri_reference = rfc3986.iri_reference(url).encode()78 except rfc3986.exceptions.InvalidAuthority as exc:79 raise InvalidURL(message=str(exc)) from None71 unmodified lines151 def fragment(self) -> str:152 return self._uri_reference.fragment or ""153 154 @property144- def raw(self) -> typing.Tuple[bytes, bytes, typing.Optional[int], bytes]:155+ def raw(self) -> RawURL:156 return (157 self.scheme.encode("ascii"),158 self.host.encode("ascii"),159 self.port,435 unmodified lines595 596 class Request:597 def __init__(598 self,588- method: str,589- url: typing.Union[str, URL],599+ method: typing.Union[str, bytes],600+ url: typing.Union["URL", str, RawURL],601 *,602 params: QueryParamTypes = None,603 headers: HeaderTypes = None,604 cookies: CookieTypes = None,2 unmodified lines607 files: RequestFiles = None,608 json: typing.Any = None,609 stream: ContentStream = None,610 ):611+ if isinstance(method, bytes):612+ self.method = method.decode("ascii").upper()613+ else:600- self.method = method.upper()614+ self.method = method.upper()615 self.url = URL(url, params=params)616 self.headers = Headers(headers)617 if cookies:618 Cookies(cookies).set_cookie_header(self)643 unmodified linesmodified:httpx/_types.py+2 added
25 unmodified lines26 27 28 PrimitiveData = Optional[Union[str, int, float, bool]]29 30+RawURL = Tuple[bytes, bytes, Optional[int], bytes]31+32 URLTypes = Union["URL", str]33 34 QueryParamTypes = Union[35 "QueryParams",48 unmodified linesmodified:tests/utils.py+3 added-11 removed
34 unmodified lines35 headers: List[Tuple[bytes, bytes]] = None,36 stream: httpcore.SyncByteStream = None,37 timeout: Mapping[str, Optional[float]] = None,38 ) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], httpcore.SyncByteStream]:39- raw_scheme, raw_host, port, raw_path = url40- scheme = raw_scheme.decode("ascii")41- host = raw_host.decode("ascii")42- port_str = "" if port is None else f":{port}"43- path = raw_path.decode("ascii")44-39 request_headers = httpx.Headers(headers)40 content = (41 (item for item in stream)42 if stream4 unmodified lines47 else None48 )49 50 request = httpx.Request(57- method=method.decode("ascii"),58- url=f"{scheme}://{host}{port_str}{path}",51+ method=method,52+ url=url,53 headers=request_headers,54 content=content,55 )56 request.read()57 response = self.handler(request)58 return (65- response.http_version.encode("ascii")66- if response.http_version67- else b"HTTP/1.1",59+ (response.http_version or "HTTP/1.1").encode("ascii"),60 response.status_code,61 response.reason_phrase.encode("ascii"),62 response.headers.raw,63 response._raw_stream,40 unmodified lines