Skip to main content

No project description provided

Project description

Presto! Requests

An object-oriented REST API client & requests extesion library.

Installation

pip install presto-requests
poetry add presto-requests

Concept:

Presto! Requests is a library that extends the functionality of the requests library. It provides a simple way to create a REST API client that is object-oriented and easy to use.

Example:

from pprint import pprint
from presto import Presto

presto = Presto("https://api.github.com")

user = presto.users.sitbon()  # == presto.users["sitbon"]()

print(f"User {user.attr.login} has {user.attr.public_repos} public repositories.")

pprint(user.json())
User sitbon has 15 public repositories.
{'avatar_url': 'https://avatars.githubusercontent.com/u/1381063?v=4',
 'bio': None,
 'blog': '',
 'company': None,
 'created_at': '2012-01-26T04:25:21Z',
 'email': None,
 'events_url': 'https://api.github.com/users/sitbon/events{/privacy}',
 'followers': 7,
 'followers_url': 'https://api.github.com/users/sitbon/followers',
 'following': 13,
 'following_url': 'https://api.github.com/users/sitbon/following{/other_user}',
 'gists_url': 'https://api.github.com/users/sitbon/gists{/gist_id}',
 'gravatar_id': '',
 'hireable': None,
 'html_url': 'https://github.com/sitbon',
 'id': 1381063,
 'location': 'Portland, OR, USA',
 'login': 'sitbon',
 'name': 'Phillip Sitbon',
 'node_id': 'MDQ6VXNlcjEzODEwNjM=',
 'organizations_url': 'https://api.github.com/users/sitbon/orgs',
 'public_gists': 4,
 'public_repos': 15,
 'received_events_url': 'https://api.github.com/users/sitbon/received_events',
 'repos_url': 'https://api.github.com/users/sitbon/repos',
 'site_admin': False,
 'starred_url': 'https://api.github.com/users/sitbon/starred{/owner}{/repo}',
 'subscriptions_url': 'https://api.github.com/users/sitbon/subscriptions',
 'twitter_username': None,
 'type': 'User',
 'updated_at': '2022-11-22T00:41:18Z',
 'url': 'https://api.github.com/users/sitbon'}

Usage:

Each dot in the path of the request is a new request object.

Calling the object without any arguments will execute the request and return the response object.

Indexing the object like a list is a convient way to extend the path to a new object for things like id paths, e.g. presto.note[1]().

Specifying keyword arguments will add them to the request as keyword arguments to requests.request(), and then return the current object for further chaining.

There are a few special top-level attributes that can be used to modify the request: get, post, put, patch, delete, head, options, and finally request which is an empty path component that can be used to indirectly modify existing top-level auto created request objects.

All of these top-level attributes are able to clone existing request attributes, to modify the path and parent parameters while using the same component path and parameters.

For example:

from presto import Presto

presto = Presto("http://127.0.0.1:8000", APPEND_SLASH=True)

api = presto.api

print("api:", api)
print("presto.request.api:", presto.request.api)

api(headers={"X-User": "Testing"})(allow_redirects=False)

print("api(...):", api)

resp = api.note[4]()

print("req headers:", resp.request.headers)
print("resp:", resp)
print("note:", resp.attr)
api: Presto(url='http://127.0.0.1:8000/', params=adict(method='GET', headers=adict(Accept='application/json'))).Request(path='/api/')
presto.request.api: Presto(url='http://127.0.0.1:8000/', params=adict(method='GET', headers=adict(Accept='application/json'))).Request(path='/').Request(path='/api/')
api(...): Presto(url='http://127.0.0.1:8000/', params=adict(method='GET', headers=adict(Accept='application/json'))).Request(path='/api/', params=adict(headers={'X-User': 'Testing'}, allow_redirects=False))
req headers: {'User-Agent': 'python-requests/2.28.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/json', 'Connection': 'keep-alive', 'X-User': 'Testing'}
resp: <Response [200]>
note: adict(id=4, url='http://127.0.0.1:8000/api/note/4/', time='2022-12-02T19:26:09-0800', note='Hello from the API!!', collection='http://127.0.0.1:8000/api/coll/3/')

response.attr is an adict instance, which is a dictionary that can be accessed as attributes. It contains the JSON-decoded content of a response, if any.

APPEND_SLASH is meant to be client implementation-specific, e.g. for a Django Rest Framework client, one would typically set Presto.APPEND_SLASH = True or inherit from Presto in a pre-defined API client class.

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

presto_requests-0.3.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

presto_requests-0.3.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file presto_requests-0.3.0.tar.gz.

File metadata

  • Download URL: presto_requests-0.3.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.6 Linux/5.15.0-53-generic

File hashes

Hashes for presto_requests-0.3.0.tar.gz
Algorithm Hash digest
SHA256 aa8bd1d99843eb4b9086f06f4e325cb4fba0814ae3e97eaf1de66b9585106369
MD5 d09f85552f80933a00dc7d11aa6f7a72
BLAKE2b-256 6b1cb88e6b1e91e61a71a60c00a6d8b8e39c28f6c724379310f1a8c2ed477133

See more details on using hashes here.

File details

Details for the file presto_requests-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: presto_requests-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.6 Linux/5.15.0-53-generic

File hashes

Hashes for presto_requests-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1deff4d96a0d7b0f1af7321faabfba6e1d23730f7fdcd9a0c0bc8e1be36701f9
MD5 1e23d122967c9c734ec5c2fcbd78724a
BLAKE2b-256 135120f2712b83b2847ed5ad39a7abca94b6adc756ec6aa9db150c3d6e8e7f68

See more details on using hashes here.

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