0
해당 응용 프로그램은 웹 서버를 처리하기 위해 플라스크를 감싸는 connexion을 사용합니다. API는 swagger로 지정됩니다. http 응답이 웹 서버에서 공식화되는 코드로 어딘가에 들어가는 쉽고 직선적 인 방법이 있습니까?모든 HTTP 응답을 오류 코드> = 400으로 기록하십시오.
가능한 경우 200 개의 오류 처리기 또는 10 개의 가장 인기있는 내 손가락을 쓰는 것을 피하고 싶습니다.
api.py
import connexion
app = connexion.App(__name__,
specification_dir='../swagger/',
swagger_ui=False,
validator_map={
'body': connexion.decorators.validation.RequestBodyValidator
})
app.add_api('swagger.yml', strict_validation=True)
# If I had to use app.error_handler decorators to implement the special
# treatment of http responses with error codes, I would put it here
swagger.yml
swagger: '2.0'
info:
title: My Minimal Working Example
consumes:
- application/json
produces:
- application/json
basePath: /api/v1
paths:
'/do_something':
post:
tags:
- MyTag
operationId: entrypoint.do_something
summary: Do something on request
parameters:
- name: data
in: body
schema:
$ref: '#/definitions/data'
responses:
'200':
description: Success!
schema:
$ref: '#/definitions/Response'
'400':
description: Error!
schema:
$ref: '#/definitions/Response'
'403':
description: Not Authorized
schema:
$ref: '#/definitions/Response'
# a lot more stuff and "definitions"
코드를 직접 쓰지 않고 swagger를 사용하는 경우라면, swagger가 할 수있는 것에 국한됩니다. 그렇지 않으면 Flask를 하위 클래스로 분류하고 오류 처리를 재정의하는 방법이 있습니다. Swagger 또는 Connexion을 알지 못하면이 문제가 귀하의 경우 가능할 지 모르겠습니다. Flask 클래스 또는 app 인스턴스를 제어 할 수 있습니까? – davidism
코드를 직접 작성 중이며 앱 인스턴스를 제어 할 수 있습니다. – Arne
[mcve]를 포함하도록 [편집]하십시오. – davidism