In my last post, I set up an SSL cert so I could communicate with my API using HTTPS. I also previously discussed setting up a python falcon framework in this post. Now that it is all set up, I’m going to dive a little deeper into the python falcon framework code.
Here is the most simple functional falcon script.
def on_get(self, req, resp):
resp.body = falcon.HTTP_200
app = falcon.API()
checksite = CheckSite()
Once you have falcon installed (I used pip install falcon instead of using the cython compiler–for some reason it wasn’t working for me), you should be able to import it using import falcon. Then, you create a class for each API method you want for your application. For this example, I am using the CheckSite class to check if the site is functioning correctly. Then, you can define a function, on_get or on_post or on_put, etc. using HTTP methods. resp.body is what the function will return, so if we set it to an HTTP 200 status, it will return a 200 OK response. Then, I create an instance of the app using falcon.API(), create an instance of the class CheckSite(), and then add a route to the app using app.add_route(‘/’, checksite). Now, when I go to [my domain].com I should get a 200 status ok.
This is super simple and provides a quick starting place for creating a Back-End API for a mobile application using python.