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
Often times you'll find yourself needing something particular to an interface (say a header, a session, or content_type) but don't want to tie your function
5
-
to a single interface. To support this: hug introduces the concept of `directives`. In hug directives are simply arguments that have been registered to automatically provide a parameter value based on knowledge known to the interface.
4
+
Oftentimes you'll find yourself needing something particular to an interface (say a header, a session, or content_type), but don't want to tie your function
5
+
to a single interface. To support this, hug introduces the concept of `directives`. In hug, directives are simply arguments that have been registered to automatically provide a parameter value based on knowledge known to the interface.
6
6
7
-
For example - this is the builtin session directive:
7
+
For example, this is the built-in session directive:
"""Returns the session associated with the current request"""
13
13
return request and request.context.get(context_name, None) or None
14
14
15
-
Then when using this directive in your code, you can either specify the directive via type annotation:
15
+
Then, when using this directive in your code, you can either specify the directive via type annotation:
16
16
17
17
@hug.get()
18
18
def my_endpoint(session: hug.directives.session):
19
19
session # is here automatically, without needing to be passed in
20
20
21
-
Or, prefixing the argument with `hug_`:
21
+
Or by prefixing the argument with `hug_`:
22
22
23
23
@hug.get()
24
24
def my_endpoint(hug_session):
@@ -35,22 +35,22 @@ Built-in directives
35
35
36
36
hug provides a handful of directives for commonly needed attributes:
37
37
38
-
- hug.directives.Timer (hug_timer=precision): Stores the time the interface was initially called, returns how much time has passed since the function was called if casted as a float. Automatically converts to a the time taken when returned as part of a JSON structure. The default value is used to specify the float precision desired when keeping track of the time passed
39
-
- hug.directives.module (hug_module): Passes along the module that contains the API associated with this endpoint
40
-
- hug.directives.api (hug_api): Passes along the hug API singleton associated with this endpoint
41
-
- hug.directives.api_version (hug_api_version): Passes along the version of the API being called
42
-
- hug.directives.documentation (hug_documentation): Generates and passes along the entire set of documentation for the API the endpoint is contained within
43
-
- hug.directives.session (hug_session=context_name): Passes along the session associated with the current request. The default value is used to provide a different key where the value is stored on the request.context object.
44
-
- hug.directives.user (hug_user): Passes along the user object associated with the request
45
-
- hug.directives.CurrentAPI (hug_current_api): Passes along a smart versionaware API caller, to enable calling other functions within your API with reissurence the correct function is being called for the version of the API being requested
38
+
- hug.directives.Timer (hug_timer=precision): Stores the time the interface was initially called, returns how much time has passed since the function was called, if casted as a float. Automatically converts to the time taken when returned as part of a JSON structure. The default value specifies the float precision desired when keeping track of the time passed.
39
+
- hug.directives.module (hug_module): Passes along the module that contains the API associated with this endpoint.
40
+
- hug.directives.api (hug_api): Passes along the hug API singleton associated with this endpoint.
41
+
- hug.directives.api_version (hug_api_version): Passes along the version of the API being called.
42
+
- hug.directives.documentation (hug_documentation): Generates and passes along the entire set of documentation for the API that contains the endpoint.
43
+
- hug.directives.session (hug_session=context_name): Passes along the session associated with the current request. The default value provides a different key whose value is stored on the request.context object.
44
+
- hug.directives.user (hug_user): Passes along the user object associated with the request.
45
+
- hug.directives.CurrentAPI (hug_current_api): Passes along a smart, version-aware API caller, to enable calling other functions within your API, with reassurance that the correct function is being called for the version of the API being requested.
46
46
47
47
Building custom directives
48
48
===================
49
49
50
-
hug provides the `@hug.directive()` to enable creation of new directives. It takes one argument: apply_globally which defaults to False.
51
-
If you set this parameter to True the hug directive will be automatically made available as a magic `hug_` argument on all endpoints outside of just your defined API. This is not a concern if your applying directives via type annotation.
50
+
hug provides the `@hug.directive()` to enable creation of new directives. It takes one argument: apply_globally, which defaults to False.
51
+
If you set this parameter to True, the hug directive will be automatically made available as a magic `hug_` argument on all endpoints outside of your defined API. This is not a concern if you're applying directives via type annotation.
52
52
53
-
The most basic directive will simply take an optional default value, and**kwargs:
53
+
The most basic directive will take an optional default value, as well as**kwargs:
54
54
55
55
@hug.directive()
56
56
def basic(default=False, **kwargs):
@@ -65,16 +65,16 @@ This directive could then be used like this:
65
65
66
66
assert endpoint() == 'hi there!'
67
67
68
-
It's important to always accept **kwargs for directive functions as each interface get's to decide it's own set of
69
-
key word arguments to send to the directive, which can then be used to pull-in information for the directive.
68
+
It's important to always accept **kwargs for directive functions, as each interface gets to decide its own set of
69
+
keyword arguments to send to the directive, which can then be used to pullin information for the directive.
70
70
71
71
Common directive key word parameters
72
72
===================
73
73
74
-
Independ of what interface a directive is being ran through, the following key word arguments will be passed to it:
74
+
Independent of interface, the following key word arguments will be passed to the directive:
75
75
76
-
-`interface` - the interface that the directive is being ran through. Useful for conditionally injecting different data via the decorator depending on the interface it is being called through, as demonstrated at the bottom of this section
77
-
-`api` - the API singleton associated with this endpoint
76
+
-`interface` - The interface that the directive is being run through. Useful for conditionally injecting data (via the decorator) depending on the interface it is being called through, as demonstrated at the bottom of this section.
77
+
-`api` - The API singleton associated with this endpoint.
78
78
79
79
Interface Example:
80
80
@@ -92,15 +92,15 @@ Interface Example:
92
92
HTTP directive key word parameters
93
93
===================
94
94
95
-
Directives are passed the following additional keyword parameters when they are being ran through an HTTP interface:
95
+
Directives are passed the following additional keyword parameters when they are being run through an HTTP interface:
96
96
97
-
-`response`: The HTTP response object that will be returned for this request
98
-
-`request`: The HTTP request object that caused this interface to be called
99
-
-`api_version`: The version of the endpoint being hit
97
+
-`response`: The HTTP response object that will be returned for this request.
98
+
-`request`: The HTTP request object that caused this interface to be called.
99
+
-`api_version`: The version of the endpoint being hit.
100
100
101
101
CLI directive key word parameters
102
102
===================
103
103
104
-
Directives get one additional argument when they are being ran through a command line interface:
104
+
Directives get one additional argument when they are run through a command line interface:
105
105
106
-
-`argparse`: The argparse instance created to parse command line arguments
106
+
-`argparse`: The argparse instance created to parse command line arguments.
Copy file name to clipboardExpand all lines: documentation/OUTPUT_FORMATS.md
+22-22Lines changed: 22 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
hug output formats
2
2
===================
3
3
4
-
Every endpoint that is exposed via an external facing interface will need to output the data returned in a standard format that users of the interface will be able to understand.
4
+
Every endpoint that is exposed through an externally facing interface will need to return data in a standard, easily understandable format.
5
5
6
-
The default output format for all hug APIs is JSON unless you explicitly specify a different default output_format:
6
+
The default output format for all hug APIs is JSON. However, you may explicitly specify a different default output_format:
Or to specify an output_format for a specific endpoint, simply specify the output format within it's router:
16
+
Or, to specify an output_format for a specific endpoint, simply specify the output format within its router:
17
17
18
18
@hug.get(output=hug.output_format.html)
19
19
def my_endpoint():
20
20
return # HTML generating code goes here
21
21
22
-
Remember that you can also always use route chaining to specify an output format for a group of endpoints within an API:
22
+
You can use route chaining to specify an output format for a group of endpoints within an API:
23
23
24
24
html = hug.get(output=hug.output_format.html)
25
25
@@ -31,7 +31,7 @@ Remember that you can also always use route chaining to specify an output format
31
31
def root():
32
32
return # HTML generating code goes here
33
33
34
-
Finally, it's perfectly valid for an output format to in essence be a collection of other output format's that get conditionally used. For example using the builtin suffix output format:
34
+
Finally, an output format may be a collection of different output formats that get used conditionally. For example, using the built-in suffix output format:
@@ -40,33 +40,33 @@ Finally, it's perfectly valid for an output format to in essence be a collection
40
40
def my_endpoint():
41
41
return ''
42
42
43
-
In this case if the endpoint is accesed via my_endpoint.js the output type will be JSON, however if it's accesed via .html the output type will be HTML.
43
+
In this case, if the endpoint is accesed via my_endpoint.js, the output type will be JSON; however if it's accesed via my_endoint.html, the output type will be HTML.
44
44
45
-
Builtin hug output formats
45
+
Built-in hug output formats
46
46
===================
47
47
48
-
hug provides a large catolog of builtin output formats that can be used to build useful APIs right away:
48
+
hug provides a large catalog of built-in output formats, which can be used to build useful APIs right away:
49
49
50
-
-`hug.output_format.json`: The default hug output formatter for all endpoints, outputs in Javascript Serialized Object Notation (JSON)
51
-
-`hug.output_format.text`: Outputs in a plain text format
52
-
-`hug.output_format.html`: Outputs Hyper Text Markup Language (HTML)
53
-
-`hug.output_format.json_camelcase`: Outputs in the JSON format but first converts all keys to camelCase to better conform to Javascript coding standards.
54
-
-`hug.output_format.pretty_json`: Outputs in the JSON format, with extra white-space to improve human readability
55
-
-`hug.output_format.image(format)`: Outputs an image (of the specified format)
50
+
-`hug.output_format.json`: The default hug output formatter for all endpoints; outputs in Javascript Serialized Object Notation (JSON).
51
+
-`hug.output_format.text`: Outputs in a plain text format.
52
+
-`hug.output_format.html`: Outputs Hyper Text Markup Language (HTML).
53
+
-`hug.output_format.json_camelcase`: Outputs in the JSON format, but first converts all keys to camelCase to better conform to Javascript coding standards.
54
+
-`hug.output_format.pretty_json`: Outputs in the JSON format, with extra whitespace to improve human readability.
55
+
-`hug.output_format.image(format)`: Outputs an image (of the specified format).
56
56
- There are convience calls in the form `hug.output_format.{FORMAT}_image for the following image types: 'png', 'jpg', 'bmp', 'eps', 'gif', 'im', 'jpeg', 'msp', 'pcx', 'ppm', 'spider', 'tiff', 'webp', 'xbm',
Automatically works on returned file names, streams, or objects that produce an image on read, save, or render
59
+
Automatically works on returned file names, streams, or objects that produce an image on read, save, or render.
60
60
61
-
-`hug.output_format.video(video_type, video_mime, doc)`: Streams a video back to the user in the specified format
61
+
-`hug.output_format.video(video_type, video_mime, doc)`: Streams a video back to the user in the specified format.
62
62
- There are convience calls in the form `hug.output_format.{FORMAT}_video for the following video types: 'flv', 'mp4', 'm3u8', 'ts', '3gp', 'mov', 'avi', and 'wmv'.
63
-
Automatically works on returned file names, streams, or objects that produce a video on read, save, or render
63
+
Automatically works on returned file names, streams, or objects that produce a video on read, save, or render.
64
64
65
-
-`hug.output_format.file`: Will dynamically determine and stream a file based on its content. Automatically works on returned file names and streams
65
+
-`hug.output_format.file`: Will dynamically determine and stream a file based on its content. Automatically works on returned file names and streams.
66
66
67
-
-`hug.output_format.on_content_type(handlers={content_type: output_format}, default=None)`: Dynamically changes the output format based on the request content type
68
-
-`hug.output_format.suffix(handlers={suffix: output_format}, default=None)`: Dynamically changes the output format based on a suffix at the end of the requested path
69
-
-`hug.output_format.prefix(handlers={suffix: output_format}, defualt=None)`: Dynamically changes the output format based on a prefix at the begining of the requested path
67
+
-`hug.output_format.on_content_type(handlers={content_type: output_format}, default=None)`: Dynamically changes the output format based on the request content type.
68
+
-`hug.output_format.suffix(handlers={suffix: output_format}, default=None)`: Dynamically changes the output format based on a suffix at the end of the requested path.
69
+
-`hug.output_format.prefix(handlers={suffix: output_format}, defualt=None)`: Dynamically changes the output format based on a prefix at the begining of the requested path.
70
70
71
71
Creating a custom output format
72
72
===================
@@ -77,7 +77,7 @@ An output format is simply a function with a content type attached that takes a
A common pattern is to only apply the output format validation errors aren't passed in since it's hard to deal with this for several formats (such as images) and it may make more sense to simply return the error as JSON. hug makes this pattern simple as well with the `hug.output_format.on_valid` decorator:
80
+
A common pattern is to only apply the output format. Validation errors aren't passed in, since it's hard to deal with this for several formats (such as images), and it may make more sense to simply return the error as JSON. hug makes this pattern simple, as well, with the `hug.output_format.on_valid` decorator:
0 commit comments