Python-based API management platform development (V1.0) and pythonapi
Today, the blogger finally completed the API interface management platform, and the last difference was database maintenance,
Here, the author introduces the design principles of the platform. First, based on python, the flask web framework + bootstrap front-end framework is used to complete the front-end display page.
The second phase should be added, logged out, and managed in the background
The following is the document structure
Third-party python modules involved: flask, flask-bootstrap, and sqlalchemy
Overall page layout: navigation of the page header, API category on the right, page information
The page information includes: interface description, request parameters, return parameters, request example, and return example.
The following is the model. py file that defines the database object.
#! /Usr/bin/env python #-*-coding: UTF-8-*-from sqlalchemy import Column, create_enginefrom sqlalchemy. orm import sessionmakerfrom sqlalchemy. ext. declarative import declarative_baseimport jsonimport sys # import all table Field Types of the database from sqlalchemy. dialects. mysql import \ BIGINT, BINARY, BIT, BLOB, BOOLEAN, CHAR, DATE, \ DATETIME, DECIMAL, DECIMAL, DOUBLE, ENUM, FLOAT, INTEGER, \ LONGBLOB, LONGTEXT, MEDIUMBLOB, MEDIUMINT, MEDIUMTEXT, NCHAR, \ NUMERIC, NVARCHAR, REAL, SET, SMALLINT, TEXT, TIME, TIMESTAMP, \ TINYBLOB, TINYINT, TINYTEXT, VARBINARY, VARCHAR, YEAR # Base class for creating objects: Base = declarative_base () reload (sys) sys. setdefaultencoding ("UTF-8") # define the api table object class Api (Base): # Name of the Table: _ tablename _ = 'API' # table structure: id = Column (INTEGER (10), primary_key = True) name = Column (VARCHAR (50) url = Column (TEXT) method = Column (VARCHAR (10 )) service = Column (VARCHAR (50) access_token = Column (VARCHAR (255) reqParam = Column (TEXT) response = Column (TEXT) requestExam = Column (TEXT) responseExam = Column (TEXT) # define the model Table object class Model (Base): # Table Name: _ tablename _ = 'model' # table structure: id = Column (VARCHAR (25), primary_key = True) name = Column (VARCHAR (50) ch_name = Column (VARCHAR (50) from_id = Column (VARCHAR (25 ))
The following is an attempt to view. py
#!/usr/bin/env python# -*- coding: utf-8 -*-from app import appfrom flask import render_template, flash, redirect, session, url_for, request, gfrom models import Api, Modelfrom config import connect_dbimport jsonimport sysreload(sys)sys.setdefaultencoding("utf-8")@app.route("/")@app.route("/index")def index(): return render_template("index.html")@app.route("/<id>", methods=["GET", "POST"])def model(id): all_name = [] db = connect_db() conn = db() table_model = conn.query(Model).filter(Model.name == id).one() model_name = table_model.name model_ch_name = table_model.ch_name model_all = conn.query(Model).filter(Model.from_id == table_model.id).all() for i in range(len(model_all)): name = {"ch_name": model_all[i].ch_name, "name": model_all[i].name} all_name.append(name) conn.close() return render_template("model.html", model_name=model_name, model_ch_name=model_ch_name, all_name=all_name)@app.route("/desk/<id>")def form(id): all_names = [] db = connect_db() conn = db() table_model = conn.query(Model).filter(Model.name == id).one() model_all = conn.query(Model).filter(Model.from_id == table_model.from_id).all() for i in range(len(model_all)): name = {"ch_name": model_all[i].ch_name, "name": model_all[i].name} all_names.append(name) model_id = conn.query(Model).filter(Model.id == table_model.from_id).one() model_ch_name = model_id.ch_name model_name = model_id.name table_api = conn.query(Api).filter(Api.id == table_model.id).one() name = table_api.name url = table_api.url method = table_api.method service = json.loads(table_api.service) access_token = json.loads(table_api.access_token) reqparam = json.loads(table_api.reqParam) response = json.loads(table_api.response) request_exam = table_api.requestExam response_exam = table_api.responseExam conn.close() return render_template("form.html", url=url, method=method, name=name, all_names=all_names, model_name=model_name, model_ch_name=model_ch_name, service=service, access_token=access_token, reqparam=reqparam, response=response, request_exam=request_exam, response_exam=response_exam)
The blogger defines the view of the home page, module, and interface based on the route.
Finally, let's take a look at the results.