feat: добавление Swagger UI для документации API - реализован маршрут для swagger.yaml и зарегистрирован blueprint для удобного доступа к документации

This commit is contained in:
Vlad 2025-04-20 02:51:10 +03:00
parent be5090f9a3
commit ba3187b5ff
2 changed files with 272 additions and 1 deletions

256
api/swagger.yaml Normal file
View File

@ -0,0 +1,256 @@
openapi: 3.0.0
info:
title: SalvageDB API
description: API for checking vehicle history and VIN decoding
version: 1.0.0
servers:
- url: /api
description: Main server
paths:
/search:
post:
summary: Search vehicle by VIN
description: Search for vehicle information by VIN number
parameters:
- name: access_code
in: query
required: true
schema:
type: string
description: API access code for authentication
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
vin:
type: string
description: Vehicle Identification Number
example: "1HGCM82633A123456"
responses:
'200':
description: Successful search
content:
application/json:
schema:
type: object
properties:
found:
type: boolean
description: Whether the vehicle was found
details:
type: object
description: Vehicle details
'400':
description: Invalid VIN or access code
content:
application/json:
schema:
type: object
properties:
error:
type: string
description: Error description
/decode:
post:
summary: Decode VIN
description: Decode VIN number to get vehicle information
parameters:
- name: access_code
in: query
required: true
schema:
type: string
description: API access code for authentication
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
vin:
type: string
description: Vehicle Identification Number
example: "1HGCM82633A123456"
responses:
'200':
description: Successful decoding
content:
application/json:
schema:
type: object
properties:
make:
type: string
description: Vehicle make
model:
type: string
description: Vehicle model
year:
type: integer
description: Manufacturing year
'400':
description: Invalid VIN or access code
content:
application/json:
schema:
type: object
properties:
error:
type: string
description: Error description
/detail/{vin}:
get:
summary: Get detailed information
description: Get detailed information about a vehicle by VIN
parameters:
- name: vin
in: path
required: true
schema:
type: string
description: Vehicle Identification Number
- name: access_code
in: query
required: true
schema:
type: string
description: API access code for authentication
responses:
'200':
description: Successful information retrieval
content:
application/json:
schema:
type: object
properties:
vin:
type: string
description: VIN number
make:
type: string
description: Vehicle make
model:
type: string
description: Vehicle model
year:
type: integer
description: Manufacturing year
history:
type: array
items:
type: object
properties:
date:
type: string
format: date
description: Event date
event:
type: string
description: Event description
'404':
description: Vehicle not found
content:
application/json:
schema:
type: object
properties:
error:
type: string
description: Error description
/restfact:
get:
summary: Check account balance
description: Get current account balance and pricing information
parameters:
- name: access_code
in: query
required: true
schema:
type: string
description: API access code for authentication
responses:
'200':
description: Successful balance check
content:
application/json:
schema:
type: object
properties:
access_code:
type: string
description: API access code
restfact:
type: number
description: Current account balance
'400':
description: Invalid access code
content:
application/json:
schema:
type: object
properties:
error:
type: string
description: Error description
/restfact_detail:
get:
summary: Get request history
description: Get detailed history of API requests for the last 45 days
parameters:
- name: access_code
in: query
required: true
schema:
type: string
description: API access code for authentication
responses:
'200':
description: Successful history retrieval
content:
application/json:
schema:
type: object
properties:
access_code:
type: string
description: API access code
report:
type: array
items:
type: object
properties:
VIN:
type: string
description: Vehicle Identification Number
IP:
type: string
description: IP address used for request
STATUS:
type: string
description: Request status (FOUND/NOT FOUND)
DATETIME:
type: string
format: date-time
description: Request date and time
COST:
type: number
description: Cost of the request
'400':
description: Invalid access code
content:
application/json:
schema:
type: object
properties:
error:
type: string
description: Error description

17
app.py
View File

@ -1,4 +1,4 @@
from flask import Flask, render_template, send_from_directory, make_response, request, redirect, session, g, json, send_file, abort
from flask import Flask, render_template, send_from_directory, make_response, request, redirect, session, g, json, send_file, abort, jsonify
import os
import random
import socket
@ -12,6 +12,7 @@ import io
import json
from expiring_dict import ExpiringDict
import uuid
from flask_swagger_ui import get_swaggerui_blueprint
@ -34,6 +35,17 @@ os.environ['NLS_LANG'] = 'American_America.AL32UTF8'
#Cache
app.cache = ExpiringDict(60*60*24)
# Swagger UI
SWAGGER_URL = '/api/docs'
API_URL = '/api/swagger.yaml'
swaggerui_blueprint = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "SalvageDB API"
}
)
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)
try:
gips = open('gips.txt').read().split('\n')
@ -767,6 +779,9 @@ def get_addr(req) -> str:
except:
return req
@app.route('/api/swagger.yaml')
def swagger_yaml():
return send_from_directory('api', 'swagger.yaml')
if __name__ == '__main__':
# Start a pool of connections