pyneric.rest_requests

The pyneric.rest_requests module contains REST resource classes.

class pyneric.rest_requests.RestResource(container)[source]

Bases: object

A standard REST resource.

A REST resource is represented by a (usually HTTP) URL, which is specified in this class via the combination of the container passed to the constructor and the url_path. See the attribute documentation for more details.

url_path = None

Path segment(s) identifying the REST resource.

This may be None to signify that this is an abstract resource; otherwise, it is the path under the base (API root or containing resource) identifying this resource, which will be automatically URL-quoted (except for path separators) when it is included in a URL produced by the library.

This may contain path separator(s) (“/”) if there is no need to access the path segments as distinct REST resources.

This cannot start with a path separator, but it may end with one if this and resources under this one (i.e., those that use this one as container) shall each have a trailing path separator. If the container passed to the constructor is a URL string with a trailing slash or a RestResource with a url_path ending with a path separator, then it is not significant whether this value has a trailing path separator, since all resources under that container are represented with a trailing path separator.

container_class = None

The parent REST resource that contains this resource.

The container passed to the constructor must be an instance of this resource. If this is None, then the container passed to the constructor must be a URL string under which this resource resides.

An attribute named after this class or explicitly named by this class is created to reference the instance passed to the constructor as container.

container_is_collection = False

Whether the containing resource is the specified collection.

This only applies when the container_class is a subclass of RestCollection; it is simply a convenience for automatically confirming the resource’s validity within the REST API.

If this is false, then this resource exists under each member of the collection; otherwise, it exists directly under the collection itself.

reference_attribute = None

The attribute name used to refer to this resource.

For example, this applies when another resource refers to this one as the container.

If this is None (the default), then the attribute used to refer to this resource (as container) is the class name converted to lower-case and underscored (with additional underscore(s) appended when it would conflict with existing attributes in the referring resource).

classmethod from_url(url)[source]

Construct an instance of this resource based on the given URL.

container

The container of this resource.

This is an instance of container_class if that is not None; otherwise, this is the URL under which this resource resides.

Whether the container has a trailing slash determines whether the resource’s URL includes a trailing slash.

url

The complete URL of the resource.

is_abstract = True
class pyneric.rest_requests.RestCollection(container, id=None)[source]

Bases: pyneric.rest_requests.RestResource

A standard REST collection.

This is a special type of resource in REST where a set of usually homogeneous, but at least related, resources are contained within a collection. The collection is represented by the url_path (usually a plural noun) and each member of the collection is represented by a unique identifier under the collection in the URL path. For example, a collection called “resources” might have individual members of the collection represented by “resources/1” and “resources/2”. In this case, “resources” would be the url_path, and “1” and “2” would be the values for id.

An instance will represent either the collection or a member of the collection, depending on the id argument passed to the constructor.

container_class = None
container_is_collection = False
is_abstract = True
reference_attribute = None
url_path = None
id_type

The type of the id attribute.

The id_type is str by default, since that is how it is represented in the resource URL, but it can be set to another type if the id attribute should be accepted and presented differently from its string representation.

alias of str

classmethod validate_id(value)[source]

Validate the given value as a valid identifier for this resource.

id

The identifier of the member within the REST collection.

This is None if this instance represents the entire collection.

The value provided to the constructor must be one of:

In the last case, the object that results from the instantiation becomes the value of this property.

Like url_path, when this value is included in a URL produced by the library, it is automatically cast to a string and URL-quoted, except that path separators (slashes) in id are also quoted.