cowbird.api.generic

Attributes

JSON

AnyResponseType

CONTENT_TYPE_ANY

CONTENT_TYPE_JSON

FORMAT_TYPE_MAPPING

SUPPORTED_ACCEPT_TYPES

LOGGER

Classes

RemoveSlashNotFoundViewFactory

Utility that will try to resolve a path without appended slash if one was provided.

Functions

get_header(→ Optional[str])

Retrieves header_name by fuzzy match (independently of upper/lower-case and underscore/dash) from various

get_logger(→ logging.Logger)

Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when

internal_server_error(...)

Overrides default HTTP.

not_found_or_method_not_allowed(...)

Overrides the default HTTPNotFound [404] by appropriate HTTPMethodNotAllowed [405] when applicable.

guess_target_format(→ Tuple[str, bool])

Guess the best applicable response Content-Type header according to request Accept header and format

validate_accept_header_tween(...)

Tween that validates that the specified request Accept header or format query (if any) is supported by the

apply_response_format_tween(...)

Tween that obtains the request Accept header or format query (if any) to generate the response with the

get_exception_info(→ cowbird.typedefs.JSON)

Obtains additional exception content details about the response according to available information.

get_request_info(→ cowbird.typedefs.JSON)

Obtains additional content details about the request according to available information.

Module Contents

cowbird.api.generic.JSON[source]
cowbird.api.generic.AnyResponseType[source]
cowbird.api.generic.CONTENT_TYPE_ANY = '*/*'[source]
cowbird.api.generic.CONTENT_TYPE_JSON = 'application/json'[source]
cowbird.api.generic.FORMAT_TYPE_MAPPING[source]
cowbird.api.generic.SUPPORTED_ACCEPT_TYPES[source]
cowbird.api.generic.get_header(header_name: str, header_container: cowbird.typedefs.AnyHeadersType, default: str | None = None, split: str | List[str] | None = None) str | None[source]

Retrieves header_name by fuzzy match (independently of upper/lower-case and underscore/dash) from various framework implementations of Headers.

If split is specified, the matched header_name is first split with it and the first item is returned. This allows to parse complex headers (e.g.: text/plain; charset=UTF-8 to text/plain with split=';').

Parameters:
  • header_name – header to find.

  • header_container – where to look for header_name.

  • default – value to returned if header_container is invalid or header_name could not be found.

  • split – character(s) to use to split the found header_name.

cowbird.api.generic.get_logger(name: str, level: int | None = None, force_stdout: bool = None, message_format: str | None = None, datetime_format: str | None = None) logging.Logger[source]

Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when level is logging.NOTSET.

cowbird.api.generic.LOGGER[source]
class cowbird.api.generic.RemoveSlashNotFoundViewFactory(notfound_view: Callable[[pyramid.request.Request], cowbird.typedefs.AnyResponseType] | None = None)[source]

Bases: object

Utility that will try to resolve a path without appended slash if one was provided.

__call__(request: pyramid.request.Request) cowbird.typedefs.AnyResponseType[source]
cowbird.api.generic.internal_server_error(request: pyramid.request.Request) pyramid.httpexceptions.HTTPException[source]

Overrides default HTTP.

cowbird.api.generic.not_found_or_method_not_allowed(request: pyramid.request.Request) pyramid.httpexceptions.HTTPException[source]

Overrides the default HTTPNotFound [404] by appropriate HTTPMethodNotAllowed [405] when applicable.

Not found response can correspond to underlying process operation not finding a required item, or a completely unknown route (path did not match any existing API definition). Method not allowed is more specific to the case where the path matches an existing API route, but the specific request method (GET, POST, etc.) is not allowed on this path.

Without this fix, both situations return [404] regardless.

cowbird.api.generic.guess_target_format(request: pyramid.request.Request) Tuple[str, bool][source]

Guess the best applicable response Content-Type header according to request Accept header and format query, or defaulting to CONTENT_TYPE_JSON.

Returns:

tuple of matched MIME-type and where it was found (True: header, False: query)

cowbird.api.generic.validate_accept_header_tween(handler: Callable[[pyramid.request.Request], pyramid.response.Response], registry: pyramid.registry.Registry) Callable[[pyramid.request.Request], pyramid.response.Response][source]

Tween that validates that the specified request Accept header or format query (if any) is supported by the application and for the given context.

Raises:

HTTPNotAcceptable – if desired Content-Type is not supported.

cowbird.api.generic.apply_response_format_tween(handler: Callable[[pyramid.request.Request], pyramid.httpexceptions.HTTPException], registry: pyramid.registry.Registry) Callable[[pyramid.request.Request], pyramid.response.Response][source]

Tween that obtains the request Accept header or format query (if any) to generate the response with the desired Content-Type.

The target Content-Type is expected to have been validated by validate_accept_header_tween() beforehand to handle not-acceptable errors.

The tween also ensures that additional request metadata extracted from get_request_info() is applied to the response body if not already provided by a previous operation.

cowbird.api.generic.get_exception_info(response: pyramid.httpexceptions.HTTPException | pyramid.request.Request | pyramid.response.Response, content: cowbird.typedefs.JSON | None = None, exception_details: bool = False) cowbird.typedefs.JSON[source]

Obtains additional exception content details about the response according to available information.

cowbird.api.generic.get_request_info(request: pyramid.request.Request | pyramid.httpexceptions.HTTPException, default_message: str | None = None, exception_details: bool = False) cowbird.typedefs.JSON[source]

Obtains additional content details about the request according to available information.