利用元编程批量生成方法调用 HTTP 接口
概述
一个项目通常会有一定数量的 http 接口,这些接口按功能模块进行分类,每个模块中的接口有相似点.
例如 github 部分接口如下:
所有数据格式均为 json.
-
查看用户信息
GET
https://api.github.com/users/用户名
-
查看用户下仓库列表
GET
https://api.github.com/repos/用户名/仓库名
-
获取某个 repo 的内容列表
GET
https://api.github.com/repos/用户名/仓库名/contents
-
repo 中所有的 commits 列表
GET
https://api.github.com/repos/用户名/仓库名/commits
-
某一条 commit 详情
GET
https://api.github.com/repos/用户名/仓库名/commits/某一条commit的SHA
-
issues 列表
GET
https://api.github.com/repos/用户名/仓库名/issues
-
某条 issue 详情
GET
https://api.github.com/repos/用户名/仓库名/issues/序号
issues 都是以 1,2,3 这样的序列排号的,可以加?state=状态
参数过滤状态. -
创建/修改文件
PUT
https://api.github.com/repos/用户名/仓库名/contents/文件路径
{ "message": "commit message", "content": "bXkgbmV3IGZpbGUerdf9udGVudHM=" }
-
... ...
如果需要多次调用这些接口通常需要为每个接口封装一个方法,这种方法工作量大,而且扩展性差.
本文将要实现的功能如下:
-
统一管理一个 api 配置文件
格式大致如下:
base_url = "https://api.github.com" # github api api = { "users": { "get_user_info": "{user_name}" # 获取用户信息 }, "repos": { "get_repo": "{user_name}/{repo_name}", # 获取仓库信息 "get_contents": "{user_name}/{repo_name}/contents", # 获取仓库内容 "set_file": "{user_name}/{repo_name}/contents/{path}", # 添加/修改文件 "delete_file": "{user_name}/{repo_name}/contents/{path}", # 删除文件 "get_contents_in_dir": "{user_name}/{repo_name}/contents/{dir_name}", # 获取文件夹中文件列表 "get_commits": "{user_name}/{repo_name}/commits", # 获取提交列表 "get_commit": "{user_name}/{repo_name}/commits/{commit_RSA}", # 获取一次提交的详细信息 "get_issuess": "{user_name}/{repo_name}/issues?state={state:open}", # 获取issuess列表 "get_issue": "{user_name}/{repo_name}/{issue_NO}", # 获取一个issue详情 } }
-
通过 meta_class 自动生成方法
-
调用
from api.api_class import API handler = API(token='***') user_info = handler.get_user_info('kailunfan') repo_info = handler.get_repo('kailunfan', 'githubApiTest') # 需要传入payload时用data接收 data = { "message": "new file", "content": base64.b64encode("this is the original content!".encode()).decode() } file_info = handler.set_file("kailunfan", "githubApiTest", "src/test_set_file2.txt", data=data)
完整示例
大致流程
- 定义一个API类,作为调用方法的handler;
- 为API类指定一个meta_class;
- 将配置文件内容定义为API类属性;
- 在meta_class中读取配置文件,生成HTTP调用的method,url,header以及payload,封装为function,作为属性赋给API;
- 实例化API调用方法.