juriwiens
juriwiens

Reputation: 11

How to return something else than a model using endpoint-proto-datastore

I'm coding an API via Google Cloud Endpoints and the endpoints-proto-datastore library.

Here's my model:

class Domain(EndpointsModel):
    _message_fields_schema = ('id', 'name', 'enabled', 'adminEmails')
    name = ndb.StringProperty(required=True)
    enabled = ndb.BooleanProperty(required=True)
    adminEmails = ndb.StringProperty(repeated=True)

And this is my delete method:

@Domain.method(request_fields=('id',), path='domains/{id}', http_method='DELETE', name='domain.delete')
def delete_domain(self, domain):
    if not domain.from_datastore:
        raise endpoints.NotFoundException('Domain not found.')
    domain._key.delete()
    return domain

Can I return something else than the model itself? How do I return a specific HTTP Status code or something like VoidMessage?

Upvotes: 0

Views: 286

Answers (1)

Scarygami
Scarygami

Reputation: 15569

You can define a response_message parameter in the decorator (as opposed to the more commonly used response_fields parameter) and set it to VoidMessage. And then return a VoidMessage from your method instead of the model.

from protorpc import message_types

(...)

@Domain.method(request_fields=('id',),
               response_message=message_types.VoidMessage,
               path='domains/{id}',
               http_method='DELETE',
               name='domain.delete')
def delete_domain(self, domain):

    (...)

    return message_types.VoidMessage()

Of course you can also return any other protorpc Message that way. As far as I know there's no way to define what HTTP Status Code to return though.

Upvotes: 4

Related Questions