Flask应用示例7 - 罗列所有API

1. 概述

本文介绍如何查询flask服务所有的api。
通过swagger的方式,会自动生成接口文档,描述更详细,功能更强大。但是,对代码的侵入性太强。
所以,通过flask原生的工具,挺好。

2. 获取Api列表的几种方式

2.1. flask routes

进入到flask应用root目录,执行flask routes

$ flask routes
Endpoint                                Methods  Rule
--------------------------------------  -------  ------------------------------
geoip_service.get_ip_location           GET      /api/v1/geoips
login_log_service.query_logs            GET      /api/v1/logs/
ping                                    GET      /ping
sentry                                  GET      /sentry
static                                  GET      /
subuser_service.account_login           POST     /api/v1/subusers/login/account
subuser_service.create_a_sub_user       POST     /api/v1/subusers/
subuser_service.delete_a_sub_user       DELETE   /api/v1/subusers/
subuser_service.modify_a_sub_user       PUT      /api/v1/subusers/
subuser_service.query_sub_users         GET      /api/v1/subusers/
user_service.account_login              POST     /api/v1/users/login/account
user_service.create_a_enterprise_user   POST     /api/v1/users/enterprise
user_service.create_a_individual_user   POST     /api/v1/users/individual
user_service.create_a_individual_user   POST     /api/v1/users/
user_service.delete_a_user              DELETE   /api/v1/users/
user_service.email_login                POST     /api/v1/users/login/email
user_service.mobile_login               POST     /api/v1/users/login/mobile
user_service.modify_a_user              PUT      /api/v1/users/
user_service.query_default_permissions  GET      /api/v1/users/permissions
user_service.query_users                GET      /api/v1/users/
user_service.verify_email               POST     /api/v1/users/verify/email
user_service.verify_mobile              POST     /api/v1/users/verify/mobile

2.2. flask shell

$ flask shell
Python 3.6.9 (default, Jan 26 2021, 15:33:00) 
[GCC 8.4.0] on linux
App: passport.app [dev]
Instance: /home/shuzhang/code/passport/instance
>>> app.url_map
Map([ subuser_service.account_login>,
  user_service.verify_mobile>,
  user_service.verify_email>,
  user_service.account_login>,
  user_service.mobile_login>,
  user_service.email_login>,
  user_service.query_default_permissions>,
  user_service.create_a_individual_user>,
  user_service.create_a_enterprise_user>,
  subuser_service.create_a_sub_user>,
  subuser_service.query_sub_users>,
  geoip_service.get_ip_location>,
  user_service.create_a_individual_user>,
  user_service.query_users>,
  login_log_service.query_logs>,
  sentry>,
  ping>,
 ' (OPTIONS, DELETE) -> subuser_service.delete_a_sub_user>,
 ' (PUT, OPTIONS) -> subuser_service.modify_a_sub_user>,
 ' (OPTIONS, DELETE) -> user_service.delete_a_user>,
 ' (PUT, OPTIONS) -> user_service.modify_a_user>,
 ' (GET, HEAD, OPTIONS) -> static>])

2.3. manager command

  • 代码
@manager.command
def list_routes():
    import urllib
    from passport.app import create_app
    app = create_app()

    output = []
    for rule in app.url_map.iter_rules():
        methods = ','.join(rule.methods)
        line = urllib.parse.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, rule))
        output.append(line)

    for line in sorted(output):
        print(line)
  • 控制台执行命令
$ python manage.py test list_routes
geoip_service.get_ip_location                      HEAD,OPTIONS,GET     /api/v1/geoips
login_log_service.query_logs                       HEAD,OPTIONS,GET     /api/v1/logs/
ping                                               HEAD,OPTIONS,GET     /ping
sentry                                             HEAD,OPTIONS,GET     /sentry
static                                             HEAD,OPTIONS,GET     /
subuser_service.account_login                      POST,OPTIONS         /api/v1/subusers/login/account
subuser_service.create_a_sub_user                  POST,OPTIONS         /api/v1/subusers/
subuser_service.delete_a_sub_user                  OPTIONS,DELETE       /api/v1/subusers/
subuser_service.modify_a_sub_user                  PUT,OPTIONS          /api/v1/subusers/
subuser_service.query_sub_users                    HEAD,OPTIONS,GET     /api/v1/subusers/
user_service.account_login                         POST,OPTIONS         /api/v1/users/login/account
user_service.create_a_enterprise_user              POST,OPTIONS         /api/v1/users/enterprise
user_service.create_a_individual_user              POST,OPTIONS         /api/v1/users/
user_service.create_a_individual_user              POST,OPTIONS         /api/v1/users/individual
user_service.delete_a_user                         OPTIONS,DELETE       /api/v1/users/
user_service.email_login                           POST,OPTIONS         /api/v1/users/login/email
user_service.mobile_login                          POST,OPTIONS         /api/v1/users/login/mobile
user_service.modify_a_user                         PUT,OPTIONS          /api/v1/users/
user_service.query_default_permissions             HEAD,OPTIONS,GET     /api/v1/users/permissions
user_service.query_users                           HEAD,OPTIONS,GET     /api/v1/users/
user_service.verify_email                          POST,OPTIONS         /api/v1/users/verify/email
user_service.verify_mobile                         POST,OPTIONS         /api/v1/users/verify/mobile

2.4. flask api

  • 代码
all_links_template = '''

    {% for endpoint, methods, url in links %}
    
    {% endfor %}
Endpoint Methods Url
{{endpoint}} {{methods}} {{url}}
''' @app.route("/all-links") def all_links(): from flask import render_template_string links = [] for rule in app.url_map.iter_rules(): methods = ','.join(rule.methods) links.append((rule.endpoint, methods, rule)) return render_template_string(all_links_template, links=links)
  • 浏览器结果


    API列表

3. 总结

按照上述方法,可以获取到api列表,帮忙我们更好地实现团队内分享和沟通。
提一些优化,如果可以看到api request和response就更好了。

  • 可以在endpoint增加gitlab的连接,这样直接看源码
  • 也可以在endpoint函数增加一些注释或参数,添加到#2.4的表格,比如接口描述、接口request参数等

你可能感兴趣的:(Flask应用示例7 - 罗列所有API)