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.
Parameters¶
url: strThe 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'))
Parameters¶
url: strThe 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
Parameters¶
path: strThe 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.
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'))
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
Parameters¶
url: strThe 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.
module bareclient.helpers ¶
Summary¶
Helpers
async function bareclient.get ¶
Summary¶
Issues a GET request
Parameters¶
url: strThe 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.
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: 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.
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: 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.
async function bareclient.post ¶
Summary¶
Issues a POST request
Parameters¶
url: strThe url
content: bytesThe 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.
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[[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.
async function bareclient.post_text ¶
Summary¶
Issues a POST request with a str body
Parameters¶
url: strThe 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.
async function bareclient.request ¶
Summary¶
Gets bytes from a url.
Description¶
buf = await request(
'https://jsonplaceholder.typicode.com/todos/1',
'GET',
ssl=ssl.SSLContext()
)
Parameters¶
url: strThe url to get.
method: strThe 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.