API¶
application Module¶
configuration Module¶
contexts Module¶
Context¶
-
class
nanohttp.contexts.
Context
(environ, application=None)[source]¶ Bases:
object
A Global context for Request and Response.
- Context are initialized and entering on every request (referring to
nanohttp application lifecycle).
Context also supports to use stack nested (>=0.16.6), its useful on testing.
def sample: return context.query['weather'] with Context({'QUERY_STRING': 'weather=Sunny'}): assert sample() == 'Sunny with Context({'QUERY_STRING': 'weather=Rainy'}): assert sample() == 'Rainy'
-
application
= None¶ Current
Application
instance
Cookies
-
form
()[source]¶ Request form values
Note
if using multipart/form-data uploaded file will reproduce as
cgi.FieldStorage
.
-
classmethod
get_current
() → nanohttp.contexts.Context[source]¶ Get current context
Not initialized context raises
ContextIsNotInitializedError
,
-
property
response_content_type
¶ Response content type property
-
response_encoding
= None¶ Response encoding
-
thread_local
= <_thread._local object>¶ Thread local variable contexts stored in
controllers Module¶
RestController¶
-
class
nanohttp.controllers.
RestController
[source]¶ Bases:
nanohttp.controllers.Controller
HTTP method oriented controller
Static¶
-
class
nanohttp.controllers.
Static
(directory='.', default_document='index.html')[source]¶ Bases:
nanohttp.controllers.Controller
Serves static files
RegexRouteController¶
-
class
nanohttp.controllers.
RegexRouteController
(routes)[source]¶ Bases:
nanohttp.controllers.Controller
This is how to use it:
class Root(RegexRouteController): def __init__(self): super().__init__(( (r'/installations/(?P<installation_id>\d+)/access_tokens', self.access_tokens), )) @json def access_tokens(self, installation_id: int): return dict(installationId=installation_id)
decorators Module¶
-
nanohttp.decorators.
action
(*args, verbs='any', encoding='utf-8', content_type=None, inner_decorator=None, prevent_empty_form=None, prevent_form=None, form_whitelist=None, **kwargs)[source]¶ Base action decorator
Marks the function as a nanohttp handler/action.
- Parameters
verbs – Allowed HTTP methods as list or tuple of strings.
encoding – Content encoding
content_type – Response Content Type
inner_decorator – Inner decorator, to put it between this decorator and the handler.
prevent_empty_form – Boolean or str, indicates to prevent empty HTTP form. if str given, a
HTTPStatus(<str>)
will be raised. otherwiseHTTPBadRequest
.prevent_form – Boolean or str, indicates to prevent any HTTP form. if str given, a
HTTPStatus(<str>)
will be raised, otherwiseHTTPBadRequest
.form_whitelist – A list of allowed form fields. or a tuple(list, httpstatus)
-
nanohttp.decorators.
binary
(*args, verbs='any', encoding=None, content_type='application/octet-stream', inner_decorator=None, prevent_empty_form=None, prevent_form=None, form_whitelist=None, **kwargs)¶ Binary-data action decorator
-
nanohttp.decorators.
chunked
(trailer_field=None, trailer_value=None)[source]¶ Enables chunked encoding on an action
-
nanohttp.decorators.
html
(*args, verbs='any', encoding='utf-8', content_type='text/html', inner_decorator=None, prevent_empty_form=None, prevent_form=None, form_whitelist=None, **kwargs)¶ HTML action decorator
-
nanohttp.decorators.
json
(*args, verbs='any', encoding='utf-8', content_type='application/json', inner_decorator=<function jsonify>, prevent_empty_form=None, prevent_form=None, form_whitelist=None, **kwargs)¶ JSON action decorator
accepts list, dict, int, str or objects have
to_dict
method.
-
nanohttp.decorators.
text
(*args, verbs='any', encoding='utf-8', content_type='text/plain', inner_decorator=None, prevent_empty_form=None, prevent_form=None, form_whitelist=None, **kwargs)¶ Plain Text action decorator
-
nanohttp.decorators.
xml
(*args, verbs='any', encoding='utf-8', content_type='application/xml', inner_decorator=None, prevent_empty_form=None, prevent_form=None, form_whitelist=None, **kwargs)¶ XML action decorator
validations Module¶
-
nanohttp.validation.
validate
(**fields)[source]¶ Decorator to validate HTTP Forms and query string.
@validate(field1=dict(required=True)) def index(self, *, field1): ...
Available parameters for validation is listed below:
- Parameters
required – Boolean or str, indicates the field is required.
not_none – Boolean or str, Raise when field is given and it’s value is None.
type – A callable to pass the received value to it as the only argument and get it in the apprpriate type, Both
ValueError
and TypeError` may be raised if the value cannot casted to the specified type. A good example of this callable would be theint
.minimum – Numeric, Minimum allowed value.
maximum – Numeric, Maximum allowed value.
pattern – Regex pattern to match the value.
min_length – Only for strings, the minumum allowed length of the value.
max_length – Only for strings, the maximum allowed length of the value.
callback – A
callable(value, container, field: Field)
to be called while validating the field.
A detailed example:
my_validator = validate( title=dict( required='710 Title not in form', max_length=(50, '704 At most 50 characters are valid for title') ), description=dict( max_length=( 512, '703 At most 512 characters are valid for description' ) ), dueDate=dict( pattern=(DATE_PATTERN, '701 Invalid due date format'), required='711 Due date not in form' ), cutoff=dict( pattern=(DATE_PATTERN, '702 Invalid cutoff format'), required='712 Cutoff not in form' ), ) @json @my_validator def index(self): ...
Field¶
-
class
nanohttp.validation.
Field
(title, form=True, query=False, required=None, type_=None, minimum=None, maximum=None, pattern=None, min_length=None, max_length=None, callback=None, not_none=None, readonly=None)[source]¶ Bases:
object
-
class
nanohttp.validation.
RequiredValidator
(expression)[source]¶ Bases:
nanohttp.validation.FlagCriterion
-
class
nanohttp.validation.
NotNoneValidator
(expression)[source]¶ Bases:
nanohttp.validation.FlagCriterion
exceptions Module¶
-
exception
nanohttp.exceptions.
HTTPRedirect
(location, *args, **kw)[source]¶ Bases:
nanohttp.exceptions.HTTPKnownStatus
This is an abstract class for all redirects.
-
exception
nanohttp.exceptions.
HTTPSuccess
(status_text=None)[source]¶ Bases:
nanohttp.exceptions.KeepResponseHeadersMixin
,nanohttp.exceptions.HTTPKnownStatus