After completing the ApiLogicServer create
step, you can view the readme
in the created API Logic Project. The readme
links to this sample tutorial, created from this database..
In this tutorial, we will explore:
-
create - creating the Tutorial project, and opening it your IDE
-
run - we will first run the Admin App and the JSON:API
-
customize - we will then explore some customizations already done for the API and logic, and how to debug them
This tutorial presumes you are running in an IDE - VS Code or PyCharm. Projects are pre-configured for VS Code with .devcontainer
and launch configurations,
so these instructions are oriented around VS Code.
The diagram above summarizes the create / run / customize process. It's a video - click to view.
This tutorial illustrates some key concepts:
Observe that the files for the Admin App and API are models that describe what, not how. This makes it much easier to understand than generating large amounts of code.
The system is designed to enable rebuild
, so you can iterate the data model - without losing your customization. In general, such customizations are kept in separate files than the model files. So, model files can be rebuilt without affecting customzation files.
Please see Quick Start > Express Install, which explains how to:
- Install API Logic Server
- Create the sample Tutorial API Logic Server Project
- Open it in your IDE
- Prepare the projects' Python environment
The prior step established your Python enviroment, so your project is ready to run. We are now ready to explore the Admin App and the API.
To run the Admin App, follow these steps:
-
After completing the Create step, start the server as illustrated below:
- Use the pre-built Launch Configuration to start the server and
- Start the browser to see your Admin App
-
Navigate to
Customer
- Depending on your screen size, you may need to hit the "hamburger menu" (top left) to see the left menu
-
Click the Customer row to see Customer Details
-
Observe the
Placed Order List
tab at the bottom -
Click the first Order row
-
Observe the
Order Detail List
tab at the bottom -
Observe the elements shown in the diagram
- Multi-Page - 2 pages for each table (list, with search, and display)
- Multi-Table - database relationships (typically from foreign keys) used to build master/detail pages
- Automatic Joins - the Order Detail table contains
ProductId
, but the system has joined in theProduct Name
. You can edit theadmin.yaml
file to control such behavior.
-
Close the app (browser), but leave the server running
Key Take-away: instant multi-page / multi-table admin apps, suitable for back office, and instant agile collaboration.
Your API is instantly ready to support ui and integration development, available in swagger, as shown below. JSON:APIs are interesting because they are client configurable to reduce network traffic and minimize organizational dependencies.
The creation process builds not only the API, but swagger so you can explore it. The Admin App Home page provides a link to the swagger, but it doesn't work in VS Code's simple browser. So, we'll launch a new Simple Browser, like this:
- Click View > Command to open the Command Palette
- Enter command:
Simple Browser: Show
- Specify the URL:
http://localhost:5656/api
- Enter command:
- Explore the swagger
- Note: you can drag windows to arrange your viewing area
- (Leave the swagger and server running)
Key Take-away: instant rich APIs, with filtering, sorting, pagination and swagger. Custom App Dev is unblocked.
That's quite a good start on a project. But we've all seen generators that get close, but fail because the results cannot be extended, debugged, or managed with tools such as git and diff.
Let's examine how API Logic Server projects can be customized for both APIs and logic. We'll first have a quick look at the created project structure, then some typical customizations.
The API and admin app you just reviewed above were not customized - they were created completely from the database structure. For the sample project, we've injected some API and logic customizations, so you can explore them in this tutorial, as described below.
Use VS Code's Project Explorer to see the project structure:
Directory | Usage | Key Customization File | Typical Customization |
---|---|---|---|
api |
JSON:API | api/customize_api.py |
Add new end points / services |
database |
SQLAlchemy Data Model Classes | database/customize_models.py |
Add derived attributes, and relationships missing in the schema |
logic |
Transactional Logic | logic/declare_logic.py |
Declare multi-table derivations, constraints, and events such as send mail / messages |
ui |
Admin App | ui/admin/admin.yaml |
Control field display, ordering, etc. |
Let's now explore some examples.
There is no code for the Admin app - it's behavior is declared in the admin.yaml
model file. Alter this file to control labels, hide fields, change display order, etc:
- Open Explorer > ui/admin/admin.yaml
- Find and alter the string
- label: 'Placed Order List*'
(e.g, make it plural) - Click Save
- Find and alter the string
- Launch the app: http://localhost:5656
- Load the updated configuration: click Configuration > Reset
- Revisit Customer > Order to observe the new label
Key Take-away: you can alter labels, which fields are displayed and their order, etc -- via a simple model. No need to learn a new framework, or deal with low-level code or html.
While a standards-based API is a great start, sometimes you need custom endpoints tailored exactly to your business requirement. You can create these as shown below, where we create an additional endpoint for add_order
.
To review the implementation:
- Open Explorer > api/customize_api.py:
- Set the breakpoint as shown
- Use the swagger to access the
ServicesEndPoint > add_order
, and- Try it out, then
- execute
- Your breakpoint will be hit
- You can examine the variables, step, etc.
- Click Continue on the floating debug menu (upper right in screen shot below)
API and UI automation are impressive answers to familiar challenges. Logic automation is a unique answer to a significant and unaddressed problem:
For transaction systems, backend constraint and derivation logic is often nearly half the system. This is not addressed by conventional approaches of "your code goes here".
The logic portion of API Logic server is a declarative approach - you declare spreadsheet-like rules for multi-table constraints and derivations. The 5 rules shown below represent the same logic as 200 lines of Python - a remarkable 40X.
Since they automate all the re-use and dependency management, rules are 40X more concise than code. Like a spreadsheet, rules watch for changes, react by automatically executing relevant rules, which can chain to activate other rules; you can visualize the process here.
Logic consists of rules and conventional Python code. Explore it like this:
- Open Explorer > logic/declare_logic.py:
- Observe the 5 rules highlighted in the diagram below. These are built with code completion.
- Set a breakpoint as shown
- This event illustrates that logic is mainly rules, extensible with standard Python code
- Using swagger, re-execute the
add_order
endpoint - When you hit the breakpoint, expand
row
VARIABLES list (top left)
Internally, rules execute by listening to SQLAlchemy before_flush
events, as described here.
This rule architecture ensures that rules are always re-used across all client applications and integrations. This avoids common "fat client" approaches that embed logic in user interface controllers, which leads to replication and inconsistency.
You can test using standard api and ui test tools. We recommend exploring the Behave framework. This can be used as part of an overall agile approach as described in the Logic Tutorial.
TL;DR - features and test scripts are predefined in the sample; to run them (with the server running):
- Run Launch Configuration
Run Behave Logic
- Run Launch Configuration
Behave Logic Report
- Open
test/api_logic_server_behave/reports/Behave Logic Report.md
The sample Scenarios below were chosen to illustrate the basic patterns of using rules. Open the disclosure box ("Tests - and their logic...") to see the implementation and notes.
For more information, see Testing with Behave.
Let's recap what you've seen:
-
ApiLogicProject Creation and Execution - a database API and an Admin App - created automatically from a database, in moments instead of weeks or months
-
Customizable - the UI, API and Logic - using Visual Studio code, for both editing and debugging
Explore the Logic Tutorial.
VS Code leaves the container and image definitions intact, so you can quickly resume your session. You may wish to delete this. it will look something like vsc-ApiLogicProject...
.