magpie.ui.utils
¶
Module Contents¶
Functions¶
|
|
|
Use a pyramid sub-request to request Magpie API routes via the UI. This avoids max retries and closed connections |
|
Redirects the contents to be rendered by the UI ‘error’ page. |
|
Decorator that encapsulates the operation in a try/except block, and redirects the response to the UI error page |
-
magpie.ui.utils.
check_response
(response: magpie.typedefs.AnyResponseType) → magpie.typedefs.AnyResponseType[source]¶ - Returns
response if the HTTP status code is successful.
- Raises
HTTPError – (of appropriate type) if the response corresponds to an HTTP error code
-
magpie.ui.utils.
request_api
(request: pyramid.request.Request, path: magpie.typedefs.Str, method: magpie.typedefs.Str = 'GET', data: Optional[Union[JSON, Str]] = None, headers: Optional[HeadersType] = None, cookies: Optional[CookiesType] = None) → magpie.typedefs.AnyResponseType[source]¶ Use a pyramid sub-request to request Magpie API routes via the UI. This avoids max retries and closed connections when using 1 worker (eg: during tests).
Some information is retrieved from
request
to pass down to the sub-request (eg: cookies). If they are passed as argument, corresponding values will override the ones found inrequest
.All sub-requests to the API are assumed to be
magpie.common.CONTENT_TYPE_JSON
unless explicitly overridden withheaders
. Headers are also looked for for additionalSet-Cookie
header in case they need to be passed down tocookies
.- Parameters
request – incoming Magpie UI request that requires sub-request to Magpie API, to retrieve required details.
path – local Magpie API path (relative to root without URL).
method – HTTP method to send the API sub-request.
data – JSON dictionary or literal string content of the request body.
headers – override headers to employ for the API sub-request. Defaults to JSON Accept & Content-Type headers.
cookies – override cookies to employ for the API sub-request. Defaults to current logged user.
-
magpie.ui.utils.
redirect_error
(request: pyramid.request.Request, code: int = None, content: Optional[JSON] = None) → magpie.typedefs.AnyResponseType[source]¶ Redirects the contents to be rendered by the UI ‘error’ page.
- Parameters
request – incoming request that resulted into some kind of error.
code – explicit HTTP status code for the error response, extracted from contents if otherwise available.
content – any body content provided as error response from the API.
-
magpie.ui.utils.
handle_errors
(func)[source]¶ Decorator that encapsulates the operation in a try/except block, and redirects the response to the UI error page with API error contents.
In worst case scenario where the operation cannot figure out what to do with the exception response, raise the most basic
HTTPInternalServerError
that can be formulated from available details.See also