Using only the standard library in Python to build an API
2 min read

Using only the standard library in Python to build an API

This week I was on a business trip and while waiting for time to pass I asked myself the question whether or not it would be easy to write a simple REST API using only python's standard library.

And indeed it is possible and quite easy too:

Simple and functional REST server for Python (3.5) using no dependencies beyond the Python standard library. Ported from https://gist.github.com/tliron/8e9757180506f25e46d9
Simple and functional REST server for Python (3.5) using no dependencies beyond the Python standard library. Ported from https://gist.github.com/tliron/8e9757180506f25e46d9 - rest35.py

Why is this cool?

Well, combining this with PyInstaller, you can now create a single binary file that will create an api on any computer that is similar to the one the binary was built on. Having it in one binary is very convenient for distribution and for testing and frankly, if it can be a binary it should be a binary. :)

Using only the standard library stuff I can be quite sure that PyInstaller will work without hiccups, I've had bad experiences with the pass when bundling external libs/packages to my projects and running PyInstaller. Now it works smoothly.

Now what?

Here's the reason to all of this, sqlite is in the standard library too right? So what about having a single binary / crud app to do something similar to DBCore? Meaning providing a quick and simple API to bootstrap any backend / API without all the hassle and have the data persisted?

By hassle I mean doing things like installing Flask & or Django, getting the right versions, writing migrations, writing the schema, code for the different routes & other get-your-hands-dirty things. That code has been written and rewritten a billion times now.

What I need is just to define my model and have the standard routes and  a basic admin GUI to update / edit records manually leveraging the same API.

Thoughts

I think my time shouldn't be wasted rewriting all the same code over and over but to be able to get to a working API as fast as possible with the data model that I need.

There are actually many options that do this today but the setup is usually very involved and adds another layer of complexity to the whole thing. I essentially need an opinionated tool (binary) that provides a configurable interface to an SQLite database (or other) as a REST API.

I'll work on something like this during the weekend and see if I can get a working prototype.