使REST API调用变得更简单
项目描述
RESTEasy
使REST API调用变得更简单
安装
pip install resteasy
使用示例
导入
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
示例 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
示例 2:所有方法: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()
示例 3:Chuck Norris笑话
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>
示例 4:使用自定义解码器:解析MyAnimeList HTML内容
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
调试
要启用调试,只需传递或设置 debug=True
api.debug = True
一旦将调试设置为 'True',每次HTTP调用将返回调试信息,而不是执行实际请求
>>> posts.debug = True
>>> posts.get(userId=1)
{'endpoint': 'https://jsonplaceholder.typicode.com/posts',
'params': {'userId': 1},
'method': 'GET',
'timeout': None}
异常
- 由于此包使用requests模块执行HTTP调用,因此所有异常将由requests模块本身引发。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分布
resteasy-3.1.2.tar.gz (5.6 kB 查看哈希值)
构建版本
RESTEasy-3.1.2-py3-none-any.whl (5.4 kB 查看哈希值)
关闭
resteasy-3.1.2.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 18cf0dffa9e190b02639440a0eeacd1a7ef6c58efde0d7edf762e2ab2476596f |
|
MD5 | 427b8e319004deebc4d7cf11c3a96717 |
|
BLAKE2b-256 | afa4af46708b1d1d6e98e0f5384b3edb3da0da890ec17c680b793185e7c93798 |
关闭
RESTEasy-3.1.2-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 3298a7de9d041045a630c37379c51beb460995161370c778653789238d7a6840 |
|
MD5 | d0a2919c04a855861fdf9f9ef3a44d1d |
|
BLAKE2b-256 | 2d17ca27c5783c0a7388af5ddc76439cdd887d4ac4c3cf2f4d4894b818101410 |