# RESTEasy
REST API calls made easier
[![PyPI version](https://img.shields.io/pypi/v/resteasy.svg)](https://pypi.org/project/resteasy)
[![Python versions](https://img.shields.io/pypi/pyversions/resteasy.svg)](https://pypi.org/project/resteasy)
[![Build status](https://travis-ci.org/sayanarijit/resteasy.svg?branch=master)](https://travis-ci.org/sayanarijit/resteasy)
[![Code coverage](https://codecov.io/gh/sayanarijit/resteasy/branch/master/graph/badge.svg)](https://codecov.io/gh/sayanarijit/resteasy)
- [RESTEasy](#resteasy)
- [Installation](#installation)
- [Usage and examples](#usage-and-examples)
- [Import](#import)
- [Example 1: GitHub Jobs](#example-1-github-jobs)
- [Example 2: All methods: GET, POST, PUT, PATCH, DELETE](#example-2-all-methods-get-post-put-patch-delete)
- [Example 3: Chuck Norris jokes](#example-3-chuck-norris-jokes)
- [Example 4: Using custom decoder: Parsing MyAnimeList HTML content](#example-4-using-custom-decoder-parsing-myanimelist-html-content)
- [Debugging](#debugging)
- [Exceptions](#exceptions)
## Installation
```bash
pip install resteasy
```
## Usage and examples
### Import
```python
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
```python
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
```python
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
```python
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
```python
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(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***
```python
api.debug = True
```
Once debugging is set to 'True', Every HTTP call will return debug information instead of doing the actual request
```python
>>> 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.
Raw data
{
"_id": null,
"home_page": "https://github.com/rapidstack/RESTEasy",
"name": "RESTEasy",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "REST API client",
"author": "Arijit Basu",
"author_email": "sayanarijit@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/af/a4/af46708b1d1d6e98e0f5384b3edb3da0da890ec17c680b793185e7c93798/resteasy-3.1.2.tar.gz",
"platform": null,
"description": "# RESTEasy\n\nREST API calls made easier\n\n[![PyPI version](https://img.shields.io/pypi/v/resteasy.svg)](https://pypi.org/project/resteasy)\n[![Python versions](https://img.shields.io/pypi/pyversions/resteasy.svg)](https://pypi.org/project/resteasy)\n[![Build status](https://travis-ci.org/sayanarijit/resteasy.svg?branch=master)](https://travis-ci.org/sayanarijit/resteasy)\n[![Code coverage](https://codecov.io/gh/sayanarijit/resteasy/branch/master/graph/badge.svg)](https://codecov.io/gh/sayanarijit/resteasy)\n\n- [RESTEasy](#resteasy)\n - [Installation](#installation)\n - [Usage and examples](#usage-and-examples)\n - [Import](#import)\n - [Example 1: GitHub Jobs](#example-1-github-jobs)\n - [Example 2: All methods: GET, POST, PUT, PATCH, DELETE](#example-2-all-methods-get-post-put-patch-delete)\n - [Example 3: Chuck Norris jokes](#example-3-chuck-norris-jokes)\n - [Example 4: Using custom decoder: Parsing MyAnimeList HTML content](#example-4-using-custom-decoder-parsing-myanimelist-html-content)\n - [Debugging](#debugging)\n - [Exceptions](#exceptions)\n\n## Installation\n\n```bash\npip install resteasy\n```\n\n## Usage and examples\n\n### Import\n\n```python\nfrom resteasy import RESTEasy, json\n\napi = RESTEasy(endpoint='https://api.example.com',\n auth=('user', '****'),\n verify=False, cert=None, timeout=None,\n allow_redirects=True,\n encoder=json.dumps, decoder=json.loads, debug=False)\n \n# optional timeout\napi.timeout = 60\n```\n\n### Example 1: GitHub Jobs\n\n```python\napi = RESTEasy(endpoint='https://jobs.github.com')\n\npositions = api.route('positions.json')\n\npositions.get(description='python', full_time=1)\n# or\npositions.do('GET', {'description': 'python', 'full_time': 1})\n\n# GET https://jobs.github.com/positions.json?description=python&full_time=1\n```\n\n### Example 2: All methods: GET, POST, PUT, PATCH, DELETE\n\n```python\nfrom resteasy import RESTEasy\n\napi = RESTEasy(endpoint='https://jsonplaceholder.typicode.com')\n\nposts = api.route('posts')\n\n### GET (fetch resources)\nposts.get()\nposts.get(userId=1)\nposts.route(1).get()\n\n### POST (create a resource)\nposts.post(title='foo', body='bar', userId=1)\n\n### PUT & PATCH (update a resource)\nposts.route(1).put(id=1, title='foo', body='bar', userId=1)\nposts.route(1).patch(title='foo')\n\n### DELETE (delete a resource)\nposts.route(1).delete()\n```\n\n### Example 3: Chuck Norris jokes\n\n```python\nfrom __future__ import print_function\nfrom resteasy import RESTEasy\n\napi = RESTEasy(endpoint='https://api.chucknorris.io')\n\n\n### Print a random joke\njokes = api.route('jokes')\nrandom = jokes.route('random')\nprint(random.get())\n\n# GET https://api.chucknorris.io/jokes/random\n\n\n### Get all categories\ncategories = jokes.route('categories').get()\nprint(categories)\n\n# GET https://api.chucknorris.io/jokes/categories\n\n\n### Print a random joke from each category\nfor category in categories:\n random_joke = random.get(category=category)\n print(category, ':', random_joke['value'])\n\n # GET https://api.chucknorris.io/jokes/random?category=<category>\n```\n\n### Example 4: Using custom decoder: Parsing MyAnimeList HTML content\n\n```python\nfrom resteasy import RESTEasy\nfrom html.parser import HTMLParser\n\nclass MyHTMLParser(HTMLParser):\n '''Custom HTML parser'''\n \n def handle_starttag(self, tag, attrs):\n '''Overriding abstract method'''\n if tag == 'title' and not self.found:\n self.found = True\n\n def handle_data(self, data):\n '''Overriding abstract method'''\n if self.found and self.anime is None:\n self.anime = data\n \n def parse(self, content):\n '''Parse content and return object'''\n self.found = False\n self.anime = None\n self.feed(content)\n title = self.anime.strip().replace(' - MyAnimeList.net', '') if self.found else None\n return dict(title=title)\n\nparser = MyHTMLParser()\n\napi = RESTEasy(endpoint='https://myanimelist.net', decoder=parser.parse)\n\n### One way\napi.route('anime/1').get()\n\n### Another way\napi.route('anime', 1).get()\n\n### Yet another way\napi.route('anime').route(1).get()\n\n### This is the last way I swear\napi.route('anime').route(1).do(api.GET)\n\n### Just kidding...\napi.route('anime').route(1).request(api.GET).json()\n\n# GET https://myanimelist.net/anime/1\n```\n\n## Debugging\n\nTo enable debugging just pass or set ***debug=True***\n\n```python\napi.debug = True\n```\n\nOnce debugging is set to 'True', Every HTTP call will return debug information instead of doing the actual request\n\n```python\n>>> posts.debug = True\n>>> posts.get(userId=1)\n{'endpoint': 'https://jsonplaceholder.typicode.com/posts',\n 'params': {'userId': 1},\n 'method': 'GET',\n 'timeout': None}\n```\n\n## Exceptions\n\n* As this package uses requests module to perform HTTP calls, so all exceptions will be raised by requests module itself.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "REST API calls made easier",
"version": "3.1.2",
"project_urls": {
"Download": "https://github.com/rapidstack/RESTEasy/archive/v3.1.2.tar.gz",
"Homepage": "https://github.com/rapidstack/RESTEasy"
},
"split_keywords": [
"rest",
"api",
"client"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2d17ca27c5783c0a7388af5ddc76439cdd887d4ac4c3cf2f4d4894b818101410",
"md5": "d0a2919c04a855861fdf9f9ef3a44d1d",
"sha256": "3298a7de9d041045a630c37379c51beb460995161370c778653789238d7a6840"
},
"downloads": -1,
"filename": "RESTEasy-3.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d0a2919c04a855861fdf9f9ef3a44d1d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5392,
"upload_time": "2024-06-22T04:32:59",
"upload_time_iso_8601": "2024-06-22T04:32:59.242001Z",
"url": "https://files.pythonhosted.org/packages/2d/17/ca27c5783c0a7388af5ddc76439cdd887d4ac4c3cf2f4d4894b818101410/RESTEasy-3.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "afa4af46708b1d1d6e98e0f5384b3edb3da0da890ec17c680b793185e7c93798",
"md5": "427b8e319004deebc4d7cf11c3a96717",
"sha256": "18cf0dffa9e190b02639440a0eeacd1a7ef6c58efde0d7edf762e2ab2476596f"
},
"downloads": -1,
"filename": "resteasy-3.1.2.tar.gz",
"has_sig": false,
"md5_digest": "427b8e319004deebc4d7cf11c3a96717",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5577,
"upload_time": "2024-06-22T04:33:01",
"upload_time_iso_8601": "2024-06-22T04:33:01.496232Z",
"url": "https://files.pythonhosted.org/packages/af/a4/af46708b1d1d6e98e0f5384b3edb3da0da890ec17c680b793185e7c93798/resteasy-3.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-22 04:33:01",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "rapidstack",
"github_project": "RESTEasy",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "resteasy"
}