So you got a very cool Raplet ready to fire up our Gmail inboxes, but don’t have a server to host it? Worry not my friends, Google App Engine comes to the rescue! You can host your Raplet on Google App Engine, and leverage the power of Google’s services such as mail, users, datastore etc. for making your Raplet even better. This article will show you how.
You could also call this post as ‘Make your first ‘Hello World’ Raplet using Python’ ! In my Raplets tutorial series of posts, I used PHP as the language for making a Raplet. But this time I’ll explain how to make a Raplet using Python, and we’ll use Google App Engine as a platform to host it.
This is the url for my Raplet hosted on Google App Engine: http://www.myrapletisongae.appspot.com. We’ll be making this!
Before you begin, make sure you meet the following prequisites:
1) A basic understanding of Rapportive Raplets. This article assumes that you know what a Rapportive Raplet is and have made atleast a simple ‘Hello World’ Raplet. If you need an intro on what Raplets are, please visit the Raplets website. Learn how to make a ‘Hello World’ Raplet using PHP.
2) Google App Engine: You should know what Google App Engine is. Just the basics, even if you’ve completed the ‘Hello World’ tutorial from GAE docs, you’ll have no problem following this article. If not, then I highly suggest you to complete it.
3) Python programming: GAE supports only 2 languages, Python and Java. We will use Python to do our job. Hence, you should know object oriented programming in Python.
4) JSON: A basic understanding of what a JSON document is. Better if you know how to form it and parse it using Python. See JSON website.
Okay, let’s get our hands dirty then…!
Make a new folder to store our Raplet’s files. As all our responses will be encoded as JSON, we will not need any subfolders to hold any templates or stylesheets. Make the config file ‘app.yaml‘ and put the following code in it:
[html]
application: my-raplet-app
version: 1
runtime: python
api_version: 1
handlers:
– url: /.*
script: raplet.py
[/html]
We identify our Raplet as ‘my-raplet-app’. This is for local development only, you can get a new identifier for the Raplet once we are done testing it using our local server and ready to upload it on Google App Engine. ‘version’ is 1, using Python ‘runtime’ for development and ‘api_version’ is 1. The ‘handlers’ section tells that all the urls will be handled by the ‘raplet.py’ file.
Now let’s jump to make the ‘raplet.py’ file. This script will get the all parameters from the ‘GET’ request sent to our Raplet, perform any processing if required, form the JSON encoded response and send it back to Rapportive for displaying. Make a new Python file and name it as ‘raplet.py’. Here’s the code for it:
[python]
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from django.utils import simplejson
class Raplet(webapp.RequestHandler):
def get(self):
parameters = {}
callback = self.request.get(‘callback’)
parameters[‘html’] = ‘<h4>Hello World… Raplets work here too!</h4>’
parameters[‘css’] = ‘h4{ color:red; }’
parameters[‘status’] = 200
self.response.out.write(callback+'(‘+simplejson.dumps(parameters)+’)’)
application = webapp.WSGIApplication([(‘/’, Raplet)
],
debug = True)
def main():
run_wsgi_app(application)
if __name__=="__main__":
main()
[/python]
We’re importing all the basic modules required for the script. ‘webapp’ is the framework that is packed along with GAE, ‘run_wsgi_app’ runs the app, and we need ‘simplejson’ to do the JSON encoding for our Raplet. The class ‘Raplet’ is the only class we require (You can name it anything you want, something that depicts the purpose !). The function ‘get’ handles the ‘GET’ request. We use a ‘dictionary’ object named ‘parameters’ to store our response variables. We assign the ‘html’ parameter a very simple ‘Hello World… Raplets work here too!’ string, ‘css’ parameter a style for the header tag and the ‘status’ as 200, i. e. OK. Then we JSON encode the ‘parameters’ dictionary using ‘dumps’ method from ‘simplejson’ module.
‘callback’, is a very important parameter sent in every request to our Raplet. It is used to match the request with the response has to be appended to our repsonse before returning it. The other parameters sent in the request are: ‘show’, ‘name’, ’email’ and ‘twitter_username’ (Read the docs for details). Finally we write the response back using ‘self.response.out.write’.
‘application’ is the WSGIApplication object. Any requests for the root url (‘/’) instantiates the ‘Raplet’ class and calls the ‘get’ function. ‘main’ runs the app using ‘run_wsgi_app’.
Now to test if the Raplet is working, start the local server, using the command ‘dev_appserver.py
Go to Google App Engine website and register an identifier for our Raplet. It should be unique and something that suits the Raplet’s functionality. Copy the identifier and paste it in the ‘app.yaml’ config file so the it looks like this:
[html]
application: <your app/raplet identifier>
version: 1
runtime: python
api_version: 1
handlers:
– url: /.*
script: raplet.py
[/html]
Now upload the Raplet to GAE server using ‘appcfg.py update <path to the Raplet’s folder>’. Repeat the process for adding a Raplet, and put ‘http://<identifier for your Raplet>.appspot.com/’ as the Raplet’s url. (If your Raplet’s identifier is ‘yabbadabba’, then the Raplet’s url will be ‘http://www.yabbadabba.appspot.com/’. Open up an email again to see the Raplet in action!
Congrats, you’ve just made your first GAE hosted Raplet!
Any doubts, suggestions, comments regarding this article, please contact me at mail@rutwick.com. @tweetrut. Thanks! 🙂