Skip to main content

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.

Features

The client has the following notable features:

  • Lightweight
  • Uses asyncio
  • Supports HTTP versions 1.0, 1.1, 2
  • Supports middleware

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.

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

Source Distribution

bareclient-5.0.0a1.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bareclient-5.0.0a1-py3-none-any.whl (32.5 kB view details)

Uploaded Python 3

File details

Details for the file bareclient-5.0.0a1.tar.gz.

File metadata

  • Download URL: bareclient-5.0.0a1.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.9.6 Darwin/19.6.0

File hashes

Hashes for bareclient-5.0.0a1.tar.gz
Algorithm Hash digest
SHA256 1cdadfb060bd781d83b9c83b57e5ede53195b6f7ddfa660cff4bd3a265b445d9
MD5 b749a0ac6830a59e0eb10f996aabbd32
BLAKE2b-256 6514eedb6859b6bbe0e0cd073874e40822712a6a6c7a7115d209715f1d6da0ce

See more details on using hashes here.

File details

Details for the file bareclient-5.0.0a1-py3-none-any.whl.

File metadata

  • Download URL: bareclient-5.0.0a1-py3-none-any.whl
  • Upload date:
  • Size: 32.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.9.6 Darwin/19.6.0

File hashes

Hashes for bareclient-5.0.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 4942d5c3b53142bd50bda39e6debe49cdd6b7cf7ce5864eb756b53244789c2bc
MD5 2ceb2a4e4dcf789bd636b6df473d32dc
BLAKE2b-256 4d76d7b5f463daf9031acd05686ab18747933e378f63d4a6c4754f3d523ede66

See more details on using hashes here.

Supported by

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