Skip to main content

Python 3.7 Lint and Test

Abstract Http Client

This project is a starting template for quickly implementing a python REST api client. A concrete base class encapsulating the popular requests library is provided. Abstract base class building blocks for integrating other http libraries (such as aiohttp, etc.) are also available. The main advantage to using this base class as a starting point is to save on common boilerplate actions, which are highlighted below.

Basic Features

  • Setup of session object and common general attributes
  • helper methods to send requests with session
  • Validation and debug logging of all requests sent
  • internal counter of requests sent
  • Context manager to manage session tokens (login / logout methods must be implemented)
  • Composable client and service classes

Installation

pip install abstract-http-client

Basic Usage

  • Sample implementation of api client that has no auth
from abstract_http_client.http_clients.requests_client import RequestsClient
import json

class JsonPlaceholderApiClient(RequestsClient):
  def __init__(self, host):
    super().__init__(host=host, use_https=True)

  def get_users(self):
    return self.rest_service.request_get(uri="/users").json()

  def get_posts(self):
    return self.rest_service.request_get("/posts").json()

  def add_post(self):
    return self.rest_service.request_post("/posts", data={"post": "my_post"})


if __name__ == "__main__":
  api = JsonPlaceholderApiClient(host="jsonplaceholder.typicode.com")
  users = api.get_users()
  print(json.dumps(users[:2], indent=4))
  print(f"total requests sent {api.rest_service.request_counter}")

Auth Client Sample

  • Example that requires login and authorization header on every request
  • Implement a login method - here it stores header on session object
  • Base Class context managers call Logout on exit by default
  • Login not in enter by default, can be added if prefer to have context manager trigger login. Or just put into init
from abstract_http_client.http_clients.requests_client import RequestsClient


class SampleAuthClient(RequestsClient):
  def __init__(self, host, user, password):
    super().__init__(host=host, user=user, password=password)
    self.login()

  def login(self):
    """  sample login - getting token and storing on requests session object """
    data = {"user": self.user, "password": self.password}
    self.token = self.rest_service.request_put(uri="/login", json=data)
    self.rest_service.session.headers.update({"Authorization": self.token})

  def logout(self):
    """  sample logout - invalidating token and clearing session auth header """
    self.rest_service.request_delete(uri=f"/logout/{self.token}")
    self.rest_service.session.headers.pop({"Authorization": self.token})

  def get_stuff(self) -> dict:
    """ NOTE: this is pseudocode, not real endpoint """
    return self.rest_service.request_get("/stuff").json()


if __name__ == "__main__":
  # Context manager will handle api logout
  with SampleAuthClient(host="192.168.1.3", user="admin", password="admin") as api:
    
    # call your api here 
    stuff = api.get_stuff()
    # Do more stuff

Metadata

Release files for abstract-http-client 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for abstract-http-client 1.0.3
File Size Uploaded
abstract-http-client-1.0.3.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for abstract-http-client 1.0.3
File Interpreter ABI Platform
abstract_http_client-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 15.1 kB

Release files / abstract-http-client-1.0.3.tar.gz

Download URL abstract-http-client-1.0.3.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8054d49cd53ab506cfc4ba16063ffb1e0b8d9a5d6b75c3a4dfaaee6353882e3d
BLAKE2b-256 checksum
How to use checksums
9242a930490c53b2fe2ec5148cf0166d8ab0af32fb3e27ec113c7bd6711c697d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release files / abstract_http_client-1.0.3-py3-none-any.whl

Download URL abstract_http_client-1.0.3-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
12d610139d091251f907ad68188884c3bb34091459436ef7d79e5d0e41560b34
BLAKE2b-256 checksum
How to use checksums
c991a50926922a1c38232441ab202ceb7cc7dc2a34ed55179ee723f78c74ce1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page