Skip to content

Commit ff0313d

Browse files
authored
Merge pull request #29 from nyu-devops/fa24-updates
Moved Api to routes
2 parents f5ef3d2 + 651b222 commit ff0313d

File tree

3 files changed

+26
-48
lines changed

3 files changed

+26
-48
lines changed

service/__init__.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,13 @@
2222
"""
2323
import sys
2424
from flask import Flask
25-
from flask_restx import Api
2625
from service.common import log_handlers
2726
from service import config
2827

2928
# NOTE: Do not change the order of this code
3029
# The Flask app must be created
3130
# BEFORE you import modules that depend on it !!!
3231

33-
# Document the type of authorization required
34-
authorizations = {
35-
"apikey": {
36-
"type": "apiKey",
37-
"in": "header",
38-
"name": "X-Api-Key"
39-
}
40-
}
41-
42-
# Will be initialize when app is created
43-
api = None # pylint: disable=invalid-name
44-
4532

4633
############################################################
4734
# Initialize the Flask instance
@@ -56,27 +43,11 @@ def create_app():
5643
# Turn off strict slashes because it violates best practices
5744
app.url_map.strict_slashes = False
5845

59-
######################################################################
60-
# Configure Swagger before initializing it
61-
######################################################################
62-
global api
63-
api = Api(
64-
app,
65-
version="1.0.0",
66-
title="Pet Demo REST API Service",
67-
description="This is a sample server Pet store server.",
68-
default="pets",
69-
default_label="Pet shop operations",
70-
doc="/apidocs", # default also could use doc='/apidocs/'
71-
authorizations=authorizations,
72-
prefix="/api",
73-
)
74-
7546
with app.app_context():
7647
# Import the routes After the Flask app is created
7748
# pylint: disable=import-outside-toplevel
7849
from service import routes, models # noqa: F401, E402
79-
from service.common import error_handlers # pylint: disable=unused-import
50+
from service.common import error_handlers # pylint: disable=unused-import
8051

8152
try:
8253
models.Pet.init_db(app.config["CLOUDANT_DBNAME"])

service/common/error_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"""
2222

2323
from flask import current_app as app # Import Flask application
24-
from service import api
24+
from service.routes import api
2525
from service.models import DataValidationError, DatabaseConnectionError
2626
from . import status
2727

service/routes.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,27 @@
3232
from functools import wraps
3333
from flask import request
3434
from flask import current_app as app # Import Flask application
35-
from flask_restx import Resource, fields, reqparse, inputs
35+
from flask_restx import Api, Resource, fields, reqparse, inputs
3636
from service.models import Pet, Gender
3737
from service.common import status # HTTP Status Codes
38-
from . import api
38+
39+
# Document the type of authorization required
40+
authorizations = {"apikey": {"type": "apiKey", "in": "header", "name": "X-Api-Key"}}
41+
42+
######################################################################
43+
# Configure Swagger before initializing it
44+
######################################################################
45+
api = Api(
46+
app,
47+
version="1.0.0",
48+
title="Pet Demo REST API Service",
49+
description="This is a sample server Pet store server.",
50+
default="pets",
51+
default_label="Pet shop operations",
52+
doc="/apidocs", # default also could use doc='/apidocs/'
53+
authorizations=authorizations,
54+
prefix="/api",
55+
)
3956

4057

4158
######################################################################
@@ -56,13 +73,9 @@ def index():
5673
required=True,
5774
description="The category of Pet (e.g., dog, cat, fish, etc.)",
5875
),
59-
"available": fields.Boolean(
60-
required=True, description="Is the Pet available for purchase?"
61-
),
76+
"available": fields.Boolean(required=True, description="Is the Pet available for purchase?"),
6277
# pylint: disable=protected-access
63-
"gender": fields.String(
64-
enum=Gender._member_names_, description="The gender of the Pet"
65-
),
78+
"gender": fields.String(enum=Gender._member_names_, description="The gender of the Pet"),
6679
"birthday": fields.Date(required=True, description="The day the pet was born"),
6780
},
6881
)
@@ -71,20 +84,14 @@ def index():
7184
"PetModel",
7285
create_model,
7386
{
74-
"_id": fields.String(
75-
readOnly=True, description="The unique id assigned internally by service"
76-
),
87+
"_id": fields.String(readOnly=True, description="The unique id assigned internally by service"),
7788
},
7889
)
7990

8091
# query string arguments
8192
pet_args = reqparse.RequestParser()
82-
pet_args.add_argument(
83-
"name", type=str, location="args", required=False, help="List Pets by name"
84-
)
85-
pet_args.add_argument(
86-
"category", type=str, location="args", required=False, help="List Pets by category"
87-
)
93+
pet_args.add_argument("name", type=str, location="args", required=False, help="List Pets by name")
94+
pet_args.add_argument("category", type=str, location="args", required=False, help="List Pets by category")
8895
pet_args.add_argument(
8996
"available",
9097
type=inputs.boolean,

0 commit comments

Comments
 (0)