A library for making simple REST-like HTTP requests
Project description
httprest
httprest
is a minimalistic framework for communicating with REST APIs.
Its goal is to reduce a boilerplate code required to communicate with APIs.
- It has the
http
package exposing an HTTP client interface. There is also a client implementation that usesurllib
under the hood. So no need to use extra libraries likerequests
- It has the
api.API
class which may be overridden and used to make API calls
Usage
from httprest import API
class MyAPI(API):
def operation(self):
result = self._request("POST", "/operation/endpoint", json={...})
if result.ok:
print(result.json)
api = MyAPI("http://api.com")
api.operation()
Installation
pip install httprest
HTTP client
The library exposes an HTTPClient
interface and provides two implementations for it:
http.urllib_client.UrllibHTTPClient
: the default implementation, uses theurllib
library under the hoodhttp.requests_client.RequestsHTTPClient
: uses therequests
library under the hood
Custom HTTP client
from httprest.http import HTTPClient, HTTPResponse
class MyHTTPClient(HTTPClient):
def _request(...) -> HTTPResponse
And then you simply use it in the API client:
api = MyAPI(..., http_client=MyHTTPClient())
Fake client
The library provides the http.fake_client
module containing FakeHTTPClient
class.
That class may be used for API testing. Example:
from httprest.http.fake_client import FakeHTTPClient, HTTPResponse
http_client = FakeHTTPClient(responses=[HTTPResponse(200, b"", headers={})])
api = MyAPI(..., http_client=http_client)
api.operation()
# assert your expectations here
assert http_client.history == [...]
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
httprest-0.3.0.tar.gz
(6.9 kB
view details)
Built Distribution
File details
Details for the file httprest-0.3.0.tar.gz
.
File metadata
- Download URL: httprest-0.3.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.9 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d0031c63958ac5a7a78a438cffa2f5d415fa144904be0c9e36817759006d656 |
|
MD5 | 7cd49fac82382a528cb0d483407dcf38 |
|
BLAKE2b-256 | 07feb6e9aab8a194ba52fc7bb008bbe19a14762214a7ff260ff7ac1f237ccb36 |
File details
Details for the file httprest-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: httprest-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.9 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18798a576135bf928159c6f1233edb96d1f03f33648de3c725d1b7c754ff399f |
|
MD5 | f6e8a1e3635255d4579b0b85155e4477 |
|
BLAKE2b-256 | 777a210cf0e29347d5b64bd1416553c1afa43c817edd29aa82d2c0c4b81631c0 |