You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+16-12Lines changed: 16 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,6 @@ As a result of these goals, hug is Python 3+ only and built upon [Falcon's](http
25
25
26
26
[](https://github.com/hugapi/hug/blob/develop/examples/hello_world.py)
27
27
28
-
29
28
Installing hug
30
29
===================
31
30
@@ -37,9 +36,9 @@ pip3 install hug --upgrade
37
36
38
37
Ideally, within a [virtual environment](http://docs.python-guide.org/en/latest/dev/virtualenvs/).
39
38
40
-
41
39
Getting Started
42
40
===================
41
+
43
42
Build an example API with a simple endpoint in just a few lines.
44
43
45
44
```py
@@ -118,7 +117,6 @@ Then you can access the example from `localhost:8000/v1/echo?text=Hi` / `localho
118
117
119
118
Note: versioning in hug automatically supports both the version header as well as direct URL based specification.
@@ -182,7 +178,6 @@ def math(number_1:int, number_2:int): #The :int after both arguments is the Type
182
178
Type annotations also feed into `hug`'s automatic documentation
183
179
generation to let users of your API know what data to supply.
184
180
185
-
186
181
**Directives** functions that get executed with the request / response data based on being requested as an argument in your api_function.
187
182
These apply as input parameters only, and can not be applied currently as output formats or transformations.
188
183
@@ -242,7 +237,6 @@ def hello():
242
237
243
238
as shown, you can easily change the output format for both an entire API as well as an individual API call
244
239
245
-
246
240
**Input Formatters** a function that takes the body of data given from a user of your API and formats it for handling.
247
241
248
242
```py
@@ -253,7 +247,6 @@ def my_input_formatter(data):
253
247
254
248
Input formatters are mapped based on the `content_type` of the request data, and only perform basic parsing. More detailed parsing should be done by the Type Annotations present on your `api_function`
255
249
256
-
257
250
**Middleware** functions that get called for every request a hug API processes
258
251
259
252
```py
@@ -272,6 +265,18 @@ You can also easily add any Falcon style middleware using:
272
265
__hug__.http.add_middleware(MiddlewareObject())
273
266
```
274
267
268
+
**Parameter mapping** can be used to override inferred parameter names, eg. for reserved keywords:
269
+
270
+
```py
271
+
import marshmallow.fields as fields
272
+
...
273
+
274
+
@hug.get('/foo', map_params={'from': 'from_date'}) # API call uses 'from'
275
+
defget_foo_by_date(from_date: fields.DateTime()):
276
+
return find_foo(from_date)
277
+
```
278
+
279
+
Input formatters are mapped based on the `content_type` of the request data, and only perform basic parsing. More detailed parsing should be done by the Type Annotations present on your `api_function`
275
280
276
281
Splitting APIs over multiple files
277
282
===================
@@ -314,7 +319,6 @@ Or alternatively - for cases like this - where only one module is being included
314
319
hug.API(__name__).extend(something, '/something')
315
320
```
316
321
317
-
318
322
Configuring hug 404
319
323
===================
320
324
@@ -346,14 +350,14 @@ def not_found_handler():
346
350
return"Not Found"
347
351
```
348
352
349
-
350
353
Asyncio support
351
354
===============
352
355
353
356
When using the `get` and `cli` method decorator on coroutines, hug will schedule
354
357
the execution of the coroutine.
355
358
356
359
Using asyncio coroutine decorator
360
+
357
361
```py
358
362
@hug.get()
359
363
@asyncio.coroutine
@@ -362,6 +366,7 @@ def hello_world():
362
366
```
363
367
364
368
Using Python 3.5 async keyword.
369
+
365
370
```py
366
371
@hug.get()
367
372
asyncdefhello_world():
@@ -371,9 +376,9 @@ async def hello_world():
371
376
NOTE: Hug is running on top Falcon which is not an asynchronous server. Even if using
372
377
asyncio, requests will still be processed synchronously.
373
378
374
-
375
379
Using Docker
376
380
===================
381
+
377
382
If you like to develop in Docker and keep your system clean, you can do that but you'll need to first install [Docker Compose](https://docs.docker.com/compose/install/).
378
383
379
384
Once you've done that, you'll need to `cd` into the `docker` directory and run the web server (Gunicorn) specified in `./docker/gunicorn/Dockerfile`, after which you can preview the output of your API in the browser on your host machine.
0 commit comments