bareclient
class HttpClient ¶
Summary¶
Make an HTTP client.
Description¶
The following example will make a GET request.
import asyncio
from bareclient import HttpClient
async def main(url: str) -> None:
async with HttpClient(url, method='GET') as response:
print(response)
if response.status == 200 and response.body is not None:
async for part in response.body:
print(part)
asyncio.run(main('https://docs.python.org/3/library/cgi.html'))
Parameters¶
url: strThe url
method: str (optional)The HTTP method. Defaults to 'GET'.
headers: Optional[List[Tuple[bytes, bytes]]] (optional)The headers. Defaults to None.
body: Optional[AsyncIterable[bytes]] (optional)The body content. Defaults to None.
loop: Optional[AbstractEventLoop] (optional)The optional asyncio event loop. Defaults to None.
h11_bufsiz: int (optional)The HTTP/1 buffer size. Defaults to 8096.
cafile: Optional[str] (optional)The path to a file of concatenated CA certificates in PEM format. Defaults to None.
capath: Optional[str] (optional)The path to a directory containing several CA certificates in PEM format. Defaults to None.
cadata: Optional[str] (optional)Either an ASCII string of one or more PEM-encoded certificates or a bytes-like object of DER-encoded certificates. Defaults to None.
ssl_context: Optional[SSLContext] (optional)An ssl context to be used instead of generating one from the certificates.
protocols: Iterable[str] (optional)The supported protocols. Defaults to DEFAULT_PROTOCOLS.
ciphers: Iterable[str] (optional)The supported ciphers. Defaults to DEFAULT_CIPHERS.
options: Iterable[int] (optional)The ssl.SSLContext.options. Defaults to DEFAULT_OPTIONS.
connect_timeout: Optional[Union[int, float]] (optional)The number of seconds to wait for the connection. Defaults to None.
middleware: Optional[List[HttpClientMiddlewareCallback]] (optional)Optional middleware. Defaults to None.
class Request ¶
Summary¶
An HTTP request.
Parameters¶
host: strThe host (
).
The scheme ('http'
or 'https'
).
The path.
method: strThe method (e.g. 'GET'
).
The headers.
body: Optional[AsyncIterable[bytes]]The body.
class method Request.from_url ¶
Summary¶
Create a request using a url.
Parameters¶
method: strThe request method: e.g. "POST".
headers: Optional[List[Tuple[bytes, bytes]]]The headers.
body: Optional[AsyncIterable[bytes]]The body.
class Response ¶
Summary¶
An HTTP response.
Parameters¶
url: strThe url.
status: intThe status code.
headers: List[Tuple[bytes, bytes]]The headers.
body: Optional[AsyncIterable[bytes]]The body.
async method Response.json ¶
Summary¶
Return the body as unpacked JSON.
Parameters¶
loads: Callable[[bytes], Any] (optional)The function
to parse the JSON. Defaults to json.loads
.
async method Response.raise_for_status ¶
Summary¶
Raise an error for a non 200 status code.
async method Response.raw ¶
Summary¶
Read the body as bytes.
module bareclient.helpers ¶
Summary¶
Helpers
async function bareclient.get ¶
Summary¶
Issues a GET request
Parameters¶
url: strThe url
headers: List[Tuple[bytes, bytes]] (optional)Any extra headers required. Defaults to None.
loop: Optional[AbstractEventLoop] (optional)The optional asyncio event loop.. Defaults to None.
cafile: Optional[str] (optional)The path to a file of concatenated CA certificates in PEM format. Defaults to None.
capath: Optional[str] (optional)The path to a directory containing several CA certificates in PEM format. Defaults to None.
cadata: Optional[str] (optional)Either an ASCII string of one or more PEM-encoded certificates or a bytes-like object of DER-encoded certificates. Defaults to None.
ssl_context: Optional[SSLContext] (optional)An ssl context to be used instead of generating one from the certificates.
protocols: Iterable[str] (optional)The supported protocols. Defaults to DEFAULT_PROTOCOLS.
ciphers: Iterable[str] (optional)The supported ciphers. Defaults to DEFAULT_CIPHERS.
options: Iterable[int] (optional)The ssl.SSLContext.options. Defaults to DEFAULT_OPTIONS.
connect_timeout: Optional[Union[int, float]] (optional)The number of seconds to wait for the connection. Defaults to None.
middleware: Optional[List[HttpClientMiddlewareCallback]] (optional)Optional middleware. Defaults to None.
async function bareclient.get_json ¶
Summary¶
Issues a GET request returning a JSON object
Description¶
The following gets some json:
import asyncio
from bareclient import get_json
async def main(url: str) -> None:
obj = await get_json(url)
print(obj)
asyncio.run(main('https://jsonplaceholder.typicode.com/todos/1'))
Parameters¶
url: strThe url
headers: List[Tuple[bytes, bytes]] (optional)Any extra headers required. Defaults to None.
loads: Callable[[bytes], Any] (optional)The function to loads the JSON object from the string. Defaults to json.loads.
loop: Optional[AbstractEventLoop] (optional)The optional asyncio event loop.. Defaults to None.
cafile: Optional[str] (optional)The path to a file of concatenated CA certificates in PEM format. Defaults to None.
capath: Optional[str] (optional)The path to a directory containing several CA certificates in PEM format. Defaults to None.
cadata: Optional[str] (optional)Either an ASCII string of one or more PEM-encoded certificates or a bytes-like object of DER-encoded certificates. Defaults to None.
ssl_context: Optional[SSLContext] (optional)An ssl context to be used instead of generating one from the certificates.
protocols: Iterable[str] (optional)The supported protocols. Defaults to DEFAULT_PROTOCOLS.
ciphers: Iterable[str] (optional)The supported ciphers. Defaults to DEFAULT_CIPHERS.
options: Iterable[int] (optional)The ssl.SSLContext.options. Defaults to DEFAULT_OPTIONS.
connect_timeout: Optional[Union[int, float]] (optional)The number of seconds to wait for the connection. Defaults to None.
middleware: Optional[List[HttpClientMiddlewareCallback]] (optional)Optional middleware. Defaults to None.
async function bareclient.get_text ¶
Summary¶
Issues a GET request returning a string
Description¶
The following gets some text:
import asyncio
from bareclient import get_text
async def main(url: str) -> None:
text = await get_text(url)
print(text)
asyncio.run(main('https://docs.python.org/3/library/cgi.html'))
Parameters¶
url: strThe url
headers: List[Tuple[bytes, bytes]] (optional)Any extra headers required. Defaults to None.
loop: Optional[AbstractEventLoop] (optional)The optional asyncio event loop.. Defaults to None.
encoding: str (optional) cafile: Optional[str] (optional)The path to a file of concatenated CA certificates in PEM format. Defaults to None.
capath: Optional[str] (optional)The path to a directory containing several CA certificates in PEM format. Defaults to None.
cadata: Optional[str] (optional)Either an ASCII string of one or more PEM-encoded certificates or a bytes-like object of DER-encoded certificates. Defaults to None.
ssl_context: Optional[SSLContext] (optional)An ssl context to be used instead of generating one from the certificates.
protocols: Iterable[str] (optional)The supported protocols. Defaults to DEFAULT_PROTOCOLS.
ciphers: Iterable[str] (optional)The supported ciphers. Defaults to DEFAULT_CIPHERS.
options: Iterable[int] (optional)The ssl.SSLContext.options. Defaults to DEFAULT_OPTIONS.
connect_timeout: Optional[Union[int, float]] (optional)The number of seconds to wait for the connection. Defaults to None.
middleware: Optional[List[HttpClientMiddlewareCallback]] (optional)Optional middleware. Defaults to None.
async function bareclient.post ¶
Summary¶
Issues a POST request
Parameters¶
url: strThe url
content: bytesThe body content
headers: List[Tuple[bytes, bytes]] (optional)Any extra headers required. Defaults to None.
loop: Optional[AbstractEventLoop] (optional)The optional asyncio event loop.. Defaults to None.
cafile: Optional[str] (optional)The path to a file of concatenated CA certificates in PEM format. Defaults to None.
capath: Optional[str] (optional)The path to a directory containing several CA certificates in PEM format. Defaults to None.
cadata: Optional[str] (optional)Either an ASCII string of one or more PEM-encoded certificates or a bytes-like object of DER-encoded certificates. Defaults to None.
ssl_context: Optional[SSLContext] (optional)An ssl context to be used instead of generating one from the certificates.
protocols: Iterable[str] (optional)The supported protocols. Defaults to DEFAULT_PROTOCOLS.
ciphers: Iterable[str] (optional)The supported ciphers. Defaults to DEFAULT_CIPHERS.
options: Iterable[int] (optional)The ssl.SSLContext.options. Defaults to DEFAULT_OPTIONS.
chunk_size: int (optional)The size of each chunk to send or -1 to send as a single chunk.. Defaults to -1.
connect_timeout: Optional[Union[int, float]] (optional)The number of seconds to wait for the connection. Defaults to None.
middleware: Optional[List[HttpClientMiddlewareCallback]] (optional)Optional middleware. Defaults to None.
async function bareclient.post_json ¶
Summary¶
Issues a POST request with a JSON payload
Description¶
import asyncio
from bareclient import post_json
async def main(url: str) -> None:
obj = await post_json(
url,
{'title': 'A job'},
headers=[(b'accept-encoding', b'gzip')]
)
print(obj)
asyncio.run(main('https://jsonplaceholder.typicode.com/todos'))
Parameters¶
url: strThe url
obj: AnyThe JSON payload
loads: Callable[[bytes], Any] (optional)The function used to decode the response. Defaults to json.loads.
dumps: Callable[[Any], str] (optional)The function used to encode the request. Defaults to json.dumps.
headers: List[Tuple[bytes, bytes]] (optional)Any extra headers required. Defaults to None.
loop: Optional[AbstractEventLoop] (optional)The optional asyncio event loop.. Defaults to None.
cafile: Optional[str] (optional)The path to a file of concatenated CA certificates in PEM format. Defaults to None.
capath: Optional[str] (optional)The path to a directory containing several CA certificates in PEM format. Defaults to None.
cadata: Optional[str] (optional)Either an ASCII string of one or more PEM-encoded certificates or a bytes-like object of DER-encoded certificates. Defaults to None.
ssl_context: Optional[SSLContext] (optional)An ssl context to be used instead of generating one from the certificates.
protocols: Iterable[str] (optional)The supported protocols. Defaults to DEFAULT_PROTOCOLS.
ciphers: Iterable[str] (optional)The supported ciphers. Defaults to DEFAULT_CIPHERS.
options: Iterable[int] (optional)The ssl.SSLContext.options. Defaults to DEFAULT_OPTIONS.
chunk_size: int (optional)The size of each chunk to send or -1 to send as a single chunk.. Defaults to -1.
connect_timeout: Optional[Union[int, float]] (optional)The number of seconds to wait for the connection. Defaults to None.
middleware: Optional[List[HttpClientMiddlewareCallback]] (optional)Optional middleware. Defaults to None.
async function bareclient.post_text ¶
Summary¶
Issues a POST request with a str body
Parameters¶
url: strThe url
text: str encoding: Any (optional) headers: List[Tuple[bytes, bytes]] (optional)Any extra headers required. Defaults to None.
loop: Optional[AbstractEventLoop] (optional)The optional asyncio event loop.. Defaults to None.
cafile: Optional[str] (optional)The path to a file of concatenated CA certificates in PEM format. Defaults to None.
capath: Optional[str] (optional)The path to a directory containing several CA certificates in PEM format. Defaults to None.
cadata: Optional[str] (optional)Either an ASCII string of one or more PEM-encoded certificates or a bytes-like object of DER-encoded certificates. Defaults to None.
ssl_context: Optional[SSLContext] (optional)An ssl context to be used instead of generating one from the certificates.
protocols: Iterable[str] (optional)The supported protocols. Defaults to DEFAULT_PROTOCOLS.
ciphers: Iterable[str] (optional)The supported ciphers. Defaults to DEFAULT_CIPHERS.
options: Iterable[int] (optional)The ssl.SSLContext.options. Defaults to DEFAULT_OPTIONS.
chunk_size: int (optional)The size of each chunk to send or -1 to send as a single chunk.. Defaults to -1.
connect_timeout: Optional[Union[int, float]] (optional)The number of seconds to wait for the connection. Defaults to None.
middleware: Optional[List[HttpClientMiddlewareCallback]] (optional)Optional middleware. Defaults to None.