Python API Setup
Estimated reading time: 7 minutesOverview
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
- Python >= 3.9
Once Python is installed the dependencies for the Interject API include:
- poetry (for build)
- typer
- fastapi
- uvicorn
- pydantic
- pyjwt
- pandas
- sqlalchemy
- pyodbc
- loguru
- requests
- pyOpenSSL (when running API with HTTPS)
Note: For the most current version requirements of the dependencies, see the repo.
Get The Code
Start by cloning the repository to your system.
git clone https://github.com/GoInterject/ids-python-api
Note: If this repo is private and you need access, please contact us. It will be public soon.
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:
-
Install mysql_config (for mysqlclient/MySQL connections):
sudo apt-get install libmysqlclient-dev
-
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 a 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 GitHub.