|
@@ -48,6 +48,91 @@ python setup.py develop
|
48
|
48
|
|
49
|
49
|
## Give it a try
|
50
|
50
|
|
|
51
|
+### Fast Flask example
|
|
52
|
+
|
|
53
|
+```
|
|
54
|
+from datetime import datetime
|
|
55
|
+import flask
|
|
56
|
+import marshmallow
|
|
57
|
+import hapic
|
|
58
|
+from hapic.ext.flask import FlaskContext
|
|
59
|
+import json
|
|
60
|
+
|
|
61
|
+hapic = hapic.Hapic()
|
|
62
|
+app = flask.Flask(__name__)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+class UriPathSchema(marshmallow.Schema): # schema describing the URI and allowed values
|
|
66
|
+ name = marshmallow.fields.String(required=True)
|
|
67
|
+ age = marshmallow.fields.Integer(required=False)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+class HelloResponseSchema(marshmallow.Schema): # schema of the API response
|
|
71
|
+ name = marshmallow.fields.String(required=True)
|
|
72
|
+ now = marshmallow.fields.DateTime(required=False)
|
|
73
|
+ greetings = marshmallow.fields.String(required=False)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+@app.route('/hello/<name>') # flask route. must always be before hapic decorators
|
|
77
|
+@hapic.with_api_doc() # the first hapic decorator. Register the method for auto-documentation
|
|
78
|
+@hapic.input_path(UriPathSchema()) # validate the URI structure
|
|
79
|
+@hapic.output_body(HelloResponseSchema()) # define output structure
|
|
80
|
+def hello(name='<No name>', hapic_data=None):
|
|
81
|
+ return {
|
|
82
|
+ 'name': name,
|
|
83
|
+ 'now': datetime.now(),
|
|
84
|
+ 'dummy': { 'some': 'dummy' } # will be ignored
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+class UriPathSchemaWithAge(marshmallow.Schema): # schema describing the URI and allowed values
|
|
88
|
+ name = marshmallow.fields.String(required=True)
|
|
89
|
+ age = marshmallow.fields.Integer(required=False)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+@app.route('/hello/<name>/age/<age>')
|
|
93
|
+@hapic.with_api_doc()
|
|
94
|
+@hapic.input_path(UriPathSchemaWithAge())
|
|
95
|
+@hapic.output_body(HelloResponseSchema())
|
|
96
|
+def hello2(name='<No name>', age=42, hapic_data=None):
|
|
97
|
+ return {
|
|
98
|
+ 'name': name,
|
|
99
|
+ 'age': age,
|
|
100
|
+ 'greetings': 'Hello {name}, it looks like you are {age}'.format(
|
|
101
|
+ name=name,
|
|
102
|
+ age=age
|
|
103
|
+ ),
|
|
104
|
+ 'now': datetime.now(),
|
|
105
|
+ 'dummy': { 'some': 'dummy' } # will be ignored
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+hapic.set_context(FlaskContext(app))
|
|
110
|
+print(json.dumps(hapic.generate_doc(title='API Doc', description='doc desc.'))) # Generate the documentation
|
|
111
|
+app.run('127.0.0.1', 8080, debug=True)
|
|
112
|
+```
|
|
113
|
+
|
|
114
|
+How to use it:
|
|
115
|
+
|
|
116
|
+Nominal cases:
|
|
117
|
+
|
|
118
|
+```
|
|
119
|
+$ curl "http://127.0.0.1:8080/hello/michel"
|
|
120
|
+# {"now": "2017-12-18T12:37:10.751623+00:00", "name": "michel"}
|
|
121
|
+```
|
|
122
|
+
|
|
123
|
+```
|
|
124
|
+$ curl "http://127.0.0.1:8080/hello/michel/age/17"
|
|
125
|
+# {"name": "damien", "greetings": "Hello damien, it looks like you are 17", "now": "2017-12-18T12:41:58.229679+00:00"}
|
|
126
|
+```
|
|
127
|
+
|
|
128
|
+Error case (returns a 400):
|
|
129
|
+
|
|
130
|
+```
|
|
131
|
+$ curl "http://127.0.0.1:8080/hello/michel/age/mistaken"
|
|
132
|
+# {"details": {"age": ["Not a valid integer."]}, "message": "Validation error of input data"}
|
|
133
|
+```
|
|
134
|
+
|
|
135
|
+
|
51
|
136
|
### A complete user API
|
52
|
137
|
|
53
|
138
|
In the `example/usermanagement` directory you can find a complete example of an API allowing to manage users.
|