Python CRUD REST API with Cloudant
Setup
-
Sign up with Bluemix so that we can easily setup Python hosting and a Cloudant database.
-
Download the Cloud Foundry command line interface. This can be run in OSX Terminal or Win Command Prompt (Cygwin not supported)
$ cf api https://api.ng.bluemix.net
$ cf login
-
Install Python version 2.7.x. You can check if you already have Python installed with:
$ python --version
-
Get the starter code for a Python Flask server. This starter code will have everything we need to run our server in Bluemix. The [app_name] has to be unique. Your name followed by 4 random numbers should do (e.g. patrick_5342)
$ git clone https://github.com/pwcremin/pythonstarter [app_name]
$ cd [app_name]
-
Now all we have to do is push our app and we will have a hosted Python server! This step will take a few minutes.
$ cf push [app_name]
Once the server has successfully started, navigate to [app_name].mybluemix.net
-
Create and bind your Cloudant service
$ cf marketplace -s cloudantNoSQLDB
You can see from the output that Bluemix will give you plenty without there being any cost, so lets get this service going:
$ cf create-service cloudantNoSQLDB Shared [app_name]-cloudantNoSQLDB
-
Bind the service to your app
$ cf bind-service [app-name] [app_name]-cloudantNoSQLDB
$ cf restage [app-name]
-
Install Flask and Cloudant python modules. You will need pip if it is not already installed. These modules are already in your requirements.txt
$ pip install flask
$ pip install cloudant
-
Setup your local environment
$ cf env [app_name]
Copy the VCAP_SERVICES block and stick it into a file named .env.vcap_services.json.
.env.vcap_services.json
-
Run your server locally
$ python server.py
Create the CRUD REST API
Now we have a pretty basic Flask server. Run this locally and hit http://localhost:8000 to make sure it works.
To implement the CRUD operations (create, read, update, delete), we will need to connect to our Cloudant db. Add the below code and you will be able to connect and create the db.
Delete the boring HelloWorld code, and put this more exciting GET call in its place
Note the weird @app.route above your function. This is a route decorator and is used to let Python know that this function should be called for the given url.
Run the server and navigate to http://localhost:8000/api/v1/calls. Ok, pretty cool that you can pull data from the server, but its not very interesting yet.
Using your browser is fine for GET calls, but we need something more powerful to test out the rest of our api. Postman is an awesome tool for this. Add the tool to Chrome and lets use it for our request.
Click the down arrow next to the floppy disk icon so that you can save this request to a collection (name the collection whatever you want).
Create the POST endpoint so that we can add data:
Change your Postman request to POST, and update the request like below. Make sure to save this request as well. You can run this POST a few times, and then try your GET request again. Look at that, it works.
We do not always, or rarely even, want to GET every document in our database. Lets make a GET call that will only return the specified doc by id.
At this point you should have the gist of how to create a REST api. Lets go ahead and knock out the last two CRUD endpoints; update and delete
-
Push your awesome server up to Bluemix
cf push [app_name]
Now test your REST calls against your Bluemix Python server