Sunday, October 27, 2013

Into the unknown with Google App Engine

As mentioned before, I am setting up a cloud back-end for my mobile app.

This has involved writing code in a few different languages.  
  • Java - for the android application.
  • Python - for writing the Google Endpoints API.
  • Javascript/Html/Css - for writing a web client for the endpoints API. 
Working from the tutorials, I have been able to progress without too much issue.  However, I ran into a little bit of confusion when trying to pass around a datetime object.  Specifically, I wanted to:
  1. Start with a date / time value. 
  2. Pass that to my endpoints api. 
  3. Store it in the Google App Engine datastore.
I couldn't find solid documentation on how this works, so it took me a while to figure out. Here are the steps:
  1. In javascript code pass in your date time for some field like so:
    { some_field : "2011-12-16T10:00:00.000" }
    The format is Year-Month-Date"T"Hour:Minute:Second.FractionalSeconds
    When I tested, passing in a time zone offset threw an error.
  2. In python, when you construct your date time field use:
    from protorpc import message_types
    some_field = message_types.DateTimeField(1, required = True)
    # This is a little bit different from passing other fields which use protorpc.messages
  3. Once you have the datetime field, you can use the type ndb.DateTimeProperty in your model that you will insert.
This all works by passing in a string, which gets parsed and converted to a datetime object.  If you didn't want to use protorpc conversion to a datetime object, you can pass in a datetime string that you'd have to encode and decode.

Update - After I set up an Android client to send data to my endpoints api, I got more errors about the datetime having a bad timezone format.  After some trial and error then googling for solutions, I decided that dealing with datetime data wasn't worth the hassle.  I changed the datastore to only save "Long" values.  Then I updated the API to send and receive long values.  Finally, when I get to the UI, I convert the long into date time format for display.  This made things much simpler and cleaner.

No comments:

Post a Comment