py2http.default_configs¶
This module provides default configurations, input mappers, output mappers, and error handlers for an HTTP service implemented in Python using the py2http library. It includes functions for handling JSON requests and responses, as well as mapping errors to appropriate HTTP responses based on different HTTP frameworks (AIOHTTP, BOTTLE, FLASK).
The module also defines a set of default configuration values for setting up the HTTP service, such as the application name, framework to use, port number, error handling strategy, and CORS settings. It provides flexibility to customize these configurations based on the specific requirements of the project.
Overall, this module serves as a foundational component for building and setting up an HTTP service in Python using the py2http library.
Functions
|
|
|
|
|
|
|
|
|
|
|
The HTTP status for an error a handler raised (500 when it is not a client's). |
|
The |
|
The default output mapper of a Flask app: the output as a JSON response. |
|
|
|
Make a Flask view's response of what an output mapper returns. |
|
The error handler and output mapper to use under |
The |
- py2http.default_configs.error_status(error)[source]¶
The HTTP status for an error a handler raised (500 when it is not a client’s).
>>> error_status(AuthorizationError()), error_status(ForbiddenError()) (401, 403) >>> error_status(InputError()), error_status(KeyError()) (400, 500)- Return type:
- py2http.default_configs.flask_error_handler(error)[source]¶
The
(body, status, headers)a Flask view returns for an error, mapped like the bottle handler (OPAQUE_ERRORShides the text of a 500).
- py2http.default_configs.flask_json_output_mapper(output, **inputs)[source]¶
The default output mapper of a Flask app: the output as a JSON response.
- py2http.default_configs.flask_response_mapper(output_mapper)[source]¶
Make a Flask view’s response of what an output mapper returns.
The
send_*_respdecorators set Bottle’sresponse.content_typeand return the body (strorbytes); Flask ignores that and would answertext/html. A mapper that declares acontent_typeand returns astr/bytesbody, or a(body, status[, headers])tuple of one, gets that content type (unless the tuple’s headers set their own); anasyncmapper is awaited first. Anything else (aResponse, a dict) is kept as is.>>> from py2http.decorators import send_raw_resp # (needs flask) >>> flask_response_mapper(send_raw_resp(lambda o: o))(b"x").content_type 'text/plain'
- py2http.default_configs.framework_defaults(framework, *, error_handler, output_mapper)[source]¶
The error handler and output mapper to use under
framework: the given ones, except that the defaults (which only know of Bottle and AIOHTTP) are swapped for the framework’s own, so that no process-wide state decides how a response is made.>>> framework_defaults(FLASK, error_handler=default_error_handler, ... output_mapper=default_output_mapper)[0] is flask_error_handler True
- py2http.default_configs.www_authenticate_challenge()[source]¶
The
WWW-Authenticatevalue of a 401 a dispatched function raised: thePY2HTTP_WWW_AUTHENTICATEenvironment variable, elseBearer(also when the variable holds a line break, which would make every 401 fail to send).>>> os.environ.pop(WWW_AUTHENTICATE_ENV_VAR, None) and None >>> www_authenticate_challenge() 'Bearer'- Return type: