Library "apywrapper" to easily develop a wrapper for RESTful API

TL;DR

--Developed a library called "apywrapper"

--API wrapper can be developed at explosive speed --Supports RESTful API __ only __

I think there are many things to think about when creating an API wrapper, such as request processing and serialization of response data, but when you really want data, thinking about them and worrying about them can be a waste of time.

_ When I throw a parameter to the server, an object is returned in the response _

In fact, that's all I want to do.

I will introduce the code that realizes it with ʻapy wrapper`

Example

from dataclasses import dataclass
from apywrapper import Apy

api = Apy(host="https://example.com/api", headers={"api_token": "xxxxxx"})


@dataclass
class User:
    user_id: str
    name: str
    age: int


@api.get("/users/{user_id}")
def get_user(user_id: str):
    return User, {"user_id": user_id}

That's it! If you call get_user ("sh1ma "), you will get a user object with ʻuser_id`` sh1ma`!

Commentary

from dataclasses import dataclass
from apywrapper import Apy

This line imports the body ʻApy of data class and ʻapywrapper

api = Apy(host="https://example.com/api", headers={"api_token": "xxxxxx"})

Specify the host of the API here. headers is optional, but in most cases you will need it. (token etc.)

@dataclass
class User:
    user_id: str
    name: str
    age: int

Defines the class of objects required as a data class. This is used to serialize the response.

@api.get("/users/{user_id}")
def get_user(user_id: str):
    return User, {"user_id": user_id}

The @ api.get ("/ users / {user_id} ") part is very important. Here, specify the Path of the API. You can pass a variable to Path by enclosing it in curly braces like {user_id}.

def get_user (user_id: str): defines a function. There is nothing special about it. The argument name of the ʻuser_id` part is not actually used, so feel free to use any name you like!

return User, {"user_id ": user_id} is very, very important! Everything is packed here! Of the values to return, the first part of ʻUser will pass the object to be responded (even if a list (List [User] ) is to be returned, specify a singular, that is, ʻUser. Masu). The following {"user_id ": user_id} part is the request parameter. ʻUser_id` is a Path variable, but you can also specify queries and json data in the same way.

It's hard to understand, so here's an example.

POST data

@api.post("/users")
def create_user(username: str, user_id: str):
    return User, {"user_name": username, "user_id": user_id}

The data when POSTing is JSON, but parameters can be specified in the same way as when GET.

Get data (with query)

@api.get("/users/{user_id}")
def get_user(user_id: str, only_name: bool):
    return User, {"user_id": user_id, "only_name": True}

You can specify queries and Path variables as well.

Installation method

pip install apywrapper

Finally

――It is still under development. --Currently, it does not support POST or PUT query strings __ ――We are waiting for PR and issues! (https://github.com/sh1ma/apywrapper)

Recommended Posts

Library "apywrapper" to easily develop a wrapper for RESTful API
I made a Python wrapper library for docomo image recognition API.
Created a Python wrapper for the Qiita API
kabu Station® API-I made a Python wrapper for REST API
Qiita API Python wrapper for batch processing to grab Qiita posts
I made a library to easily read config files with Python
I created a Python library to call the LINE WORKS API
Created a library for python that can easily handle morpheme division
[For beginners] How to register a library created in Python in PyPI
A guidebook for doing IoT with MicroPython easily to the last minute
For Pandas users to practice SQL easily
A tool for easily entering Python code
I tried to make a Web API
Implemented Python wrapper for Qiita API v2
I made a library for actuarial science
I tried to explain what a Python generator is for as easily as possible.
Touch the sample v20-python-samples of the OANDA v20 REST API wrapper library for Python