ProtoRPC Python postservice.py example

107 views
Skip to first unread message

dansalmo

unread,
Oct 27, 2012, 10:00:07 PM10/27/12
to google-prot...@googlegroups.com
Does anyone know if there is working set of example files for this tutorial?  https://developers.google.com/appengine/docs/python/tools/protorpc/overview

Or if anyone tried what is being described to make sure it works?

I have tried to piece it together but there are some obvious errors in the example instructions.  For instance it says it uses the guestbook.Greeting class from the  guestbook tutorial and has an "import guestbook" statement in postservice.py but the guestbook tutorial is based on a helloworld.py file.  Changing postservice.py to "import helloworld" and calling helloworld.Greeting seems to get past this problem.

The tutorial is also not clear as the what the final postservice.py file should look like.  If you follow the instructions verbatim you will likely leave out the Note class definition.  It is not clear what fragments mentioned in the example code boxes should actually go into the postservice.py file.  When I got the to point where it suggests testing with a curl command, it seemed to work as described in that I got the empty json response {} and the log file showed a successful post with a 200 response, but the guestbook database was not updated.

Here is the postservice.py file contents with my modifications to get around the import guestbook error and missing Note class error:

import datetime

from protorpc import messages
from protorpc import message_types
from protorpc import remote

import helloworld

class Note(messages.Message):

    text = messages.StringField(1, required=True)
    when = messages.IntegerField(2)

class PostService(remote.Service):

    # Add the remote decorator to indicate the service methods
    @remote.method(Note, message_types.VoidMessage)
    def post_note(self, request):

        # If the Note instance has a timestamp, use that timestamp
        if request.when is not None:
            when = datetime.datetime.utcfromtimestamp(request.when)

        # Else use the current time
        else:
            when = datetime.datetime.now()
        note = helloworld.Greeting(content=request.text, date=when)
        note.put()
        return message_types.VoidMessage()

dansalmo

unread,
Oct 27, 2012, 11:21:06 PM10/27/12
to google-prot...@googlegroups.com
I did get the service working by adding the parent=helloworld.guestbook_key() argument to the call to helloworld.Greeting.

note = helloworld.Greeting(parent=helloworld.guestbook_key(), content=request.text, date=when)

Whoever wrote the ProtoRPC Python example based it on something completely different that what is described in the guestbook tutorial they linked to.  The guestbook example adds an automatically detected username and user supplied message to the Datastore.  The Greeting(db.Model) has the date entry set to auto_now_add=True.  The service example does not set a user, but does does supply the date unnecessarily.  This is a very confusing mash up of examples. 

I hope someone corrects these issues soon to prevent others from wasting time wondering what they did wrong when it does not work as described.

thed...@gmail.com

unread,
Feb 6, 2013, 4:33:12 AM2/6/13
to google-prot...@googlegroups.com
I am having the same problems you are - except I got 404 errors whenever I used curl in cygwin. I got the log to show two lines each time I tried posting with requests or httplib2, but still haven't been able to get the lousy thing to actually add a post onto localhost:8081. It's too bad the developers aren't commenting on this at all. I have been having a lot of trouble trying to get my project to post to the app engine and actually being able to retrieve it once I have done so.

thed...@gmail.com

unread,
Feb 6, 2013, 5:01:50 AM2/6/13
to google-prot...@googlegroups.com
Did you ever actually get your remote text to show up on the Guestbook page?


On Saturday, October 27, 2012 11:21:06 PM UTC-4, dansalmo wrote:
Reply all
Reply to author
Forward
0 new messages