Skip to content

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'))
bareclient.HttpClient(
url: str,
*,
method: str,
headers: Optional[List[Tuple[bytes, bytes]]],
body: Optional[AsyncIterable[bytes]],
loop: Optional[AbstractEventLoop],
h11_bufsiz: int,
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
connect_timeout: Optional[Union[int, float]],
middleware: Optional[List[HttpClientMiddlewareCallback]]
) -> None

Parameters

url: str

The 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.

bareclient.Request(
host: str,
scheme: str,
path: str,
method: str,
headers: Optional[List[Tuple[bytes, bytes]]],
body: Optional[AsyncIterable[bytes]]
) -> None

Parameters

host: str

The host ( [ ':' ]).

scheme: str

The scheme ('http' or 'https').

path: str

The path.

method: str

The method (e.g. 'GET').

headers: Optional[List[Tuple[bytes, bytes]]]

The headers.

body: Optional[AsyncIterable[bytes]]

The body.

property Request.url

Summary

The request URL.

url -> str
url: str = ...

class method Request.from_url

Summary

Create a request using a url.

Request.from_url(
method: str,
headers: Optional[List[Tuple[bytes, bytes]]],
body: Optional[AsyncIterable[bytes]]
) -> Request

Parameters

method: str

The request method: e.g. "POST".

headers: Optional[List[Tuple[bytes, bytes]]]

The headers.

body: Optional[AsyncIterable[bytes]]

The body.

Returns

Request: The request.

class Response

Summary

An HTTP response.

bareclient.Response(
url: str,
status: int,
headers: List[Tuple[bytes, bytes]],
body: Optional[AsyncIterable[bytes]]
) -> None

Parameters

url: str

The url.

status: int

The status code.

headers: List[Tuple[bytes, bytes]]

The headers.

body: Optional[AsyncIterable[bytes]]

The body.

property Response.ok

Summary

Check if the response was successful.

ok -> bool

async method Response.json

Summary

Return the body as unpacked JSON.

async Response.json(
loads: Callable[[bytes], Any]
) -> Optional[Any]

Parameters

loads: Callable[[bytes], Any] (optional)

The function to parse the JSON. Defaults to json.loads.

Returns

Optional[Any]: The unpacked JSON or None if the body was empty.

async method Response.raise_for_status

Summary

Raise an error for a non 200 status code.

async Response.raise_for_status() -> None

async method Response.raw

Summary

Read the body as bytes.

async Response.raw() -> Optional[bytes]

Returns

Optional[bytes]: The body as bytes or None if there was no body.

async method Response.text

Summary

Return the body as text.

Description

Note that the body can only be read once.

async Response.text(
encoding: str
) -> Optional[str]

Parameters

encoding: str (optional)

An optional encoding. Defaults to 'utf-8'.

Returns

Optional[str]: The body as text or None if there was no body.

module bareclient.helpers

Summary

Helpers

async function bareclient.get

Summary

Issues a GET request

async bareclient.get(
url: str,
*,
headers: List[Tuple[bytes, bytes]],
loop: Optional[AbstractEventLoop],
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
connect_timeout: Optional[Union[int, float]],
middleware: Optional[List[HttpClientMiddlewareCallback]]
) -> Optional[bytes]

Parameters

url: str

The 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.

Returns

Optional[bytes]: [description]

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'))
async bareclient.get_json(
url: str,
*,
headers: List[Tuple[bytes, bytes]],
loads: Callable[[bytes], Any],
loop: Optional[AbstractEventLoop],
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
connect_timeout: Optional[Union[int, float]],
middleware: Optional[List[HttpClientMiddlewareCallback]]
) -> Optional[Any]

Parameters

url: str

The 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.

Returns

Optional[Any]: The decoded JSON object

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'))
async bareclient.get_text(
url: str,
*,
headers: List[Tuple[bytes, bytes]],
loop: Optional[AbstractEventLoop],
encoding: str,
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
connect_timeout: Optional[Union[int, float]],
middleware: Optional[List[HttpClientMiddlewareCallback]]
) -> Optional[str]

Parameters

url: str

The 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.

Returns

Optional[str]: [description]

async function bareclient.post

Summary

Issues a POST request

async bareclient.post(
url: str,
content: bytes,
*,
headers: List[Tuple[bytes, bytes]],
loop: Optional[AbstractEventLoop],
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
chunk_size: int,
connect_timeout: Optional[Union[int, float]],
middleware: Optional[List[HttpClientMiddlewareCallback]]
) -> Optional[bytes]

Parameters

url: str

The url

content: bytes

The 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.

Returns

Optional[bytes]: The response body

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'))
async bareclient.post_json(
url: str,
obj: Any,
*,
loads: Callable[[bytes], Any],
dumps: Callable[[Any], str],
headers: List[Tuple[bytes, bytes]],
loop: Optional[AbstractEventLoop],
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
chunk_size: int,
connect_timeout: Optional[Union[int, float]],
middleware: Optional[List[HttpClientMiddlewareCallback]]
) -> Optional[Any]

Parameters

url: str

The url

obj: Any

The 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.

Returns

Optional[Any]: The decoded response

async function bareclient.post_text

Summary

Issues a POST request with a str body

async bareclient.post_text(
url: str,
text: str,
*,
encoding: Any,
headers: List[Tuple[bytes, bytes]],
loop: Optional[AbstractEventLoop],
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
chunk_size: int,
connect_timeout: Optional[Union[int, float]],
middleware: Optional[List[HttpClientMiddlewareCallback]]
) -> bytes

Parameters

url: str

The 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.

Returns

bytes: The response body