Python API Setup

Estimated reading time: 6 minutes

Overview

This is an API built on top of Python and Flask for the Interject Excel Add-in. The API Allows execution of pass-through commands on a server/database and returning data to the Interject Excel Add-in.

In this walkthrough we will setup a Python API locally which can be used to manage dataflow to and from an Interject Report.

Requirements

Before the api can be run you need to ensure that your system meets the following requirements:

  • Python 3.4 or greater must be installed
  • Git must be installed (optional)

Once Python is installed the dependancies for the Interject Api include:

  • Flask >= 0.12.1
  • Flask-RESTful >= 0.3.6
  • SQLAlchemy >= 1.1.15
  • pycryptodome >= 3.4.0
  • pyodbc >= 4.0.21
  • mysqlclient >= 1.3.12
  • urllib3
  • requests
  • pyOpenSSL (when running api with HTTPS)

Get The Code

Start by cloning the repository to your system.

git clone https://gitlab.com/Interject/Interject-Python-API.git

If you do not have git installed or prefer a different method, simply download the source code from the repository website.

Install The Python Package

After cloning the repository install the package locally to the python environment using one of the following commands.

Linux additional requirements

If you are using a linux system it will be important to install the additional 2 tools:

  1. install mysql_config (for mysqlclient/MySQL connections):

     sudo apt-get install libmysqlclient-dev
    
  2. install Microsofts ODBC driver (for pyodbc/MSSQL connections)

Install Package

python setup.py develop

or

python setup.py -e .

Setup API

Get Template Config

To run the API, settings related to its functionality need to be set. These are located in the file named appconfig.py. By default the source code has an example config named example_appconfig.py but this must be renamed to appconfig.py.

Setup Connection Strings

Interject Data Connections contain an Api Connection String Name. In order to allow the API to connect to a Data Connection, it must be added to the CONNECTIONSTRINGS dictionary object following the format 'conn_string_name' : 'conn_string'. As many connection strings as are desired to serve from the server can be added to this variable.

# Interject Data Connection Strings available to the server
CONNECTIONSTRINGS = {
    'MongoDB_Demo': 'mongodb+srv://apiTestUser:randomhashstring.mongodb.net/demo||demo|Demo'
}

Choose Controllers

In the appconfig.py file the variable, CONTROLLERS is an dictionary object with the types of database handles to be loaded by the flask server. Any of these controllers can be set to false and then will not require their dependent packages to be installed. For example if a user only wanted to use the API with a MySQL database then the controllers could be configured like below, which would not require pandas or pymongo packages to be installed.

# Controllers define what python packages should be imported 
CONTROLLERS = {
    'MySQL' : True,
    'OtherSQL' : False,
    'MongoDB' : False,
    'Pandas' : False
}

Custom Functions

If the MongoDB or Pandas controllers are being utilized, then it is possible to first pull data into a custom python module before sending it as a outgoing response from the server. This functionality utilizes the Pandas python package and more information can be found in the Python Api Custom Functions page.

Running The API

Once settings have been configured, the server can be launched using the built-in flask server (main.py) or via the Twisted Web production ready server (server.py) using the following command:

> python server.py

More Information

For more information visit the source code and readme for the project on gitlab.