Friday, 22 April 2011

Introducing ProtoRPC for writing App Engine Web Services in Python

Here on the App Engine team, we’re always looking for new ways to make it easier for developers to build applications and services. Today, I’m happy to introduce ProtoRPC, a new tool for creating simple Python services, which requires minimal set up and configuration to create new services.

What can you use ProtoRPC web-services for? Most web applications have the need to send and receive data between different components and/or applications. Typically, developers come up with ad-hoc ways of doing this as quickly as possible. As the application grows larger and the need to share information across components grows, it becomes more difficult to manage. URL end-points are defined and appear inconsistent from one another and a lot of boiler plate code is added checking parameters. This quickly becomes a maintenance nightmare and is a problem that ProtoRPC is built to solve.

ProtoRPC makes it easy to write consistent, reliable web interfaces that can be used, for example, to do the following:
  • Create AJAX URL end-points for use by dynamic web pages and forms.
  • Provide simple and understandable server-to-server communications.
  • Become a back-end for command-line tools or other non-web based clients.
Using ProtoRPC, you can define structured web-services right in the application’s Python code without having to first learn and write a new interface definition language such as Thrift and Protocol Buffers, however still retain the same powerful features such as interface introspection and automatic client generation.

The way to go about defining a web service should be familiar to you if you already have experience working with App Engine db.Models and the webapp framework. The data sent between client and web service are defined in a similar way as Datastore models. The services classes that handle requests are defined similarly to webapps RequestHandler classes. Let’s take a look at a simple example from the ProtoRPC getting started guide. This simple web service says hello to its client:
class HelloRequest(messages.Message):
    my_name = messages.StringField(1, required=True)

class HelloResponse(messages.Message):
    hello = messages.StringField(1, required=True)

class HelloService(remote.Service):

    @remote.method(HelloRequest, HelloResponse)
    def hello(self, request):
        return HelloResponse(hello='Hello there, %s!' % request.my_name)
If this web services was used as the URL end-point for an AJAX based form, Javascript to communicate with the service might look like this:
$.ajax({url: ‘/helloservice.hello’,
    type: 'POST',
    contentType: 'application/json',
    data: ‘{ my_name: Bob }’,
    dataType: 'json',
    success: function(response) {
        // The response is { hello: “Hello there, Bob!” }
            alert(response.hello);
        }
    });
As you can see from the example, ProtoRPC can speak JSON right out of the box. In addition, it is compatible with the protocol buffer binary format and can therefore communicate with clients and servers written using traditionally compiled .proto files.

Right now, ProtoRPC is available as an separate project that can be downloaded here. It’s still considered experimental and may change in substantial ways before being integrated in to the SDK. But even though it’s in preview, it already has a number of useful features, such as a remote service discovery mechanism and a forms interface for easy testing. At the moment, ProtoRPC is only available in Python. As always, we plan to offer it for Java developers in the near future.

Check out the getting started guide for a more complete overview to try out writing a few services of your own!

No comments:

Post a Comment

Share This Post