Skip to content

Moved Api to routes #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 1 addition & 30 deletions service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,13 @@
"""
import sys
from flask import Flask
from flask_restx import Api
from service.common import log_handlers
from service import config

# NOTE: Do not change the order of this code
# 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
Expand All @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion service/common/error_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 24 additions & 17 deletions service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)


######################################################################
Expand All @@ -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"),
},
)
Expand All @@ -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,
Expand Down
Loading