TL;DR
--Qiita's API wrapper library "qiipy" has been created. --Unfinished --You can easily develop an API wrapper library with your own library "apywrapper". --____ [Important] We are looking for contributors! !! !! !! Those who want to contribute to OSS [Important] __ (I want you to cooperate at the end of the article)
The other day, I made my own library called apywrapper that can easily develop API wrappers, so I wanted to test its power.
The code I actually wrote is ~~~~
First, define the model like this ...
from typing import Optional
from dataclasses import dataclass
@dataclass
class User:
    id: str
    permanent_id: int
    name: str
    profile_image_url: str
    team_only: bool
    followees_count: int
    followers_count: int
    items_count: int
    description: Optional[str] = None
    organization: Optional[str] = None
    location: Optional[str] = None
    facebook_id: Optional[str] = None
    github_login_name: Optional[str] = None
    linkedin_id: Optional[str] = None
    twitter_screen_name: Optional[str] = None
    website_url: Optional[str] = None
@dataclass
class Comment:
    body: str
    created_at: str
    id: str
    rendered_body: str
    updated_at: str
    user: User
After that, it feels like defining an API (you don't have to worry about parameters such as queries, path variables, json, etc.)
from apywrapper import Apy, get, post, delete, patch
from ._models import Comment
class Qiipy(Apy):
    def __init__(self, access_token: str, host: str = "https://qiita.com/api/v2"):
        super().__init__(host=host, headers={"Authorization": f"Bearer {access_token}"})
    @get("/comments/{comment_id}")
    def get_comment(self, comment_id: str):
        """Get comments
        Args:
            comment_id:Comment id
        Returns:
            Comment
        """
        return Comment, {"comment_id": comment_id}
    @delete("/comments/{comment_id}")
    def delete_comment(self, comment_id: str):
        """Delete comment
        Args:
            comment_id:Comment id
        Returns:
            None
        """
        return None, {"comment_id": comment_id}
   ...
that's all! w
If you explain the code for the time being
@method("/path/to/api") #api path
def get_something(self, item_id: str):
  	return Something, {"item_id": item_id} 
  	#in this case`item_id`Becomes a query(Because there are no variables in the path)
    #The return value is`Something`object
from qiipy import Qiipy
qiita = Qiipy(access_token="xxx")
qiita.get_comment("zzzzzzzzz") #Comment object is returned
Feeling like
--Definition of entity (model) (https://github.com/sh1ma/qiipy/blob/master/qiipy/_models.py) --Bug Report (https://github.com/sh1ma/qiipy/issues) --Definition of function to hit API (https://github.com/sh1ma/qiipy/blob/master/qiipy/_api.py) --Other advice
I look forward to working with you.
Recommended Posts