starlette_jsonapi.utils module
- starlette_jsonapi.utils.serialize_error(exc)
Serializes exception according to the json:api spec and returns the equivalent
JSONAPIResponse
.- Parameters
exc (Exception) –
- Return type
- starlette_jsonapi.utils.register_jsonapi_exception_handlers(app)
Registers exception handlers on a Starlette app, serializing uncaught Exception and HTTPException to a json:api compliant body.
- Parameters
app (Starlette) –
- starlette_jsonapi.utils.parse_included_params(request)
Parses a request’s
include
query parameter, if present, and returns a sequence of included relations.Example:
# request URL /some-resource/?include=foo,foo.bar assert parse_included_params(request) == {'foo', 'foo.bar'}
- Parameters
request (Request) –
- Return type
Optional[Set[str]]
- starlette_jsonapi.utils.parse_sparse_fields_params(request)
Parses a request’s
fields
query parameter, if present, and returns a dictionary of resource type -> sparse fields.Example:
# request URL: /articles/?fields[articles]=title,content assert parse_sparse_fields_params(request) == {'articles': ['title', 'content']}
- Parameters
request (Request) –
- Return type
Dict[str, List[str]]
- starlette_jsonapi.utils.filter_sparse_fields(item, sparse_fields)
Given a dictionary with the json:api representation of an item, drops any attributes or relationships that are not found in
sparse_fields
and returns a new dictionary.For detailed information, check the json:api spec.
- Parameters
item (dict) –
sparse_fields (List[str]) –
- Return type
dict
- starlette_jsonapi.utils.process_sparse_fields(serialized_data, many=False, sparse_fields=None)
Processes sparse fields requests by removing extra attributes and relationships from the final serialized data.
If a client does not specify the set of fields for a given resource type, all fields will be included.
Consult the json:api spec for more information.
- Parameters
serialized_data (dict) –
many (bool) –
sparse_fields (Optional[dict]) –
- Return type
dict
- starlette_jsonapi.utils.prefix_url_path(app, path, **kwargs)
Prefixes all URLs generated by the framework with the value of
app.url_prefix
, if set. Can be used to generate absolute links.- Parameters
app (Starlette) –
path (str) –
- starlette_jsonapi.utils.safe_merge(first, second)
Safe merge two dictionaries, returning a deepcopy of first with second merged into it.
- Parameters
first (Union[dict, list]) –
second (Union[dict, list]) –
- starlette_jsonapi.utils.merge(a, b, path=None)
Merges b into a, mainly: https://stackoverflow.com/questions/7204805/how-to-merge-dictionaries-of-dictionaries/7205107#7205107
- starlette_jsonapi.utils.isinstance_or_subclass(inst, cls)
Utility for safely checking isinstance and issubclass.