Share My Creation [Project Template] [Web] API Server 2

Web API Server​

Version: 2.05
Description: Create Web API Server using B4X template

Web API Server.png


Template 1: MinimaList API Server.b4xtemplate
Depends on:
  • WebApiUtils.b4xlib
  • MinimaListUtils.b4xlib

Template 2: Web API Server.b4xtemplate
Depends on:
  • WebApiUtils.b4xlib
  • MiniORMUtils.b4xlib

Optional:
Add WebAPIController.jar and WebAPIController.xml to B4J Additional Library folder to generate Web and API Controllers.

For older version, see webapi-b4j (v1.16) https://www.b4x.com/android/forum/threads/web-api-template.133764/

GitHub: https://github.com/pyhoon/webapi-2-b4j

Tutorials:
  1. Getting Started
  2. Download File Example
  3. Vue 3 CRUD Example
  4. Using Web API Controller
  5. Using MinimaList Controller
  6. Using #Plural tag

Features​

  • Redesign Architecture
    • The core handler - ApiHandler, acts like BaseController or Routes class
    • ApiHandler routes the RequestURI to controllers e.g /web/api/v2/data
    • HelpHandler (optional) generates API documentation for easy debugging without external tools or clients which embed tokens in request header. HelpHandler is now scanning through controllers class for APIs to list in the documentation instead of reading handlers from b4j project main module in version 1.x
    • Web and API paths can be changed in config.ini
    • Versioning can be enabled or disabled
    • Simple JSON response (Map or List)
    • Session can be toggled
    • Cookies can be toggled
    • Welcome message can be toggled
    • One stop ConfigServer sub to control all the settings
    • Default endpoint name is based on controller's name (e.g /web/api/v2/product for ProductController)
    • Override endpoint name using #Plural (e.g /web/api/v2/products)
    • Custom version name using #Version (e.g v2, live, demo, dev, staging)
    • Description is set using #Desc (i.e no more using #Desc1, #Desc2 or Literals that was very confusing in version 1.x)
    • API endpoint can be hidden using #Hide
    • INTRODUCING: MinimaList -> store as Map/List. API server can run without database (or optionally persist as KeyValueStore).

Code Example​

B4X:
Private Sub GetCategory (id As Long)
    ' #Plural = Categories
    ' #Version = v2
    ' #Desc = Get a Category by id
    ' #Elements = [":id"]
 
    Dim M1 As Map = Main.CategoryList.Find(id)
    If M1.Size > 0 Then
        HRM.ResponseCode = 200
    Else
        HRM.ResponseCode = 404
    End If
    HRM.ResponseObject = M1
    ReturnApiResponse
End Sub

What's New
Version: 2.05
  • (new) MinimaList API Server (version 2.05) as a separate template
  • (new) MiniORMUtils (version 1.07)
  • (new) MinimaListUtils (version 1.02)
  • (new) WebApiController (version 1.04)
  • (new) Use a single public variable SimpleResponse instead of 3 different public variables.
  • (update) HelpHandler - JavaScript files for search is no longer generated during compilation.
    The files are now keep inside assets/js folder instead. SimpleResponse and standard JSON are supported.
    Thanks to projects Ticketing Support System and Shao.
  • (update) Some JavaScript files have been renamed using period instead of dash.
Version: 2.04
  • (new) Web Front-end with CRUD demo (similar to version 1)
  • (new) B4X Web API Client (version 1.05) supported (note: set ConfigureResponse(False))
  • (new) WebApiUtils.b4xlib (version 2.04) (Utility and WebUtils modules merged)
  • (new) WebAPIController (version 1.03) is backed
  • (new) MinimaListUtils.b4xlib (version 1.00)
  • (new) Database support (tested with SQLite and MySQL)
  • (new) Build Standalone Package supported
  • (new) Additional library for jdbc connector is set by selecting a Build configuration during Debug
  • (new) ConfigureServer subs are now enabled by passing Boolean value instead of commenting the line
  • (new) Utilize B4J v10.00 Code Snippets feature
  • (new) DatabaseConnector (for SQLite, MySQL, SQL Server, Firebird, PostgreSQL, DBF)
  • (new) SeedData for MinimaList
  • (new) ConfigureResponse added SimpleResponseDataKey (when SimpleResponseFormat = "Map", default is data)
  • (new) JavaScript files for supporting SimpleResponseFormat
  • (new) ReturnApiResponse sub added
  • (fixed) ConfigureServer subs order changed
  • (fixed) ConfigurePort
  • (fixed) ConfigurePaths
  • (fixed) ConfigureElements list element indices during Debug
  • (fixed) ConfigureHandlers
  • (fixed) ConfigureResponse added SimpleResponseFormat = "Auto"
  • (removed) Timezone setting
  • (removed) DataController class
  • (removed) ReturnSimpleHttpResponse
  • (removed) MinimaListController (version 1.05) -> succeeded by WebAPIController
  • (updated) MinimaList subs are renamed
  • (updated) ReturnHttpResponse
  • (updated) All Controllers
  • (updated) All Handlers
  • (other) Clean up unused code and many other bugs fixes

To seed dummy data in MinimaList API Server, browse to:
http://127.0.0.1:19800/web/?seed=1

This project has been tested many many times. Please report bugs if you found any.



Special thanks to donors of this project:
  1. @yaqoob
  2. @Czampo


 

Attachments

  • Web API Server (2.05).b4xtemplate
    442.2 KB · Views: 98
  • WebApiUtils.b4xlib
    9.7 KB · Views: 104
  • MiniORMUtils.b4xlib
    13.8 KB · Views: 92
  • WebApiController.jar
    1.5 KB · Views: 102
  • WebApiController.xml
    413 bytes · Views: 95
  • MinimaList API Server (2.05).b4xtemplate
    446.6 KB · Views: 95
  • MinimaListUtils.b4xlib
    6.2 KB · Views: 90
Last edited:

aeric

Expert
Licensed User
Longtime User

Optional Modules​

  1. DataConnector (connect to SQLite, MySQL databases)
  2. JSON Web Tokens
  3. MinimaList
  4. MiniORM
  5. Encryption

Optional Filters​

  1. CORS
  2. BasicAuth
  3. JWTAuth
Optional Libraries
  1. WebApiController
  2. MinimaListController
 

Attachments

  • DataConnector.bas
    8.4 KB · Views: 212
  • JSONWebToken.bas
    4.6 KB · Views: 166
  • Encryption.bas
    1.6 KB · Views: 176
  • CorsFilter.bas
    2.2 KB · Views: 171
  • BasicAuthFilter.bas
    1.8 KB · Views: 165
  • JWTAuthFilter.bas
    1.4 KB · Views: 173
  • MiniORM.bas
    12.2 KB · Views: 140
  • Web API Server (2.03).b4xtemplate
    433.6 KB · Views: 96
Last edited:

aeric

Expert
Licensed User
Longtime User
A small issue i.e. I forgot to update the Client ID.

Change line #96 in Main module to following:
B4X:
AUTH.CLIENT_ID = "web-202301151435-web-api-200.computerise.net"        ' username

Updates: The issue is now fixed in GitHub repo.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
I may be wrong.
Does anyone really use my Web API Template?
I have the feeling not much interest on this project. I don't see any discussion or question on the issue in this project. I feel it just a pet project that I am the only one feel good about it and provide no valuable real life use cases. 🥲

But...
The fact is I am powering my production apps with this template.
 

Davisbc

Member
Licensed User
I am try to use it as the foundation for a data collection app. The mobile collects data and then syncs with the web api. I started using the v1 template. It worked, but I found it difficult to try and convert to my database. I decided to start over and then found the v2 beta. I had to comment the auth code to make it work. from your comments, that part is a work in progress. My use case is an api pack end to update a database and maintain a transaction log. I have to believe it’s a common use case. All the best.
 

aeric

Expert
Licensed User
Longtime User
I believe v1 and v2 beta 3 have provided the foundation to build any Web API server. What I am adding are just on the implementation of the front end.

The security part in v2 can be ignored as I am actually using Session for log in user instead of JWT token.

Please ask any question if you found any issue.
I know there are some known issues and waiting someone to report.
One of them are usage of comment tags in HelpHandler but it is not really a big issue and won't affect the overall system.

When no one is reporting then I get the feeling no one is using it.
 

Davisbc

Member
Licensed User
Thanks. I have a bit more cleanup of the b4a before I plan on working the backend again. That’s why I was asking when you planned on updating the template so I would start with a clean code. Cheers
 
Top