Skip to main content

Automated REST APIs for legacy (existing) databases

Project description

Build Status Coverage Status

sandman2 documentation

sandman2 automagically generates a RESTful API service from your existing database, without requiring you to write a line of code. Simply point sandman2 to your database, add salt for seasoning, and voila!, a fully RESTful API service with hypermedia support starts running, ready to accept HTTP requests.

This is a big deal. It means every single database you interact with, from the SQLite database that houses your web browser’s data up to your production PostgreSQL server can be endowed with a REST API and accessed programatically, using any number of HTTP client libraries available in every language. sandman2 frees your data.

For developers:

Imagine you’re working for AnonymousCorp and need to access Group Y’s data, which is presented to you through some horrible API or GUI. Wouldn’t it be nice if you could just interact with that database through a REST API?

More than that, imagine if you could interact with the database through a REST API and no one had to write any code. Not you. Not Group Y. No one. That means no boilerplate ORM code, no database connection logic. Nothing. sandman2 can be run as a command-line tool (sandman2ctl) that just takes your database information as parameters and connects to it, introspects the schema, generates a RESTful API, and starts the server.

What Happened to Sandman (1)?

`sandman <http://www.github.com/jeffknupp/sandman>`__, the precursor to sandman2, is no longer being maintained. sandman had almost identical functionality but had an architecture that reflected the capabilities of the underlying ORM, SQLAlchemy. As of the 0.9 release, SQLAlchemy introduced the automap construct. This fundamentally changed the way that sandman could interact with the underlying database in a way that greatly simplified things. All that was needed was the actual effort to rewrite sandman from scratch…

After wrestling with the idea for a while, I finally gave in and started the rewrite project. sandman2 is that project. While I’ll continue to support sandman in the nearterm, sandman2 definitely represents the way forward.

NOTE: sandman2 is not yet at feature parity with the original sandman, but should be soon. Getting there is currently the top priority.

Quickstart

Install sandman2 using pip: $ pip install sandman2. This provides the script sandman2ctl, which just takes the database URI string, described here. For example, to connect to a SQLite database in the same directory you’re running the script, you would run:

$ sandman2ctl sqlite+pysqlite:///database_file_name

To connect to a PostgreSQL database, make sure you install a driver like psycopg2 using pip, then use the following connection string:

$ sandman2ctl postgresql+psycopg2://scott:tiger@localhost/mydatabase

Again, see the SQLAlchemy documentation for a more comprehensive discussion of connection strings.

Supported Databases

sandman2 supports all databases that the underlying ORM, SQLAlchemy, supports. Presently, that includes:

  • MySQL

  • PostgreSQL

  • Oracle

  • Microsoft SQL Server

  • SQLite

  • Sybase

  • Drizzle

  • Firebird

Third-party packages extend support to:

  • IBM DB2

  • Amazon Redshift

  • SQL Anywhere

  • MonetDB

Admin Interface

One of the best things about the original `sandman <http://www.github.com/jeffknupp/sandman>`__ was the Admin Interface. Not only does sandman2 include the Admin Interface, but it modernizes it as well. The layout has been greatly improved, especially when dealing with larger numbers of tables. All of the original functionality of the Admin Interface remains unchanged.

Here’s a shot of the new look:

admin interface awesomesauce screenshot

admin interface awesomesauce screenshot

Customizing

If sandman2ctl doesn’t give you fine-grained enough control over your REST endpoints, or you’d like to restrict the set of tables made available via sandman2ctl, you can easily integrate sandman2 into your application. See the documentation for more info.

Running sandman2 under Docker

sandman2 has an official docker image at Docker Hub. Simply docker pull jeffknupp/sandman2 to get the latest version. It supports the most popular database engines, but not all that sandman2 currently natively supports. If you’d like to see support for your RDBMS, either add a pull request on this repo (if possible) or create a new issue with the details of your database’s Python driver.

Example

Here’s how one would run sandman2 to connect to a PostgreSQL database running on one’s host machine (i.e. not a remote database, which is far simpler) under Docker (on a Mac, explained below):

  1. $ docker pull jeffknupp/sandman2

  2. $ docker run -d -e DB_TYPE=postgres -e DB_DRIVER=psycopg2 -e USERNAME=jknupp -e DB_HOST=host.docker.internal -e DATABASE=jknupp -e DB_PORT=5432 -p 9000:5000 sandman2

  3. $ curl localhost:9000/meta or open a browser to http://localhost:9000/admin/

Note, ``DB_HOST=host.docker.internal`` is only necessary for databases that reside on the host system (and the value only works on macOS). To connect to a database on a remote machine, simply replace that value with the machine’s IP or hostname.

Parameters

Here are the parameters available to specify your connection information and their meaning:

  • $DB_TYPE - The type of RDBMS to connect to (e.g. postgres or mysql)

  • $DB_DRIVER - The name of the Python library to use as a driver (e.g. psycopg2 or pymysql)

  • $USERNAME - Database username

  • $PASSWORD - Database password

  • $DB_HOST - Database IP or hostname

  • $DB_PORT - Database port

  • $DATABASE - Name of database to connect to

Pass each value separately to the docker run command with -e <VARIABLE>=<VALUE>. Not all are required, but which ones are required differs based on your target RDBMS.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sandman2-1.2.1.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

sandman2-1.2.1-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

Details for the file sandman2-1.2.1.tar.gz.

File metadata

  • Download URL: sandman2-1.2.1.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for sandman2-1.2.1.tar.gz
Algorithm Hash digest
SHA256 dc1e5192609d00ceff548da11af088f0bbeaebff5fa758f39d4c90424bf28e49
MD5 befa53e8f3b5a9e7911c541e282f7bd0
BLAKE2b-256 83c2941269a56a1928fcb020cd430c51be77bbde739c1f76fe24cb4f87775f48

See more details on using hashes here.

File details

Details for the file sandman2-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: sandman2-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for sandman2-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6eba58de817944e5268769eb68605c488a9e2a81bc1477e85df57980bc245529
MD5 cac87df3b8d60df642c69309efb1c1b6
BLAKE2b-256 30fec26ea7b03804a5906b7b2246e07a7cf2a33f6f514e089812825342270d1c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page