djblets.webapi.resources.root¶
A flexible resource for the root of your API resource tree.
- URITemplates¶
A type alias for a mapping of URI templates.
Each key is a URI template name, and each value is a URL.
New in version 3.2.
- class RootResource(child_resources=[], include_uri_templates=True)[source]¶
Bases:
WebAPIResource
The root of a resource tree.
This is meant to be instantiated with a list of immediate child resources. The result of
get_url_patterns()
should be included in a project’surls.py
.- singleton: bool = True[source]¶
Whether the resource is a singleton.
Singleton resources behave like an item resource without a parent list resource. They have a single endpoint.
- Type:
- class ResourceEntry(name, list_href, resource, is_list, uri_template_name)[source]¶
Bases:
tuple
Whether or not the resource is a list resource.
- uri_template_name (str)
The name of the resource to use in the URI templates list.
- __init__(child_resources=[], include_uri_templates=True)[source]¶
Initialize the API resource.
This will register the resource and set up state required for processing API requests.
- get_etag(request, obj, *args, **kwargs)[source]¶
Return the ETag representing the state of the object.
By default, this uses
etag_field
to determine what field in the model is unique enough to represent the state of the object.Subclasses can override this for more complex behavior. Any overridden functions should make sure to pass the result through
encode_etag()
before returning a value.- Parameters:
request (
django.http.HttpRequest
) – The HTTP request from the client.obj (
object
) – The object for which to return an ETag.*args (
tuple
) – Additional positional arguments passed to the view.**kwargs (
dict
) – Keyword arguments representing values captured from the URL.
- Returns:
The resulting encoded ETag, or
None
if one could not be generated.- Return type:
- serialize_root(request, *args, **kwargs)[source]¶
Serialize the contents of the root resource.
By default, this just provides links and URI templates. Subclasses can override this to provide additional data, or to otherwise change the structure of the root resource.
- get_uri_templates(request: HttpRequest, *args, **kwargs) Dict[str, str] [source]¶
Return all URI templates in the resource tree.
REST APIs can be very chatty if a client wants to be well-behaved and crawl the resource tree asking for the links, instead of hard-coding the paths. The benefit is that they can keep from breaking when paths change. The downside is that it can take many HTTP requests to get the right resource.
This list of all URI templates allows clients who know the resource name and the data they care about to simply plug them into the URI template instead of trying to crawl over the whole tree. This can make things far more efficient.
- Parameters:
request (
django.http.HttpRequest
) – The GET request for the Root resource.*args (
tuple
, unused) – Additional unused arguments.**kwargs (
dict
, unused) – Additional unused keyword arguments.
- Returns:
A mapping of resources to their URI templates.
- Return type:
- build_uri_templates(base_href: str) Dict[str, str] [source]¶
Build the URI templates to include in the payload.
The resource will cache these automatically in local memory, so that repeated calls will often be unnecessary. This allows for things like per-organization APIs that each have their own cache.
Caches are automatically invalidated as needed.
New in version 3.2.
- classmethod walk_resources(resource, list_href)[source]¶
Yield all URI endpoints associated with a specified resource.
- Parameters:
resource (
djblets.webapi.resources.WebAPIResource
) – The starting point for searching the resource tree.list_href (
unicode
) – The path to the list resource, relative to the WebAPIResource provided. Used as a component of the URL in the API.
- Yields:
RootResource.ResourceEntry
– Resource entries for all sub-resources.
- api_404_handler(request, api_format=None, *args, **kwargs)[source]¶
Default handler at the end of the URL patterns.
This returns an API 404, instead of a normal django 404.
- get_url_patterns()[source]¶
Return the Django URL patterns for this object and its children.
This returns the same list as
WebAPIResource.get_url_patterns()
, but also introduces a generic catch-all 404 handler which returns API errors instead of HTML.
- register_uri_template(name: str, relative_path: str, relative_resource: Optional[WebAPIResource] = None) None [source]¶
Register the specified resource for URI template serialization.
This adds the specified name and relative resource to the Root Resource’s URI templates.
- Parameters:
name (
str
) – The name of the associated resource being added to templates.relative_path (
str
) – The path of the API resource relative to its parent resources.relative_resource (
djblets.webapi.resources.base.WebAPIResource
, optional) – The resource instance associated with this URI template.
- __annotations__ = {'_parent_resource': 'Optional[WebAPIResource]', '_prefetch_related_fields': 'List[str]', '_registered_uri_templates': '_URITemplatesByResourceMap', '_select_related_fields': 'List[str]', 'allowed_methods': 'Sequence[str]', 'allowed_mimetypes': 'Sequence[AllowedMimetypeEntry]', 'autogenerate_etags': 'bool', 'etag_field': 'Optional[str]', 'fields': 'Mapping[str, Union[WebAPIResourceFieldInfo, Mapping[str, Any]]]', 'is_webapi_handler': 'Final[bool]', 'item_child_resources': 'Sequence[WebAPIResource]', 'last_modified_field': 'Optional[str]', 'list_child_resources': 'Sequence[WebAPIResource]', 'method_mapping': 'Mapping[str, str]', 'mimetype_item_resource_name': 'Optional[str]', 'mimetype_list_resource_name': 'Optional[str]', 'mimetype_vendor': 'Optional[str]', 'model': 'Optional[Type[Model]]', 'model_object_key': 'str', 'model_parent_key': 'Optional[str]', 'paginated_cls': 'Type[WebAPIResponsePaginated]', 'singleton': 'bool', 'uri_object_key': 'Optional[str]', 'uri_object_key_regex': 'str'}¶
- unregister_uri_template(name: str, relative_resource: Optional[WebAPIResource] = None) None [source]¶
Unregister the specified resource for URI template serialization.
This removes the specified name and relative resource to the Root Resource’s URI templates.
- Parameters:
name (
str
) – The name of the resource being removed from templates.relative_resource (
djblets.webapi.resources.base.WebAPIResource
, optional) – The resource instance associated with this URI template.