Mock your API with Bottle in Heroku

Nowadays, developers on the client side of the applications normally need a mock server with mock responses to start working on their app.

During the years, I have been working as a mobile developer that always ends up with this need, I always use python since it is a very simple language and has a framework called bottle that makes our lives easier.

In this post I will explain how to build with few lines of code a mock server on Heroku. We need for this tutorial:

Heroku

Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.

If you already installed the Heroku CLI, open a terminal and login with your heroku credentials typing:

$ heroku login

You can clone my project to follow the tutorial:

git clone https://github.com/dgomez-developer/heroku-bottle-mock-server.git

Or if you want to start from scratch clone this template provided by Heroku:

git clone https://github.com/heroku/python-getting-started.git
$ cd python-getting-started

Next, we create our project in heroku with the following command:

$ heroku create name-of-your-project

When the project is created, copy the repository cloned to the Heroku project folder (you can also link your GitHub repository to the heroic project following these steps) and deploy your code with the following command:

$ git push heroku master

You will see in the logs how heroku installs python and deploys your app.

Now let’s talk about bottle and how it works.

 

Bottle

Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.

The first thing we need to do is to add the bottle dependency to the requirements.txt file:

bottle==0.12.10

If you used the heroku template or this project, please notice the runtime.txt file where the python version to be used by the app is specified.

 

The next step is to create a Python script, for example server.py.

The following code is quite straight forward. It exposes two endpoints /exercise4/people that returns the content of a local JSON file and /exercise4/menu.xml that returns the content of a local xml file.

import os

import json

from bottle import route, run, template, request, BaseResponse, response

@route('/exercise4/people')

def getPeople():

return open('exercise4/people.json')

@route('/exercise4/menu')

def getMenu():

response.headers['Content-Type'] = 'xml/application'

return open('exercise4/menu.xml')

run(host='0.0.0.0', port=os.environ.get('PORT', 5000))

The last command starts the server taking a local IP and using the port 5000.

 

Very easy right?

 

Now, we have to create a Procfile that will be read by Heroku and execute the commands specified on it. To run our python server we only have to add this line:

web: python server.py runserver $PORT

If you are using windows you should name the file Procfile.windows and put this line:

web: python server.py runserver 0.0.0.0:5000

Aa a last step, commit your changes and push them to heroku. Once the app has been deployed, you can start making calls to your mock API.

Your endpoint would be something like this:

http://your-project-url.herokuapp.com/exercise4/people

http://your-project-url.herokuapp.com/exercise4/menu.xml

 

References:

https://devcenter.heroku.com/articles/getting-started-with-python#define-a-procfile

https://bottlepy.org/docs/dev/

Share

Schreibe einen Kommentar