In my last two posts (front-end post, back-end post) I developed a timer that counts down to 0. When it hits 0, an event will fire to move to a new scene. I also talked about creating a text box and capturing user input. Then, I set up Mongo DB as my NoSQL database and configured it so it was ready to use. Up to this point I have been writing a separate post for front-end and back-end progress, but now because they are integrated together so tightly, I’m combining posts together.
In this post, I will discuss how my front-end is communicating with my back-end and what kind of API endpoints I have created.
Using the text box and timer from the last front end post, I can time a user, track their responses, and append the responses to a list and to a table. The user can see the table, and the list will be sent to my back-end. To do this, I needed a back-end method that could accept responses and store them in my database, so I created a sendResponses method. It takes response text as the values, passes along the context information for the level, and then inserts them into my database. On the client side, it just looks like a http request. I’m using the network.request function within Corona, which allows me to POST or GET. Because I am sending data, I am using the POST method.
Example network request:
network.request( https://mydomainname.com/submitresponse”, “POST”, networkListener, params )
The networkListener function will make front-end changes to my app based on the response it receives (or doesn’t receive) from my server. The params variable is a dictionary that has headers (below) and a body (URL encoded string).
headers = {}
headers[“Content-Type”] = “application/x-www-form-urlencoded”
headers[“Accept-Language”] = “en-US”
body = “variable1=blahblah&variable2=haha&variable3=tadah&var4=abc&var4=def”
params.headers = headers
params.body = body
Then, my server receives the request, processes the data, and inserts it into my database. Example code below (this code won’t work for you unless you have a falcon app running on your server, and you have imported falcon, time, and pymongo, and you have a functional instance of MongoDB running that you have access to):
resp.status = falcon.HTTP_200
variable1 = req.get_param(“variable1”)
variable2 = req.get_param(“variable2”)
variable3 = req.get_param(“variable3”)
variable4 = req.get_param_as_list(“var4”)
connection = MongoClient(‘mongodb://username:password@host:port’)
objectID = connection[‘dbName’][‘collectionName’].insert({
“variable1”:variable1,
“variable2”:variable2,
“variable3”:variable3,
“variable4”:variable4,
“created_at”:float(time.time())
})
Notice in the code above that if you have the same parameter in your URL encoded parameters, you can retrieve all values using the req.get_param_as_list() function. If there is only one of each parameter you can use the req.get_param() function. Also notice I am inserting a timestamp for when the responses were inserted into the database.
GOTCHAS
In a python falcon framework, they try to be as minimalist as possible, providing ultimate power to the user to configure anything you might need to configure. Because of this, by default, the request body comes in as a stream (bytes). To access objects in a URL encoded parameter list like I have in the example above, you have to include this line of code at the top of your python script:
app = falcon.API() #to initialize the falcon app
app.req_options.auto_parse_form_urlencoded = True #allows parsing urlencoded data
This post has described how I send responses from my app to my database and how I store the data in my MongoDB. Once I figured out the falcon framework, set up my MongoDB, and figured out how to use the network.request function in Corona it wasn’t too hard to get things working.
In my next post I’ll talk about some additional API methods I have created and how that interfaces with my mobile application.