Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requests from transport API #1293

Merged
merged 2 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
HeaderTypes,
PrimitiveData,
QueryParamTypes,
RawURL,
RequestContent,
RequestData,
RequestFiles,
Expand All @@ -60,8 +61,18 @@


class URL:
def __init__(self, url: URLTypes = "", params: QueryParamTypes = None) -> None:
if isinstance(url, str):
def __init__(
self, url: typing.Union["URL", str, RawURL] = "", params: QueryParamTypes = None
) -> None:
if isinstance(url, (str, tuple)):
if isinstance(url, tuple):
raw_scheme, raw_host, port, raw_path = url
scheme = raw_scheme.decode("ascii")
host = raw_host.decode("ascii")
port_str = "" if port is None else f":{port}"
path = raw_path.decode("ascii")
url = f"{scheme}://{host}{port_str}{path}"

try:
self._uri_reference = rfc3986.iri_reference(url).encode()
except rfc3986.exceptions.InvalidAuthority as exc:
Expand Down Expand Up @@ -141,7 +152,7 @@ def fragment(self) -> str:
return self._uri_reference.fragment or ""

@property
def raw(self) -> typing.Tuple[bytes, bytes, typing.Optional[int], bytes]:
def raw(self) -> RawURL:
return (
self.scheme.encode("ascii"),
self.host.encode("ascii"),
Expand Down Expand Up @@ -585,8 +596,8 @@ def getlist(self, key: str, split_commas: bool = False) -> typing.List[str]:
class Request:
def __init__(
self,
method: str,
url: typing.Union[str, URL],
method: typing.Union[str, bytes],
url: typing.Union["URL", str, RawURL],
*,
params: QueryParamTypes = None,
headers: HeaderTypes = None,
Expand All @@ -597,7 +608,10 @@ def __init__(
json: typing.Any = None,
stream: ContentStream = None,
):
self.method = method.upper()
if isinstance(method, bytes):
self.method = method.decode("ascii").upper()
else:
self.method = method.upper()
self.url = URL(url, params=params)
self.headers = Headers(headers)
if cookies:
Expand Down
2 changes: 2 additions & 0 deletions httpx/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

PrimitiveData = Optional[Union[str, int, float, bool]]

RawURL = Tuple[bytes, bytes, Optional[int], bytes]

URLTypes = Union["URL", str]

QueryParamTypes = Union[
Expand Down
14 changes: 3 additions & 11 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ def request(
stream: httpcore.SyncByteStream = None,
timeout: Mapping[str, Optional[float]] = None,
) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], httpcore.SyncByteStream]:
raw_scheme, raw_host, port, raw_path = url
scheme = raw_scheme.decode("ascii")
host = raw_host.decode("ascii")
port_str = "" if port is None else f":{port}"
path = raw_path.decode("ascii")

request_headers = httpx.Headers(headers)
content = (
(item for item in stream)
Expand All @@ -54,17 +48,15 @@ def request(
)

request = httpx.Request(
method=method.decode("ascii"),
url=f"{scheme}://{host}{port_str}{path}",
method=method,
url=url,
headers=request_headers,
content=content,
)
request.read()
response = self.handler(request)
return (
response.http_version.encode("ascii")
if response.http_version
else b"HTTP/1.1",
(response.http_version or "HTTP/1.1").encode("ascii"),
response.status_code,
response.reason_phrase.encode("ascii"),
response.headers.raw,
Expand Down