Exemplo n.º 1
0
 async def serialize(self, data: Any, many=False) -> JSONAPIResponse:
     """
     Serializes relationship for an object represented by the parent resource.
     """
     relationship = self._get_relationship_field()
     body = relationship.serialize(self.relationship_name, data)
     return JSONAPIResponse(
         content=body,
     )
Exemplo n.º 2
0
 async def to_response(self, data: dict, *args, **kwargs) -> JSONAPIResponse:
     """
     Wraps `data` in a JSONAPIResponse object and returns it.
     Additional args and kwargs are passed to the `starlette` based Response.
     """
     return JSONAPIResponse(
         content=data,
         *args, **kwargs,
     )
Exemplo n.º 3
0
 async def serialize(self, data: Any, many=False) -> JSONAPIResponse:
     """ Serializes data as a JSON:API payload and returns a JSONAPIResponse which can be served to clients. """
     included_relations = await self._prepare_included(data=data, many=many)
     schema = self.schema(app=self.request.app, include_data=included_relations)
     body = schema.dump(data, many=many)
     sparse_body = await self.process_sparse_fields(body, many=many)
     return JSONAPIResponse(
         content=sparse_body,
     )
Exemplo n.º 4
0
    async def delete(self, id=None, *args, **kwargs) -> Response:
        if not id:
            raise TeamNotFound
        team = Team.get_item(id)
        if not team:
            raise TeamNotFound

        team.delete()

        return JSONAPIResponse(status_code=204)
Exemplo n.º 5
0
    async def delete(self, id=None, *args, **kwargs) -> Response:
        if not id:
            raise UserNotFound
        user = User.get_item(id)
        if not user:
            raise UserNotFound

        user.delete()

        return JSONAPIResponse(status_code=204)
Exemplo n.º 6
0
    async def delete(self, id=None, *args, **kwargs) -> Response:
        if not id:
            raise OrganizationNotFound
        organization = Organization.get_item(id)
        if not organization:
            raise OrganizationNotFound

        organization.delete()

        return JSONAPIResponse(status_code=204)
Exemplo n.º 7
0
    async def delete(self, id=None, *args, **kwargs) -> Response:
        if not id:
            raise TeamNotFound
        try:
            team = await Team.get(id=id)
        except DoesNotExist:
            raise TeamNotFound

        await team.delete()

        return JSONAPIResponse(status_code=204)
Exemplo n.º 8
0
    async def delete(self, id=None, *args, **kwargs) -> Response:
        if not id:
            raise OrganizationNotFound
        try:
            organization = self.db_session.query(Organization).filter_by(id=id).one()
        except NoResultFound:
            raise OrganizationNotFound

        self.db_session.delete(organization)
        self.db_session.commit()

        return JSONAPIResponse(status_code=204)
Exemplo n.º 9
0
    async def delete(self, id=None, *args, **kwargs) -> Response:
        if not id:
            raise TeamNotFound
        try:
            team = self.db_session.query(Team).filter_by(id=id).one()
        except NoResultFound:
            raise TeamNotFound

        self.db_session.delete(team)
        self.db_session.commit()

        return JSONAPIResponse(status_code=204)
Exemplo n.º 10
0
def serialize_error(exc: Exception) -> JSONAPIResponse:
    """
    Serializes exception according to the json:api spec
    and returns the equivalent :class:`JSONAPIResponse`.
    """
    if isinstance(exc, JSONAPIException):
        status_code = exc.status_code
        errors = exc.errors
    elif isinstance(exc, HTTPException):
        status_code = exc.status_code
        errors = [{'detail': exc.detail}]
    else:
        status_code = 500
        errors = [{'detail': 'Internal server error'}]

    error_body = {'errors': errors}
    return JSONAPIResponse(status_code=status_code, content=error_body)
Exemplo n.º 11
0
    async def to_response(self,
                          data: dict,
                          meta: dict = None,
                          *args,
                          **kwargs) -> JSONAPIResponse:
        """
        Wraps ``data`` in a :class:`starlette_jsonapi.responses.JSONAPIResponse` object and returns it.
        If ``meta`` is specified, it will be included as the top level ``"meta"`` object in the json:api response.
        Additional args and kwargs are passed when instantiating a new :class:`JSONAPIResponse`.

        :param data: Serialized resources / errors, as returned by :meth:`serialize` or :meth:`serialize_related`.
        :param meta: Optional dictionary with meta information. Overwrites any existing top level `meta` in ``data``.
        """
        if meta:
            data = data.copy()
            data.update(meta=meta)
        return JSONAPIResponse(
            content=data,
            *args,
            **kwargs,
        )
Exemplo n.º 12
0
def test_jsonapi_response_headers():
    resp = JSONAPIResponse()
    assert resp.status_code == 200
    assert resp.headers['content-type'] == 'application/vnd.api+json'
    assert resp.body == b''