Skip to main content

REST API calls made easier

Project description

RESTEasy

REST API calls made easier

PyPI version Python versions Build status Code coverage

Installation

pip install resteasy

Usage and examples

Import

from resteasy import RESTEasy, json

api = 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 timeout
api.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)
# or
positions.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

from resteasy import RESTEasy

api = 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__ import print_function
from resteasy import RESTEasy

api = RESTEasy(endpoint='https://api.chucknorris.io')


### Print a random joke
jokes = api.route('jokes')
random = jokes.route('random')
print(random.get())

# GET https://api.chucknorris.io/jokes/random


### Get all categories
categories = jokes.route('categories').get()
print(categories)

# GET https://api.chucknorris.io/jokes/categories


### Print a random joke from each category
for category in categories:
    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

from resteasy import RESTEasy
from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    '''Custom HTML parser'''

    def handle_starttag(self, tag, attrs):
        '''Overriding abstract method'''
        if tag == 'title' and not self.found:
            self.found = True

    def handle_data(self, data):
        '''Overriding abstract method'''
        if self.found and self.anime is None:
            self.anime = data

    def parse(self, content):
        '''Parse content and return object'''
        self.found = False
        self.anime = None
        self.feed(content)
        title = self.anime.strip().replace(' - MyAnimeList.net', '') if self.found else None
        return dict(title=title)

parser = MyHTMLParser()

api = RESTEasy(endpoint='https://myanimelist.net', decoder=parser.parse)

### One way
api.route('anime/1').get()

### Another way
api.route('anime', 1).get()

### Yet another way
api.route('anime').route(1).get()

### This is the last way I swear
api.route('anime').route(1).do('GET')

### Just kidding...
api.route('anime').route(1).request('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.

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

RESTEasy-3.1.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

RESTEasy-3.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file RESTEasy-3.1.0.tar.gz.

File metadata

  • Download URL: RESTEasy-3.1.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6

File hashes

Hashes for RESTEasy-3.1.0.tar.gz
Algorithm Hash digest
SHA256 c39d2c617fe5f34bb903d8b24649379901977512e1bb85bec841986af65e8001
MD5 2ff181ceddc1b668d7ac59d8c92c5803
BLAKE2b-256 b1cb9805313fbe7c2df784b61c728a462beba0b91f575a1668a0db60893d3333

See more details on using hashes here.

File details

Details for the file RESTEasy-3.1.0-py3-none-any.whl.

File metadata

  • Download URL: RESTEasy-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6

File hashes

Hashes for RESTEasy-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c39c9f149504867c3ece38bbf38840c1ed0f2d55022d4266085f13ec69eef55
MD5 5d1835fb7cde2b706323fd147175a09d
BLAKE2b-256 531795af49f70d09e7166b7d3e9568eac272be301d83967307a552d0b94e6525

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