Instant APIs With Copilot and API Logic Server
Describe a database to Copilot, and create a running API and Admin App in moments. Or, use an existing database. Customize in your IDE with Rules and Python.
Join the DZone community and get the full member experience.
Join For FreeCreating an API and an admin app using a framework takes too long, and is far too complex. AI and automation can create systems in minutes rather than weeks or months, dramatically simpler, and fully customizable with tools and approaches you already know.
In this tutorial, we'll show how to create a complete system using VS Code, Copilot, and API Logic Server (open source). We'll then add business logic with rules, and use Python to add a custom endpoint and Kafka integration. Links are provided so you can execute these steps on your own.
Overview
As shown below, you can submit a Natural Language description of a database to Copilot. This creates a Python data model (SQLAlchemy classes).
You then use API Logic Server CLI to create an executable project from the model. Alternatively, you can create a project by identifying an existing database.
The project is executable, providing an API and an admin app, enabling agile collaboration and unblocking custom app dev.
Figure 1: Overview
Setup
To begin, install Python and VSCode. Optionally, install Copilot: it's moderately priced and you can execute this tutorial without it. But, it provides the Natural Language services shown here - it's quite a lot of fun to explore, so you might just want to splurge and acquire it.
Then, install the API Logic Server and start it:
python3 -m venv venv # windows: python -m venv venv
source venv/bin/activate # windows: venv\Scripts\activate
python -m pip install ApiLogicServer
ApiLogicServer start
This will launch the API Logic Server in VSCode. We've moved the Copilot chat pane to the right.
Figure 2: API Logic Manager in your IDE
1. Create Your Model With Copilot
The README page includes the Natural Language Text to supply to Copilot; paste it, and press enter. It's shown in the diagram below in dark gray ("Use SQLAlchemy to...").
Copilot creates the SQLAlchemy model code.
Paste the generated code into a new model file called sample_ai.py
(step 2 in Figure 3 below):
Figure 3: Creating a project with Copilot and API Logic Server
2. Create a Project With API Logic Server
Create your project (step 3 in Figure 3 above) by entering the following into the bottom terminal pane (als
is a synonym for ApiLogicServer
):
als create --project-name=sample_ai --from-model=sample_ai.py --db-url=sqlite
API Logic Server uses SQLAlchemy (a popular Python ORM) to create a database from the Copilot model and then creates a fully formed project by reading the database schema. The project includes your data model classes, your API, and your admin app, fully configured for execution.
Here the target database is SQLite; the same scenario will work for other databases as well, but you will need to replace "sqlite
" with a full URI of your database.
Create Project From Existing Database
Alternatively, if you don't have Copilot, you can use an existing database, and create your project using the pre-installed SQLite database (1b in Figure 1):
als create --project-name=sample_ai --db-url=sqlite:///sample_ai.sqlite
3. Microservice Automation: Executable Project
In either case, API Logic Server creates a fully formed project, ready to run, and launches it in another instance of VSCode:
Figure 4: Created Project (new VSCode instance)
Press F5 to start the server; launch the created admin app in your browser to explore your data and the API:
- The admin app, based on React Admin, provides data query/update services, with multi-table support for automatic joins and page navigations. This can kick-start agile business user collaboration, and provide back-office data functions.
- A JSON API, provides retrieval/update services, including support to choose columns and related data. This unblocks custom app dev, which is often compressed into the end of the project while waiting on custom API development.
Compare automation to framework-based development: With a framework, you are ready to code. With automation, you are ready to run.
Figure 5: Microservice Automation - created admin app and API
So, we have working software: an admin app, for business user collaboration; an API, to unblock custom app dev.
It was fast - only took a few moments - and simple - did not require months to learn a framework.
4a. Customize With Rules: Logic Automation
Well, "fast and simple" is great, but it's little more than a stupid pet trick without logic and security. That's often nearly half the work of a transactional database application.
API Logic Server contains a rule engine. You can declare rules in Python, using IDE code completion services.
It also provides value: spreadsheet-like rules reduce logic code (half the app) by 40X.
But, we can have much more fun. As shown below, we can ask Copilot to create these rules for us, and paste them into a pre-created file:
Figure 6: Creating Rules with Copilot
These 5 lines of code look just like the requirements, so the level of abstraction is remarkably high. These 5 declarative rules would have required 200 lines of traditional procedural code.
And they are executable. They listen for SQLAlchemy ORM events, and fire in response to the actual changes in the transaction. Rules (and their overhead) are pruned if their referenced data is unchanged.
Rules are debuggable. Standard logging depicts each rule firing, including the state of the row. And, you can use your debugger.
Similar declarative rules are provided for row-level security, based on a user's roles. Authorization information can be obtained from a SQL database, or corporate stores such as LDAP or Active Directory.
4b. Customize With Python
Automation is great, but let's face it: you can never automate everything. It's mandatory to have a customization capability that is standards-based — using our favorite IDE, standard languages like Python, and standard frameworks like SQLAlchemy and Flask.
Let's examine 2 typical customizations: a custom endpoint, and Kafka integration.
Custom API Endpoint
Imagine accepting orders in a designated format from a B2B partner. The custom endpoint below performs the transformation using order_b2b_def
(a declarative mapping definition, not shown), and saves it. This automatically activates the business rules above to check credit.
Figure 7: Customization - Adding a Custom Endpoint
Custom Logic for Kafka Integration
Let's further imagine that accepted orders must be further transformed, and sent to shipping via a Kafka message. The screenshot below illustrates that, in addition to rules, you can provide Python code for business logic. Here you have all the power of Python libraries: Flask, SQLAlchemy, Kafka, etc.
Figure 8: Customization - Kafka Integration
5. Deploy
Value is not realized until the system is deployed, whether for final production, or early collaboration with stakeholders. API Logic Server creates scripts to containerize your project, and deploy to Azure with Docker Compose:
Summary
The screenshots above illustrate remarkable agility. This system might have taken weeks or months using conventional frameworks.
But it's more than agility. The level of abstraction here is very high, bringing a level of simplicity that empowers you to create microservices - even if you are new to Python or frameworks such as Flask and SQLAlchemy.
Instead of a complex framework, it's more like an appliance — just plug into your idea or existing database, and you have an executable, customizable project.
There are 4 key elements that deliver this speed and simplicity:
- Natural Language Processing: Even savvy SQL programmers welcome syntax help. It's a dream come true to start from Natural Language, as provided by Copilot.
- Microservice automation: Instead of slow and complex framework coding, just plug into your database for an instant API and Admin App.
- Logic automation with declarative rules: Instead of tedious code that describes how logic operates, rules express what you want to accomplish, and reduce the backend half of your application by 40x.
- Extensibility: Finish the remaining elements with your IDE, Python, and standard packages such as Flask and SQLAlchemy.
Automation empowers more people, to do more.
Opinions expressed by DZone contributors are their own.
Comments