Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

A lightweight asyncio HTTP client

Project description

bareClient

A simple asyncio http Python client package supporting HTTP versions 1.0, 1.1 and 2 (read the docs).

This is the client companion to the ASGI server side web framework bareASGI and follows the same "bare" approach. It makes little attempt to provide any helpful features which might do unnecessary work, providing a foundation for whatever feature set is required.

It was written to allow a web server which had negotiated the HTTP/2 protocol for make outgoing HTTP/2 calls. This increases performance and simplifies proxy configuration in a micro-service architecture.

Installation

The package can be installed with pip.

pip install bareclient

This is a Python3.7 and later package.

It has dependencies on:

Usage

The basic usage is to create an HttpClient.

import asyncio
from typing import List, Optional
from bareclient import HttpClient

async def main(url: str) -> None:
    async with HttpClient(url, method='GET') as 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'))

There is also an HttpSession for maintaining session cookies.

"""Simple GET"""

import asyncio
import json

from bareutils import text_reader
import bareutils.header as header
import bareutils.response_code as response_code
from bareclient import HttpSession


async def main() -> None:
    """Session example"""

    session = HttpSession('https://jsonplaceholder.typicode.com')
    async with session.request('/users/1/posts', method='GET') as response:
        # We expect a session cookie to be sent on the initial request.
        set_cookie = header.find(b'set-cookie', response['headers'])
        print("Session cookie!" if set_cookie else "No session cookie")

        if not response_code.is_successful(response['status_code']):
            raise Exception("Failed to get posts")

        posts = json.loads(await text_reader(response['body']))
        print(f'We received {len(posts)} posts')

        for post in posts:
            path = f'/posts/{post["id"]}/comments'
            print(f'Requesting comments from "{path}""')
            async with session.request(path, method='GET') as response:
                # As we were sent the session cookie we do not expect to receive
                # another one, until this one has expired.
                set_cookie = header.find(b'set-cookie', response['headers'])
                print("Session cookie!" if set_cookie else "No session cookie")

                if not response_code.is_successful(response['status_code']):
                    raise Exception("Failed to get comments")

                comments = json.loads(await text_reader(response['body']))
                print(f'We received {len(comments)} comments')


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)


asyncio.run(main('https://jsonplaceholder.typicode.com/todos/1'))

Project details


Download files

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

Files for bareclient, version 4.2.0
Filename, size File type Python version Upload date Hashes
Filename, size bareclient-4.2.0-py3-none-any.whl (27.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size bareclient-4.2.0.tar.gz (21.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page