Requests based HTTP/REST API client with flexible cache support
Project description
Portale is minimalistic requests based HTTP/REST API client.
Advantage over other libraries: Allows different cache timeout policy for each API
Simple example
from portale import PrefixedURLSession
session = PrefixedURLSession('https://httpbin.org/')
get_thing = session.GETRequest('anything?thing={0}', timeout=10)
thing = get_thing('snake')
get_thing_by_name = session.GETRequest('anything?thing={name}', timeout=10)
thing = get_thing_by_name(name='snake')
long_request = session.GETJSONRequest('delay/{n}', timeout=20)
result = long_request(n=2)
result = long_request(n=2) # cached response
post_req = session.POSTJSONRequest('anything/{category}/')
pathargs = ()
pathkw = {'category': 'products'}
payload = {'id': 101, 'title': 'Journal'}
resp = post_req(json=payload)
from portale import PrefixedURLSession
session = PrefixedURLSession('https://cms.example/')
list_articles = session.GETJSONRequest('articles/')
new_article = session.POSTJSONRequest('articles/')
get_article = session.GETJSONRequest('articles/{0}')
update_article = session.PATCHJSONRequest('articles/{0}')
delete_article = session.DELETEJSONRequest('articles/{0}')
list_articles()
new_article(title='Best story ever', tags=['stories', 'fiction'])
get_article(101)
update_article(101, title='The Best story ever')
delete_article(101)
Cache
timeout if not specified in Request initialization, session’s timeout is used as default timeout for all the APIs using same session.
from portale import PrefixedURLSession
session = PrefixedURLSession('https://httpbin.org/', timeout=10)
get_thing = session.GETRequest('anything?thing={0}')
long_request = session.GETJSONRequest('delay/{n}')
Busting cache
long_request.cache.bust(n=n)
Access cache metrics
print(long_request.cache.metrics)
Tests
nosetests -xv tests.py
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
portale-0.7.3.tar.gz
(2.8 kB
view details)
File details
Details for the file portale-0.7.3.tar.gz
.
File metadata
- Download URL: portale-0.7.3.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6df2a0cf2457a8173631793ea90f4669c0c84d84943bdecaae24b6b73f55a58 |
|
MD5 | 6b7fb318fc71be7efe55743cd57d02cd |
|
BLAKE2b-256 | d2d22865c66f8fa19b256c548908ad30639d9f20893a6a1db325c528ababa5df |