httpx/httpcore/api.py
Tom Christie 0cbf3c7581
Sync or Async dispatch (#83)
* Support thread-pooled dispatch

* Add ConcurrencyBackend.run

* Initial work towards support byte-iterators on sync request data

* Test case for byte iterator content

* byte iterator support for RequestData

* Add BaseResponse

* Bridge sync/async data in SyncResponse

* Add BaseClient

* SyncResponse -> Response

* Tweaking type annotation

* Distinct classes for Request, AsyncRequest

* Tweak is_streaming, content in BaseRequest

* Stream handling moves to client

* Handle mediating to AsyncResponse from a standard sync Dispatcher class

* Working on thread-pooled dispatcher

* Support threaded dispatch, inc. streaming requests/responses

* Increase test coverage

* Coverage and tweaks

* Include Accept and User-Agent headers by default
2019-06-10 12:26:03 +01:00

255 lines
5.5 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import typing
from .client import Client
from .config import CertTypes, TimeoutTypes, VerifyTypes
from .models import (
AuthTypes,
CookieTypes,
HeaderTypes,
QueryParamTypes,
RequestData,
Response,
URLTypes,
)
def request(
method: str,
url: URLTypes,
*,
params: QueryParamTypes = None,
data: RequestData = b"",
json: typing.Any = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
# files
auth: AuthTypes = None,
timeout: TimeoutTypes = None,
allow_redirects: bool = True,
# proxies
cert: CertTypes = None,
verify: VerifyTypes = True,
stream: bool = False,
) -> Response:
with Client() as client:
return client.request(
method=method,
url=url,
data=data,
json=json,
params=params,
headers=headers,
cookies=cookies,
stream=stream,
auth=auth,
allow_redirects=allow_redirects,
cert=cert,
verify=verify,
timeout=timeout,
)
def get(
url: URLTypes,
*,
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
stream: bool = False,
auth: AuthTypes = None,
allow_redirects: bool = True,
cert: CertTypes = None,
verify: VerifyTypes = True,
timeout: TimeoutTypes = None,
) -> Response:
return request(
"GET",
url,
headers=headers,
cookies=cookies,
stream=stream,
auth=auth,
allow_redirects=allow_redirects,
cert=cert,
verify=verify,
timeout=timeout,
)
def options(
url: URLTypes,
*,
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
stream: bool = False,
auth: AuthTypes = None,
allow_redirects: bool = True,
cert: CertTypes = None,
verify: VerifyTypes = True,
timeout: TimeoutTypes = None,
) -> Response:
return request(
"OPTIONS",
url,
headers=headers,
cookies=cookies,
stream=stream,
auth=auth,
allow_redirects=allow_redirects,
cert=cert,
verify=verify,
timeout=timeout,
)
def head(
url: URLTypes,
*,
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
stream: bool = False,
auth: AuthTypes = None,
allow_redirects: bool = False, #  Note: Differs to usual default.
cert: CertTypes = None,
verify: VerifyTypes = True,
timeout: TimeoutTypes = None,
) -> Response:
return request(
"HEAD",
url,
headers=headers,
cookies=cookies,
stream=stream,
auth=auth,
allow_redirects=allow_redirects,
cert=cert,
verify=verify,
timeout=timeout,
)
def post(
url: URLTypes,
*,
data: RequestData = b"",
json: typing.Any = None,
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
stream: bool = False,
auth: AuthTypes = None,
allow_redirects: bool = True,
cert: CertTypes = None,
verify: VerifyTypes = True,
timeout: TimeoutTypes = None,
) -> Response:
return request(
"POST",
url,
data=data,
json=json,
headers=headers,
cookies=cookies,
stream=stream,
auth=auth,
allow_redirects=allow_redirects,
cert=cert,
verify=verify,
timeout=timeout,
)
def put(
url: URLTypes,
*,
data: RequestData = b"",
json: typing.Any = None,
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
stream: bool = False,
auth: AuthTypes = None,
allow_redirects: bool = True,
cert: CertTypes = None,
verify: VerifyTypes = True,
timeout: TimeoutTypes = None,
) -> Response:
return request(
"PUT",
url,
data=data,
json=json,
headers=headers,
cookies=cookies,
stream=stream,
auth=auth,
allow_redirects=allow_redirects,
cert=cert,
verify=verify,
timeout=timeout,
)
def patch(
url: URLTypes,
*,
data: RequestData = b"",
json: typing.Any = None,
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
stream: bool = False,
auth: AuthTypes = None,
allow_redirects: bool = True,
cert: CertTypes = None,
verify: VerifyTypes = True,
timeout: TimeoutTypes = None,
) -> Response:
return request(
"PATCH",
url,
data=data,
json=json,
headers=headers,
cookies=cookies,
stream=stream,
auth=auth,
allow_redirects=allow_redirects,
cert=cert,
verify=verify,
timeout=timeout,
)
def delete(
url: URLTypes,
*,
data: RequestData = b"",
json: typing.Any = None,
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
stream: bool = False,
auth: AuthTypes = None,
allow_redirects: bool = True,
cert: CertTypes = None,
verify: VerifyTypes = True,
timeout: TimeoutTypes = None,
) -> Response:
return request(
"DELETE",
url,
data=data,
json=json,
headers=headers,
cookies=cookies,
stream=stream,
auth=auth,
allow_redirects=allow_redirects,
cert=cert,
verify=verify,
timeout=timeout,
)