Friday, April 15, 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!

--

8 comments:

Riley said...

Fantastic! I hope this comes to GAE/J too!

hyperslug said...

I think most of us assume 'javascript' meant 'jQuery' for this example, but it's clear enough for other library users to follow.

skrat said...

Looks quite crisp, could it be used with existing frameworks / apps?

jong said...

Sounds great for Python enthusiasts. I am thinking of Google should develop another Python "framework". Of course it might be better if open-source ;)

Ondřej Čertík said...

Phenomenal! Every project is crafting its own Python JSON-RPC machinery, it's nice to have a "canonical" way of doing it.

Giacecco said...

Great Rafe, thanks! What's the best way to send you any feedback? The google-appengine-python group perhaps or you created a dedicated one? I have just tried making the first example in your documentation work and I am already getting strange mistakes. Looking forward to see your library develop further and become part of the SDK!

dan said...

Neat, but what about the hard parts of building services - authentication, authorization, rate limiting, etc?

rafek said...

Gaicecco,

If you are interested in providing feedback, I am very interested in hearing it and helping out where I can. I just created a discussion group for ProtoRPC. I will also try to keep up on the App Engine group.

Here's a link to the group:

http://groups.google.com/group/google-protorpc-discuss