A simple RESTful API Proxy written in Python/Flask that combines and caches multiple calls to an API into one single call with a lightweight response. These kind of API Proxies are useful when creating a mobile application (in order to send as little data as possible over the wire) or when combining multiple different APIs into one.
- Python 3.2 or later.
- virtualenv is recommended.
- Clone the repository using
git clone https://github.com/ninadpdr/simple-api-proxy-cached.git
. - This will create a new directory
simple-api-proxy-cached
.
From inside the directory, create a virtualenv usingvirtualenv .venv
. - Once the virtualenv is created, activate it using
source .venv/bin/activate
. - Now, install all the packages from the
requirements.txt
file,
using the command,pip install -r requirements.txt
. - Then run the app using
python app.py
.
This will start the API proxy server on http://0.0.0.0:5000/.
This app uses following Python libraries:
-
This service is a perfect candidate for using a microframework. It's a simple RESTful web service, doesn't need ORM, templating, etc. Which is why Flask is chosen.
-
Flask-RESTful is an extension for Flask that adds support for quickly building REST APIs. Flask-RESTful encourages best practices with minimal setup. For example, we use using multiple return values in Flask-RESTful to return the response with an HTTP response status code.
-
This is a requirement for connecting a custom error handler to Flask-RESTful.
-
We use requests library to call the APIs we are proxying to.
-
Test module in the Python standard library.
Given an event ID, returns the event title and the first names of its attendees.
-
URL
/events-with-subscriptions/:event_id/
-
Method
GET
-
URL Params
Required:
event_id=[string]
-
Data Params
None -
Success Response
- Code: 200 OK
- Content:
{ "id": "<event-id>", "title": "<event-title>", "names": "<list-of-first-names>" }
-
Error Response
- Code: 400 Bad Request, 502 Bad Gateway, 504 Gateway Timeout
- Content:
{ "error": { "message": "<error-description>", "status_code": <HTTP-status-code> } }
-
Sample Call
$ http GET http://0.0.0.0:5000/events-with-subscriptions/0069c02d7fa333a88b7e50d850b9bcf6_14677217380318/ HTTP/1.0 200 OK Cache-Control: max-age=252 Content-Length: 273 Content-Type: application/json Date: Fri, 08 Jul 2016 18:43:05 GMT Server: Werkzeug/0.11.10 Python/3.5.1 { "id": "0069c02d7fa333a88b7e50d850b9bcf6_14677217380318", "names": [ "API", "Michel", "Jasper", "Bob", "Dennis", "Edmon", "Aslesha", "Lars" ], "title": "Drink a cup of coffee with C42 Team" } $ http GET http://0.0.0.0:5000/events-with-subscriptions/0069c02d7fa333a88b7e50d850b9bcf6_1467721738031/ HTTP/1.0 404 NOT FOUND Content-Length: 166 Content-Type: application/json Date: Fri, 08 Jul 2016 18:43:11 GMT Server: Werkzeug/0.11.10 Python/3.5.1 { "error": { "code": "NOT_FOUND", "message": "Event 0069c02d7fa333a88b7e50d850b9bcf6_1467721738031 not found", "status_code": 404 } }
Tests are located under tests
directory. To execute all tests,
- cd to project directory.
source .venv/bin/activate
.python -m unittest discover -s tests
.