Beispiel #1
0
def get_member_identity(member):
    """Return member identity.

    Expected JSON document structured like this:

    {
        "_links": {
            "self": "/path/to/member"
        }
    }

    :param member: JSON document containing collection member
    :returns: Member document location
    """
    path = member.get('_links')
    if not path:
        raise exceptions.MissingAttributeError(attribute='_links',
                                               resource=member)

    path = path.get('self')
    if not path:
        raise exceptions.MissingAttributeError(attribute='_links',
                                               resource=member)

    return path.rstrip('/')
Beispiel #2
0
def get_sub_resource_path_by(resource, subresource_name):
    """Find subresource path.

    :param resource: Resource instance on which the name
        gets queried upon.
    :param subresource_name: name of the resource attribute.
    :returns: Resource path.
    """
    if not subresource_name:
        raise exceptions.InvalidInputError(error='subresource cannot be empty')

    if not isinstance(subresource_name, list):
        subresource_name = [subresource_name]

    body = resource.json
    for path_item in subresource_name:
        body = body.get(path_item, {})

    if not body:
        raise exceptions.MissingAttributeError(
            attribute='/'.join(subresource_name), resource=resource.path)

    try:
        return get_member_identity(body)

    except (TypeError, KeyError):
        attribute = '/'.join(subresource_name)
        raise exceptions.MissingAttributeError(attribute=attribute,
                                               resource=resource.path)
Beispiel #3
0
    def _load(self, body, resource, nested_in=None):
        """Load a field from a JSON object.

        :param body: parsed JSON body.
        :param resource: `Resource` instance for which the field is
            loaded.
        :param nested_in: parent resource path.
        :raises: MissingAttributeError if a required field is missing.
        :raises: MalformedAttributeError on invalid field value or type.
        :returns: field value.
        """
        name = self._path[-1]
        for path_item in self._path[:-1]:
            body = body.get(path_item, {})

        try:
            item = body[name]

        except KeyError:
            if self._required:
                path = (nested_in or []) + self._path
                raise exceptions.MissingAttributeError(
                    attribute='/'.join(path), resource=resource.path)
            else:
                return self._default

        if self._converter is None:
            return item

        try:
            return self._converter(item)

        except Exception as exc:
            path = (nested_in or []) + self._path
            raise exceptions.MalformedAttributeError(attribute='/'.join(path),
                                                     resource=resource.path,
                                                     error=exc)