-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.py
150 lines (127 loc) · 4.13 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import random
from wsgiref.simple_server import make_server
import validation
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from pyramid.config import Configurator
from pyramid.view import exception_view_config, view_config
from pyramid_apispec.helpers import add_pyramid_paths
@view_config(route_name="users", renderer="json")
def users_post(request):
"""Some comment.
---
x-extension: value
post:
security:
- APIKeyHeader: []
tags:
- "Legacy API"
summary: "Register user"
description: ""
operationId: "users_post"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- in: "body"
name: "body"
description: "Foo bar"
required: true
schema: FooBodySchema
responses:
200:
description: response for 200 code
schema: FooBodySchema
"""
schema = validation.FooBodySchema()
struct = schema.loads(request.body or "{}")
return struct.data
@view_config(route_name="bar_route", request_method="GET", renderer="json")
def bar_get(request):
"""Return something.
---
get:
description: some description
parameters:
- in: query
name: offset
type: integer
description: The number of items to skip
responses:
200:
description: response for 200 code
schema: BarBodySchema
"""
return {"a_field": random.random(), "b_field": random.randint(1, 999999)}
@view_config(route_name="bar_route", request_method="POST", renderer="json")
def bar_post(request):
"""Return something.
---
post:
tags:
- "Other API"
summary: "Send a list of bar's"
description: ""
operationId: "bar_post"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- in: "body"
name: "body"
description: "Bar body description"
required: true
schema: BarBodySchema
responses:
200:
description: response for 200 code
schema: BarBodySchema
"""
schema = validation.BarBodySchema(many=True)
struct = schema.loads(request.body or "{}")
return struct.data
@view_config(route_name="openapi_spec", renderer="json")
def api_spec(request):
"""
Serve the spec to explorer
"""
spec = APISpec(
title="Some API",
version="1.0.0",
openapi_version="2.0",
plugins=[MarshmallowPlugin()],
)
# using marshmallow plugin here
spec.components.schema("FooBodySchema", schema=validation.FooBodySchema)
spec.components.schema("BarBodySchema", schema=validation.BarBodySchema(many=True))
# register API security scheme
api_auth_scheme = {"type": "apiKey", "in": "header", "name": "Authorization"}
spec.components.security_scheme("APIKeyHeader", api_auth_scheme)
# inspect the `foo_route` and generate operations from docstring
add_pyramid_paths(spec, "users", request=request)
# inspection supports filtering via pyramid add_view predicate arguments
add_pyramid_paths(spec, "bar_route", request=request)
my_spec = spec.to_dict()
# you can further mutate the spec dict to include things like security definitions
return my_spec
@exception_view_config(context="marshmallow.ValidationError", renderer="json")
def marshmallow_invalid_data(context, request):
request.response.status = 422
return context.messages
def build_wsgi_app():
with Configurator() as config:
config.add_route("users", "/users")
config.add_route("bar_route", "/bar")
config.add_route("openapi_spec", "/openapi.json")
config.include("pyramid_apispec.views")
config.pyramid_apispec_add_explorer(spec_route_name="openapi_spec")
config.scan(".")
app = config.make_wsgi_app()
return app
if __name__ == "__main__":
app = build_wsgi_app()
server = make_server("0.0.0.0", 6543, app)
print("visit api explorer at http://127.0.0.1:6543/api-explorer")
server.serve_forever()