-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.py
57 lines (46 loc) · 1.79 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from flask import Flask
from flask_restful import Resource, Api
from apispec import APISpec
from marshmallow import Schema, fields
from apispec.ext.marshmallow import MarshmallowPlugin
from flask_apispec.extension import FlaskApiSpec
from flask_apispec.views import MethodResource
from flask_apispec import marshal_with, doc, use_kwargs
app = Flask(__name__) # Flask app instance initiated
api = Api(app) # Flask restful wraps Flask app around it.
app.config.update({
'APISPEC_SPEC': APISpec(
title='Awesome Project',
version='v1',
plugins=[MarshmallowPlugin()],
openapi_version='2.0.0'
),
'APISPEC_SWAGGER_URL': '/swagger/', # URI to access API Doc JSON
'APISPEC_SWAGGER_UI_URL': '/swagger-ui/' # URI to access UI of API Doc
})
docs = FlaskApiSpec(app)
class AwesomeResponseSchema(Schema):
message = fields.Str(default='Success')
class AwesomeRequestSchema(Schema):
api_type = fields.String(required=True, description="API type of awesome API")
# Restful way of creating APIs through Flask Restful
class AwesomeAPI(MethodResource, Resource):
@doc(description='My First GET Awesome API.', tags=['Awesome'])
@marshal_with(AwesomeResponseSchema) # marshalling
def get(self):
'''
Get method represents a GET API method
'''
return {'message': 'My First Awesome API'}
@doc(description='My First GET Awesome API.', tags=['Awesome'])
@use_kwargs(AwesomeRequestSchema, location=('json'))
@marshal_with(AwesomeResponseSchema) # marshalling
def post(self, **kwargs):
'''
Get method represents a GET API method
'''
return {'message': 'My First Awesome API'}
api.add_resource(AwesomeAPI, '/awesome')
docs.register(AwesomeAPI)
if __name__ == '__main__':
app.run(debug=True)