Nico
Nico

Reputation: 374

Quick Rest API with Python for mocking responses

I am testing a C# application that make requests to another Rest API and I want to mock the server. I known basic python and I was wondering if I can write a simple API Rest server without be involved with large frameworks like Django. It would be a simple server where I receive a json through request body and I have to return another json (with the logic of the return inside, like a view).

Best regards!

Something simple like this:

@path(/api/v1/somepath, GET)
def my_function(request):
    json_input = request.body.json()

    # My logic here
    response.status = 200
    response.body = {'some_field': 'something'}
    return response

Upvotes: 8

Views: 3255

Answers (1)

Ashish Ranjan
Ashish Ranjan

Reputation: 5543

In case you really don't want to use any external frameworks/ libraries, you can create a simple class which extends BaseHTTPRequestHandler, something like this :

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import json

class S(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()

    def do_GET(self):
        self._set_headers()
        self.data_string = self.rfile.read(int(self.headers['Content-Length']))

        self.send_response(200)
        self.end_headers()

        data = json.loads(self.data_string)
        # your processing
        outJson = {"success": True}
        self.wfile.write(json.dumps(outJson))

    def do_HEAD(self):
        self._set_headers()
        self.wfile.write("HEAD")

    def do_POST(self):
        self._set_headers()
        self.wfile.write("POST")

and then start the server locally at the port of your choice(80 by default) simply like this:

def run(port=80):
    httpd = HTTPServer(('', port), S)
    print 'Starting httpd...'
    httpd.serve_forever()

if __name__ == "__main__":
    from sys import argv
    if len(argv) == 2:
        run(port=int(argv[1]))
    else:
        run()

and if you need to manage lots of routes easily, you can use klein or Flask microframeworks (or even bottle) to make it easier and simpler for you, a simple minimal usage example for using klein would look something like this:

import json
from klein import Klein

class ItemStore(object):
    app = Klein()

    def __init__(self):
        self._items = {}

    @app.route('/')
    def items(self, request):
        request.setHeader('Content-Type', 'application/json')
        return json.dumps(self._items)

    @app.route('/<string:name>', methods=['PUT'])
    def save_item(self, request, name):
        request.setHeader('Content-Type', 'application/json')
        body = json.loads(request.content.read())
        self._items[name] = body
        return json.dumps({'success': True})

    @app.route('/<string:name>', methods=['GET'])
    def get_item(self, request, name):
        request.setHeader('Content-Type', 'application/json')
        return json.dumps(self._items.get(name))

and then run the server like this:

if __name__ == '__main__':
    store = ItemStore()
    store.app.run('localhost', 8080)

Also, if you want to use the mock apis remotely and not just locally, you can use tunneling tools like ngrok. It's pretty simple and easy to use.

Upvotes: 8

Related Questions