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_code'] == 200 and response['more_body']:
            async for part in response['body']:
                print(part)

asyncio.run(main('https://docs.python.org/3/library/cgi.html'))

The response is a mapping with the following content:

  • type (Unicode string) - Currently the only response is "http.response".
  • acgi["version"] (Unicode string) - Version of the ACGI spec.
  • http_version (Unicode string) - One of "1.0", "1.1" or "2".
  • stream_id (int) - The HTTP/2 stream id, otherwise None.
  • status_code (int) - The HTTP status code.
  • headers (Iterable[[byte string, byte string]]) - A iterable of [name, value] two-item iterables, where name is the header name, and value is the header value. Order must be preserved in the HTTP response. Header names must be lowercased. Optional; defaults to an empty list. Pseudo headers (present in HTTP/2 and HTTP/3) must not be present.
  • more_body (bool) - Signifies if the body has more content.
  • body (AsyncIterable[byte string]) - The body content.
bareclient.HttpClient(
url: str,
*,
method: str,
headers: Optional[List[Header]],
content: Optional[Content],
loop: Optional[AbstractEventLoop],
h11_bufsiz: int,
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
decompressors: Optional[Mapping[bytes, Type[Decompressor]]],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
connect_timeout: Optional[Union[int, float]]
) -> None

Parameters

url: str

The url

method: str (optional)

The HTTP method. Defaults to 'GET'.

headers: Optional[List[Header]] (optional)

The headers. Defaults to None.

content: Optional[Content] (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.

decompressors: Optional[Mapping[bytes, Type[Decompressor]]] (optional)

The decompressors. Defaults to None.

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.

class HttpSession

Summary

Initialise an HTTP session

Description

The following makes a get request from a session:

import asyncio
from bareclient import HttpClient


async def main(url: str, path: str) -> None:
    session =  HttpSession(url)
    async with session.request(path, method='GET') as response:
        print(response)
        if response['status_code'] == 200 and response['more_body']:
            async for part in response['body']:
                print(part)

asyncio.run(main('https://docs.python.org', '/3/library/cgi.html'))
bareclient.HttpSession(
url: str,
*,
headers: Optional[List[Header]],
cookies: Optional[Mapping[bytes, List[Cookie]]],
loop: Optional[AbstractEventLoop],
h11_bufsiz: int,
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
decompressors: Optional[Mapping[bytes, Type[Decompressor]]],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
connect_timeout: Optional[Union[int, float]]
) -> None

Parameters

url: str

The url

headers: Optional[List[Header]] (optional)

The headers. Defaults to None.

cookies: Optional[Mapping[bytes, List[Cookie]]] (optional)

The cookies. Defaults to None.

loop: Optional[AbstractEventLoop] (optional)

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

decompressors: Optional[Mapping[bytes, Type[Decompressor]]] (optional)

The decompressors. Defaults to None.

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.

method HttpSession.request

Summary

Make an HTTP request

HttpSession.request(
path: str,
*,
method: str,
headers: Optional[List[Header]],
content: Optional[Content]
) -> HttpSessionInstance

Parameters

path: str

The path excluding the scheme and host part

method: str (optional)

The HTTP method, defaults to 'GET'. Defaults to 'GET'.

headers: Optional[List[Header]] (optional)

Optional headers. Defaults to None.

content: Optional[Content] (optional)

Optional content, defaults to None. Defaults to None.

Returns

HttpSessionInstance: A context instance yielding the response and body

class HttpUnboundSession

Summary

Initialise an HTTP session

Description

The following makes a get request from a session:

import asyncio
from bareclient import HttpClient


async def main(url: str) -> None:
    session =  HttpUnboundSession()
    async with session.request(url, method='GET') as response:
        print(response)
        if response['status_code'] == 200 and response['more_body']:
            async for part in response['body']:
                print(part)

asyncio.run(main('https://docs.python.org/3/library/cgi.html'))
bareclient.HttpUnboundSession(
*,
headers: Optional[List[Header]],
cookies: Optional[Mapping[bytes, List[Cookie]]],
loop: Optional[AbstractEventLoop],
h11_bufsiz: int,
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
decompressors: Optional[Mapping[bytes, Type[Decompressor]]],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int]
) -> None

Parameters

headers: Optional[List[Header]] (optional)

The headers. Defaults to None.

cookies: Optional[Mapping[bytes, List[Cookie]]] (optional)

The cookies. Defaults to None.

loop: Optional[AbstractEventLoop] (optional)

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

decompressors: Optional[Mapping[bytes, Type[Decompressor]]] (optional)

The decompressors. Defaults to None.

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 SSLContext options. Defaults to DEFAULT_OPTIONS.

method HttpUnboundSession.request

Summary

Make an HTTP request

HttpUnboundSession.request(
url: str,
*,
method: str,
headers: Optional[List[Header]],
content: Optional[Content]
) -> HttpSessionInstance

Parameters

url: str

The url

method: str (optional)

The HTTP method, defaults to 'GET'. Defaults to 'GET'.

headers: Optional[List[Header]] (optional)

Optional headers. Defaults to None.

content: Optional[Content] (optional)

Optional content, defaults to None. Defaults to None.

Returns

HttpSessionInstance: A context instance yielding the response and body

module bareclient.helpers

Summary

Helpers

async function bareclient.get

Summary

Issues a GET request

async bareclient.get(
url: str,
*,
headers: Headers,
loop: Optional[AbstractEventLoop],
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
decompressors: Optional[Mapping[bytes, Type[Decompressor]]],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
connect_timeout: Optional[Union[int, float]]
) -> bytes

Parameters

url: str

The url

headers: Headers (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.

decompressors: Optional[Mapping[bytes, Type[Decompressor]]] (optional)

The decompressors. Defaults to None.

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.

Returns

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: Headers,
loads: Callable[[str], Any],
loop: Optional[AbstractEventLoop],
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
decompressors: Optional[Mapping[bytes, Type[Decompressor]]],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
connect_timeout: Optional[Union[int, float]]
) -> Any

Parameters

url: str

The url

headers: Headers (optional)

Any extra headers required. Defaults to None.

loads: Callable[[str], 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.

decompressors: Optional[Mapping[bytes, Type[Decompressor]]] (optional)

The decompressors. Defaults to None.

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.

Returns

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: Headers,
loop: Optional[AbstractEventLoop],
encoding: str,
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
decompressors: Optional[Mapping[bytes, Type[Decompressor]]],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
connect_timeout: Optional[Union[int, float]]
) -> str

Parameters

url: str

The url

headers: Headers (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.

decompressors: Optional[Mapping[bytes, Type[Decompressor]]] (optional)

The decompressors. Defaults to None.

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.

Returns

str: [description]

async function bareclient.post

Summary

Issues a POST request

async bareclient.post(
url: str,
content: bytes,
*,
headers: Headers,
loop: Optional[AbstractEventLoop],
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
decompressors: Optional[Mapping[bytes, Type[Decompressor]]],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
chunk_size: int,
connect_timeout: Optional[Union[int, float]]
) -> bytes

Parameters

url: str

The url

content: bytes

The body content

headers: Headers (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.

decompressors: Optional[Mapping[bytes, Type[Decompressor]]] (optional)

The decompressors. Defaults to None.

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.

Returns

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[[str], Any],
dumps: Callable[[Any], str],
headers: Headers,
loop: Optional[AbstractEventLoop],
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
decompressors: Optional[Mapping[bytes, Type[Decompressor]]],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
chunk_size: int,
connect_timeout: Optional[Union[int, float]]
) -> Optional[Any]

Parameters

url: str

The url

obj: Any

The JSON payload

loads: Callable[[str], 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: Headers (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.

decompressors: Optional[Mapping[bytes, Type[Decompressor]]] (optional)

The decompressors. Defaults to None.

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.

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: Headers,
loop: Optional[AbstractEventLoop],
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
decompressors: Optional[Mapping[bytes, Type[Decompressor]]],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
chunk_size: int,
connect_timeout: Optional[Union[int, float]]
) -> bytes

Parameters

url: str

The url

text: str

encoding: Any (optional)

headers: Headers (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.

decompressors: Optional[Mapping[bytes, Type[Decompressor]]] (optional)

The decompressors. Defaults to None.

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.

Returns

bytes: The response body

async function bareclient.request

Summary

Gets bytes from a url.

Description

buf = await request(
    'https://jsonplaceholder.typicode.com/todos/1',
    'GET',
    ssl=ssl.SSLContext()
)
async bareclient.request(
url: str,
method: str,
*,
headers: Headers,
content: Optional[bytes],
loop: Optional[AbstractEventLoop],
cafile: Optional[str],
capath: Optional[str],
cadata: Optional[str],
ssl_context: Optional[SSLContext],
decompressors: Optional[Mapping[bytes, Type[Decompressor]]],
protocols: Iterable[str],
ciphers: Iterable[str],
options: Iterable[int],
chunk_size: int,
connect_timeout: Optional[Union[int, float]]
) -> bytes

Parameters

url: str

The url to get.

method: str

The HTTP method (eg. 'GET', 'POST', etc).

headers: Headers (optional)

Any extra headers required. Defaults to None.

content: Optional[bytes] (optional)

The content to send.. 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.

decompressors: Optional[Mapping[bytes, Type[Decompressor]]] (optional)

The decompressors. Defaults to None.

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.

Returns

bytes: The bytes received.