Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kimmobrunfeldt/nap
Convenient way to request HTTP APIs
https://github.com/kimmobrunfeldt/nap
api http http-api python requests
Last synced: 4 months ago
JSON representation
Convenient way to request HTTP APIs
- Host: GitHub
- URL: https://github.com/kimmobrunfeldt/nap
- Owner: kimmobrunfeldt
- License: mit
- Created: 2013-12-15T21:47:36.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2022-12-26T19:43:37.000Z (about 2 years ago)
- Last Synced: 2024-04-13T03:00:22.544Z (10 months ago)
- Topics: api, http, http-api, python, requests
- Language: Python
- Homepage:
- Size: 107 KB
- Stars: 42
- Watchers: 5
- Forks: 10
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Nap
[![Build Status](https://travis-ci.org/kimmobrunfeldt/nap.png?branch=master)](https://travis-ci.org/kimmobrunfeldt/nap)
[![Coverage Status](https://coveralls.io/repos/kimmobrunfeldt/nap/badge.png?branch=master)](https://coveralls.io/r/kimmobrunfeldt/nap?branch=master)
[![Badge fury](https://badge.fury.io/py/nap.png)](https://badge.fury.io/py/nap.png)
[![Badge PyPi](https://pypip.in/d/nap/badge.png)](https://pypip.in/d/nap/badge.png)*Nap* provides convenient way to request HTTP APIs.
After coding a few HTTP API wrapper classes, I decided to code *Nap*.
It's is just a small(*~150 loc*) wrapper around [requests][].
Requests is a superb HTTP library, which supports everything you'll need to get things done in today's web.**Example**
```python
from nap.url import Url
api = Url('https://api.github.com/')
# GET https://api.github.com/users
api.get('users')users = api.join('users')
# GET https://api.github.com/users/kimmobrunfeldt
users.get('kimmobrunfeldt')# Another way to make the same request as above:
api.get('users/kimmobrunfeldt')
```**Get started**
* Look through [examples](#examples)
* See [API documentation](docs/nap-api.md)
* [Dive into code](nap/url.py)## Install
Python versions 2.7, 3.6, 3.7, 3.8 and PyPy are supported and tested against.
Install latest release with *pip*:
pip install nap
Install latest development version usin *pip*:
pip install git+git://github.com/kimmobrunfeldt/nap.git
Install latest development version using *setup.py*:
git clone [email protected]:kimmobrunfeldt/nap.git
cd nap
python setup.py install## Nap API documentation
See [API documentation](docs/nap-api.md)
## Examples
Find gists from GitHub.
```python
from nap.url import Url
api = Url('https://api.github.com/')# Get gists since May 1st 2014 (will be paginated)
gists = api.get('gists', params={'since': '2014-05-01T00:00:00Z'})
print(gists.json())
```You can also specify default keyword arguments to be passed on every request in *Url* initialization.
All authentications supported by *requests* are automatically supported.```python
from nap.url import Url
# Keyword arguments given to Url will be given to each request method
# by default for every request.
api = Url('https://api.github.com/', auth=('user', 'pass'))# Get authenticated user
response = api.get('user')
print(response.json())# You can also override the default keyword arguments afterwords
response = api.get('users/kimmobrunfeldt', auth=('kimmo', 'password1'))
```**A bit more complicated example.**
Automatically convert all JSON responses to Python dict objects.
Demonstrate various HTTP methods.
Also raise errors from other than `200 OK` responses.```python
from nap.url import Url
import requestsclass JsonApi(Url):
def after_request(self, response):
if response.status_code != 200:
response.raise_for_status()return response.json()
# Use https://github.com/kennethreitz/httpbin for testing
api = JsonApi('http://httpbin.org/')# response is dict object containing parsed JSON
response = api.post('post', data={'test': 'Test POST'})
print(response)response = api.put('put', data={'test': 'Test PUT'})
print(response)try:
# httpbin will response with `Method Not Allowed` if we try to do
# POST http://httpbin.org/get
api.post('get')
except requests.exceptions.HTTPError as e:
print('Response was not OK, it was: %s' % e.response.status_code)
```## Contributing
[Documentation for Nap developers](docs/)
[requests]: http://docs.python-requests.org/en/latest/ "Requests"