Writing the API: A Simple POST
In this part of the tutorial, you'll add support for a simple POST to the backend API that you created previously. The incoming request contains a user-supplied message and integer; the method repeats the message the number of times specified by the integer and returns the results.
Adding a POST method
-
In the
helloworld_api.py
file created previously, add the following lines above the lineID_RESOURCE = endpoints.ResourceContainer
:MULTIPLY_METHOD_RESOURCE = endpoints.ResourceContainer( Greeting, times=messages.IntegerField(2, variant=messages.Variant.INT32, required=True)) @endpoints.method(MULTIPLY_METHOD_RESOURCE, Greeting, path='hellogreeting/{times}', http_method='POST', name='greetings.multiply') def greetings_multiply(self, request): return Greeting(message=request.message * request.times)
-
When you are finished, your file should look like this:
"""Hello World API implemented using Google Cloud Endpoints. Defined here are the ProtoRPC messages needed to define Schemas for methods as well as those methods defined in an API. """ import endpoints from protorpc import messages from protorpc import message_types from protorpc import remote package = 'Hello' class Greeting(messages.Message): """Greeting that stores a message.""" message = messages.StringField(1) class GreetingCollection(messages.Message): """Collection of Greetings.""" items = messages.MessageField(Greeting, 1, repeated=True) STORED_GREETINGS = GreetingCollection(items=[ Greeting(message='hello world!'), Greeting(message='goodbye world!'), ]) @endpoints.api(name='helloworld', version='v1') class HelloWorldApi(remote.Service): """Helloworld API v1.""" @endpoints.method(message_types.VoidMessage, GreetingCollection, path='hellogreeting', http_method='GET', name='greetings.listGreeting') def greetings_list(self, unused_request): return STORED_GREETINGS MULTIPLY_METHOD_RESOURCE = endpoints.ResourceContainer( Greeting, times=messages.IntegerField(2, variant=messages.Variant.INT32, required=True)) @endpoints.method(MULTIPLY_METHOD_RESOURCE, Greeting, path='hellogreeting/{times}', http_method='POST', name='greetings.multiply') def greetings_multiply(self, request): return Greeting(message=request.message * request.times) ID_RESOURCE = endpoints.ResourceContainer( message_types.VoidMessage, id=messages.IntegerField(1, variant=messages.Variant.INT32)) @endpoints.method(ID_RESOURCE, Greeting, path='hellogreeting/{id}', http_method='GET', name='greetings.getGreeting') def greeting_get(self, request): try: return STORED_GREETINGS.items[request.id] except (IndexError, TypeError): raise endpoints.NotFoundException('Greeting %s not found.' % (request.id,)) APPLICATION = endpoints.api_server([HelloWorldApi])
Running and Testing
To run and test your new additions to the backend API:
-
Run the backend you just created by invoking the command
~/path/to/python/sdk/google_appengine/dev_appserver.py helloworld
When the backend is running successfully, a message similar to this one is displayed:
INFO 2013-10-07 19:41:16,687 admin_server.py:117] Starting admin server at: http://localhost:8000
-
In your browser, visit this URL:
http://localhost:8080/_ah/api/explorer
-
In the API Explorer, after you click helloworld API , notice that the list now also contains the new method. Click helloworld.greetings.multiply to display the Explorer form for it:
-
In the form's times textbox, supply the number of times you want the POSTed message to be echoed in the response, for example,
3
. -
Click inside the Request body control, click add a property > message , add the text message "Charles Darnay ", then click Execute .
In the Response, you'll see the success message and the message
"
Charles Darnay Charles Darnay Charles Darnay
".
Code summary
The greetings.multiply method supporting POST uses a ResourceContainer instead of a simple message class type, because the method must support requests containing an argument in the URL path.
Next...
Next, we'll add a method that is access-restricted by OAuth 2.0.
Continue to Adding a Method Protected by OAuth 2.0 .