Jump to >

djblets.webapi.resources.root

A flexible resource for the root of your API resource tree.

class RootResource(child_resources=[], include_uri_templates=True)[source]

Bases: djblets.webapi.resources.base.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’s urls.py.

name = u'root'[source]
singleton = True[source]
class ResourceEntry(name, list_href, resource, is_list)[source]

Bases: tuple

Whether or not the resource is a list resource.

__getnewargs__()[source]

Return self as a plain tuple. Used by copy and pickle.

__getstate__()[source]

Exclude the OrderedDict from pickling

static __new__(_cls, name, list_href, resource, is_list)[source]

Create new instance of ResourceEntry(name, list_href, resource, is_list)

__repr__()[source]

Return a nicely formatted representation string

__slots__ = ()[source]
is_list[source]

Alias for field number 3

list_href[source]

Alias for field number 1

name[source]

Alias for field number 0

resource[source]

Alias for field number 2

__init__(child_resources=[], include_uri_templates=True)[source]

x.__init__(…) initializes x; see help(type(x)) for signature

get_etag(request, obj, *args, **kwargs)[source]
get(request, *args, **kwargs)[source]

Retrieve the list of top-level resources and URL templates.

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, *args, **kwargs)[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.

classmethod walk_resources(resource, list_href)[source]

Yield all URI endpoints associated with a specified resource.

Parameters:
  • djblets.webapi.resources.WebAPIResource (resource) – 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, relative_path, relative_resource=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 (unicode) – The name of the associated resource being added to templates.
  • relative_path (unicode) – The path of the API resource relative to its parent resources.
  • relative_resource (djblets.extensions.resources.ExtensionResource, optional) – The resource instance associated with this URI template.
unregister_uri_template(name, relative_resource=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: