protorpc 

Send to Kindle
home » snippets » appengine » protorpc



Snippets

Call via curl:

curl -H 'content-type:application/json' -d '{"my_name": "curl"}' http://localhost:8000/hello.hello

Prints:

{"hello": "Hello there, curl!!"}

Call using jQuery:

// From: https://docs.google.com/a/google.com/document/d/1QJBVWnbtYoiTFHIyqcBdnv5u4C1zYvDzFFGBIun35Ro/view
$.ajax({url: "/hello.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);
          }
         });

ProtoRPC Message

from protorpc import messages

class SubMessage(messages.Message):
  name = messages.StringField(1, required=True)

class MainMessage(messages.Message):
  name = messages.StringField(1, required=True)
  sub_messages = messages.MessageField(SubMessage, 2, repeated=True)

Use protobufs on GAE

Ref: Python protobuf on Google App Engine

# Ensure that the google/protobuf/* is under "extra_paths" under your application root.

# Add protobuf to google namespace package
import google  # provided by GAE
google.__path__.append(os.path.join(os.path.dirname(__file__), 'extra_paths', 'google'))