2017-10-09 1 views
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" 
+1

코드를 직접 쓰지 않고 swagger를 사용하는 경우라면, swagger가 할 수있는 것에 국한됩니다. 그렇지 않으면 Flask를 하위 클래스로 분류하고 오류 처리를 재정의하는 방법이 있습니다. Swagger 또는 Connexion을 알지 못하면이 문제가 귀하의 경우 가능할 지 모르겠습니다. Flask 클래스 또는 app 인스턴스를 제어 할 수 있습니까? – davidism

+0

코드를 직접 작성 중이며 앱 인스턴스를 제어 할 수 있습니다. – Arne

+0

[mcve]를 포함하도록 [편집]하십시오. – davidism

답변

1

davidism이 제안 내가 플라스크 개체를 서브 클래 싱하여 문제를 해결했다.

app.py

import logging.config 
import yaml 

logging.config.dictConfig(yaml.load(open('logging.conf', 'r'))) 
logger = logging.getLogger("mainLogger") 


class LoggingFlask(Flask): 
    def make_response(self, rv): 
     rv = super(LoggingFlask, self).make_response(rv) 
     if int(rv.status_code) >= 300: 
      logger.warn("Request failed with error code %s." % rv.status_code) 
     return rv 


app = LoggingFlask(__name__) 

session.log

./app.py [2017-10-10 11:38:19,564 - werkzeug - INFO]: * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) [2017-10-10 11:38:19,566 - werkzeug - INFO]: * Restarting with stat [2017-10-10 11:38:19,690 - werkzeug - WARNING]: * Debugger is active! [2017-10-10 11:38:19,691 - werkzeug - INFO]: * Debugger PIN: 211-310-838 # issues a good request [2017-10-10 11:38:25,179 - werkzeug - INFO]: 127.0.0.1 - - [10/Oct/2017 11:38:25] "GET /todo/api/v1.0/tasks HTTP/1.1" 200 - # issued a bad request [2017-10-10 11:38:28,646 - mainLogger - WARNING]: Request failed with error code 404. [2017-10-10 11:38:28,646 - mainLogger - WARNING]: Request failed with error code 404. [2017-10-10 11:38:28,647 - werkzeug - INFO]: 127.0.0.1 - - [10/Oct/2017 11:38:28] "GET /todo/api/v1.0/task HTTP/1.1" 404 - 

사람이 현재의 응답을 트리거 요청에 액세스하는 방법을 알고있는 경우

주시기 : 짧은 버전이 있습니다 나는이 대답에도 그것을 포함시킬 수 있도록 주석을 삭제합니다.

관련 문제