Skip to content

Repository files navigation

RESTEasy

REST API calls made easier

PyPI versionPython versionsBuild statusCode coverage

Installation

pip install resteasy

Usage and examples

Import

fromresteasyimportRESTEasy, jsonapi=RESTEasy(endpoint='https://api.example.com',
auth=('user', '****'),
verify=False, cert=None, timeout=None,
allow_redirects=True,
encoder=json.dumps, decoder=json.loads, debug=False)
# optional timeoutapi.timeout=60

Example 1: GitHub Jobs

api=RESTEasy(endpoint='https://jobs.github.com')
positions=api.route('positions.json')
positions.get(description='python', full_time=1)
# orpositions.do('GET', {'description': 'python', 'full_time': 1})
# GET https://jobs.github.com/positions.json?description=python&full_time=1

Example 2: All methods: GET, POST, PUT, PATCH, DELETE

fromresteasyimportRESTEasyapi=RESTEasy(endpoint='https://jsonplaceholder.typicode.com')
posts=api.route('posts')
### GET (fetch resources)posts.get()
posts.get(userId=1)
posts.route(1).get()
### POST (create a resource)posts.post(title='foo', body='bar', userId=1)
### PUT & PATCH (update a resource)posts.route(1).put(id=1, title='foo', body='bar', userId=1)
posts.route(1).patch(title='foo')
### DELETE (delete a resource)posts.route(1).delete()

Example 3: Chuck Norris jokes

from __future__ importprint_functionfromresteasyimportRESTEasyapi=RESTEasy(endpoint='https://api.chucknorris.io')
### Print a random jokejokes=api.route('jokes')
random=jokes.route('random')
print(random.get())
# GET https://api.chucknorris.io/jokes/random### Get all categoriescategories=jokes.route('categories').get()
print(categories)
# GET https://api.chucknorris.io/jokes/categories### Print a random joke from each categoryforcategoryincategories:
random_joke=random.get(category=category)
print(category, ':', random_joke['value'])
# GET https://api.chucknorris.io/jokes/random?category=<category>

Example 4: Using custom decoder: Parsing MyAnimeList HTML content

fromresteasyimportRESTEasyfromhtml.parserimportHTMLParserclassMyHTMLParser(HTMLParser):
'''Custom HTML parser'''defhandle_starttag(self, tag, attrs):
'''Overriding abstract method'''iftag=='title'andnotself.found:
self.found=Truedefhandle_data(self, data):
'''Overriding abstract method'''ifself.foundandself.animeisNone:
self.anime=datadefparse(self, content):
'''Parse content and return object'''self.found=Falseself.anime=Noneself.feed(content)
title=self.anime.strip().replace(' - MyAnimeList.net', '') ifself.foundelseNonereturndict(title=title)
parser=MyHTMLParser()
api=RESTEasy(endpoint='https://myanimelist.net', decoder=parser.parse)
### One wayapi.route('anime/1').get()
### Another wayapi.route('anime', 1).get()
### Yet another wayapi.route('anime').route(1).get()
### This is the last way I swearapi.route('anime').route(1).do(api.GET)
### Just kidding...api.route('anime').route(1).request(api.GET).json()
# GET https://myanimelist.net/anime/1

Debugging

To enable debugging just pass or set debug=True

api.debug=True

Once debugging is set to 'True', Every HTTP call will return debug information instead of doing the actual request

>>>posts.debug=True>>>posts.get(userId=1)
{'endpoint': 'https://jsonplaceholder.typicode.com/posts',
'params': {'userId': 1},
'method': 'GET',
'timeout': None}

Exceptions

  • As this package uses requests module to perform HTTP calls, so all exceptions will be raised by requests module itself.

About

REST API calls made easier

Topics

Resources

Stars

16 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages