Skip to main content

A lightweight asyncio HTTP client

Project description

bareClient

A simple asyncio http client supporting HTTP versions 1.0, 1.1 and 2.

The docs are here.

Description

This package provides the asyncio transport for h11, and h2.

It makes little attempt to provide any helpful features which might do unnecessary work.

Installation

This is a Python 3.7 package.

pip install bareclient

Usage

The basic usage is to create an HttpClient.

import asyncio
from typing import List, Optional
from baretypes import Header

from bareclient import HttpClient


async def main(url: str, headers: Optional[List[Header]]) -> None:
    async with HttpClient(url, method='GET', headers=headers) as response:
        print(response)
        if response['status_code'] == 200 and response['more_body']:
            async for part in response['body']:
                print(part)


URL = 'https://docs.python.org/3/library/cgi.html'
HEADERS = None

asyncio.run(main(URL, HEADERS))

There is also an HttpSession for maintaining a session.

import asyncio
import logging

import bareutils.response_code as response_code
from bareclient import HttpSession

logging.basicConfig(level=logging.DEBUG)


async def main() -> None:
    session = HttpSession(
        'https://shadow.jetblack.net:9009',
        capath='/etc/ssl/certs'
    )
    headers = [
        (b'host', b'shadow.jetblack.net'),
        (b'connection', b'close')
    ]
    for path in ['/example1', '/example2', '/empty']:
        async with session.request(path, method='GET', headers=headers) as response:
            print(response)
            if not response_code.is_successful(response['status_code']):
                print("failed")
            else:
                if response['status_code'] == response_code.OK and response['more_body']:
                    async for part in response['body']:
                        print(part)


asyncio.run(main())

Finally there is a single helper function to get json.

import asyncio

from bareclient import get_json


async def main(url: str) -> None:
    """Get some JSON"""
    obj = await get_json(url, headers=[(b'accept-encoding', b'gzip')])
    print(obj)


URL = 'https://jsonplaceholder.typicode.com/todos/1'

asyncio.run(main(URL))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bareclient-4.0.0rc1.tar.gz (19.0 kB view hashes)

Uploaded Source

Built Distribution

bareclient-4.0.0rc1-py3-none-any.whl (23.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page