diff --git a/service/__init__.py b/service/__init__.py index 9e1676a..68a4c1a 100644 --- a/service/__init__.py +++ b/service/__init__.py @@ -22,7 +22,6 @@ """ import sys from flask import Flask -from flask_restx import Api from service.common import log_handlers from service import config @@ -30,18 +29,6 @@ # The Flask app must be created # BEFORE you import modules that depend on it !!! -# Document the type of authorization required -authorizations = { - "apikey": { - "type": "apiKey", - "in": "header", - "name": "X-Api-Key" - } -} - -# Will be initialize when app is created -api = None # pylint: disable=invalid-name - ############################################################ # Initialize the Flask instance @@ -56,27 +43,11 @@ def create_app(): # Turn off strict slashes because it violates best practices app.url_map.strict_slashes = False - ###################################################################### - # Configure Swagger before initializing it - ###################################################################### - global api - api = Api( - app, - version="1.0.0", - title="Pet Demo REST API Service", - description="This is a sample server Pet store server.", - default="pets", - default_label="Pet shop operations", - doc="/apidocs", # default also could use doc='/apidocs/' - authorizations=authorizations, - prefix="/api", - ) - with app.app_context(): # Import the routes After the Flask app is created # pylint: disable=import-outside-toplevel from service import routes, models # noqa: F401, E402 - from service.common import error_handlers # pylint: disable=unused-import + from service.common import error_handlers # pylint: disable=unused-import try: models.Pet.init_db(app.config["CLOUDANT_DBNAME"]) diff --git a/service/common/error_handlers.py b/service/common/error_handlers.py index f602cd3..2728c5f 100644 --- a/service/common/error_handlers.py +++ b/service/common/error_handlers.py @@ -21,7 +21,7 @@ """ from flask import current_app as app # Import Flask application -from service import api +from service.routes import api from service.models import DataValidationError, DatabaseConnectionError from . import status diff --git a/service/routes.py b/service/routes.py index d89ee2f..be8a262 100644 --- a/service/routes.py +++ b/service/routes.py @@ -32,10 +32,27 @@ from functools import wraps from flask import request from flask import current_app as app # Import Flask application -from flask_restx import Resource, fields, reqparse, inputs +from flask_restx import Api, Resource, fields, reqparse, inputs from service.models import Pet, Gender from service.common import status # HTTP Status Codes -from . import api + +# Document the type of authorization required +authorizations = {"apikey": {"type": "apiKey", "in": "header", "name": "X-Api-Key"}} + +###################################################################### +# Configure Swagger before initializing it +###################################################################### +api = Api( + app, + version="1.0.0", + title="Pet Demo REST API Service", + description="This is a sample server Pet store server.", + default="pets", + default_label="Pet shop operations", + doc="/apidocs", # default also could use doc='/apidocs/' + authorizations=authorizations, + prefix="/api", +) ###################################################################### @@ -56,13 +73,9 @@ def index(): required=True, description="The category of Pet (e.g., dog, cat, fish, etc.)", ), - "available": fields.Boolean( - required=True, description="Is the Pet available for purchase?" - ), + "available": fields.Boolean(required=True, description="Is the Pet available for purchase?"), # pylint: disable=protected-access - "gender": fields.String( - enum=Gender._member_names_, description="The gender of the Pet" - ), + "gender": fields.String(enum=Gender._member_names_, description="The gender of the Pet"), "birthday": fields.Date(required=True, description="The day the pet was born"), }, ) @@ -71,20 +84,14 @@ def index(): "PetModel", create_model, { - "_id": fields.String( - readOnly=True, description="The unique id assigned internally by service" - ), + "_id": fields.String(readOnly=True, description="The unique id assigned internally by service"), }, ) # query string arguments pet_args = reqparse.RequestParser() -pet_args.add_argument( - "name", type=str, location="args", required=False, help="List Pets by name" -) -pet_args.add_argument( - "category", type=str, location="args", required=False, help="List Pets by category" -) +pet_args.add_argument("name", type=str, location="args", required=False, help="List Pets by name") +pet_args.add_argument("category", type=str, location="args", required=False, help="List Pets by category") pet_args.add_argument( "available", type=inputs.boolean,