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: 101
  • WebApiUtils.b4xlib
    9.7 KB · Views: 107
  • MiniORMUtils.b4xlib
    13.8 KB · Views: 95
  • WebApiController.jar
    1.5 KB · Views: 105
  • WebApiController.xml
    413 bytes · Views: 99
  • MinimaList API Server (2.05).b4xtemplate
    446.6 KB · Views: 98
  • MinimaListUtils.b4xlib
    6.2 KB · Views: 94
Last edited:

aeric

Expert
Licensed User
Longtime User
Web API Template 2
version: 2.04 beta4
(436KB)
Note: This is still in beta version and for testing B4J 10.00 Snippets feature

What's New?
  • Support B4X Web API Client 1.05
  • Web Front-end with CRUD Demo
  • Changes in ConfigureServer subs order and ConfigurePort is enabled as default
  • Clean up unused code
  • Timezone removed
  • DataController has been removed
  • Methods inside MinimaList class are renamed
  • (new) WebApiUtils.b4xlib (with Code Snippets) replaces Utility and WebUtils modules
  • (new) MinimaListController (version 1.05) replaces WebAPIController
To use this template, copy all the attachments to additional libraries folder and create a new project from B4J menu New.

To seed dummy data, browse to:
http://127.0.0.1:19800/web/?seed=1
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Web API Template 2
version: 2.04 beta 5
Size: 460KB

What's New?
  • (new) Web Front-end with CRUD Demo
  • (new) B4X Web API Client (version 1.05) support -> note: set ConfigureResponse(False)
  • (new) WebApiUtils.b4xlib (version 2.04) merged Utility and WebUtils modules
  • (new) MinimaListUtils.b4xlib (version 0.09)
  • (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
  • (new) ConfigureServer subs are now enabled by passing Boolean value instead of commenting the line
  • (new) Utilize B4J v10.00 Code Snippets feature
  • (new) MinimaListController (version 1.05) -> will be removed
  • (back) WebAPIController (version 1.03)
  • (fixed) ConfigureServer subs order changed
  • (fixed) ConfigurePort
  • (fixed) ConfigurePaths
  • (fixed) ConfigureElements list element indices
  • (fixed) ConfigureHandlers
  • (fixed) ConfigureResponse added SimpleResponseFormat = "Auto"
  • (new) ConfigureResponse added SimpleResponseDataKey (when SimpleResponseFormat = "Map", default is data)
  • (new) JavaScript files for supporting SimpleResponseFormat
  • (new) ReturnApiResponse sub added
  • (new) DatabaseConnector (for SQLite, MySQL, SQL Server, Firebird, PostgreSQL, DBF)
  • (new) SeedData for MinimaList
  • (removed) Timezone setting
  • (removed) DataController class
  • (removed) ReturnSimpleHttpResponse
  • (updated) ReturnHttpResponse
  • (updated) MinimaList subs are renamed
  • (updated) All Controllers
  • (updated) All Handlers
  • Clean up unused code and many other bugs fixes
To use this template, copy all the attachments to additional libraries folder and create a new project from B4J menu New.

To seed dummy data (when using MinimaList), browse to:
http://127.0.0.1:19800/web/?seed=1
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Web API Template 2
version: 2.04
Size: 447KB +

What's New?
  • (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
I am happy to release this major update on this project template!

To use this template, copy all the attachments in post #1 to B4J additional libraries folder and create a new project from B4J menu New. Default database is SQLite.

To seed dummy data (when using MinimaList), 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.
 

aeric

Expert
Licensed User
Longtime User
Web API Template 2
version: 2.05 beta 1
Size: 447KB+

What's New?
  • (new) Use a single public variable SimpleResponse instead of 3 different public variables.
  • (new) WebApiController (version 1.04)
  • (new) MiniORMUtils (version 1.01)
  • (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.
  • (remove) Dependency to MinimaList (You need to add MinimaListUtils manually if you want to use it).
    Build configuration and conditional compilation codes are kept.

Process_Globals
Version 2.04:
Public SimpleResponse           As Boolean
Public SimpleResponseFormat     As String
Public SimpleResponseDataKey    As String
Version 2.05:
Public SimpleResponse           As SimpleResponse

ConfigureResponse sub
Version 2.04:
SimpleResponse = True
SimpleResponseFormat = "Auto"    ' no conversion
Version 2.05:
SimpleResponse.Enable = True
SimpleResponse.Format = "Auto"    ' no conversion

ShowPage sub in CategoryController
Version 2.04:
    If Main.SimpleResponse Then
        If Main.SimpleResponseFormat = "Map" Then
            ' Script file is using dash
        Else
            ' Script file is using dash       
        End If
    Else
            ' Script file is using dash
    End If
Version 2.05:
    If Main.SimpleResponse.Enable Then
        If Main.SimpleResponse.Format = "Map" Then
            ' Script file is now using period
        Else
            ' Script file is now using period
        End If
    Else
        ' Script file is now using period
    End If

JavaScript file name changed (one example)
Version 2.04:
webapicategory-simple-map.js
Version 2.05:
webapi.category.simple.map.js

Edit: Attachments are now deleted which has caused confusion.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Web API Server
version: 2.05
Size: 442KB +

What's New
  • (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.
 

Xela

Member
Licensed User
Hi Aeric and many compliments on your work

I wanted to try version 2.05 but it reports me this lack.

1709994112807.png



Can you suggest me a solution?
Many thanks
 

aeric

Expert
Licensed User
Longtime User
Thank you for your reply

from here
View attachment 151594

to here
View attachment 151595

Is not correct?
Beta 2 is an old version.
Please download from the first post which is the stable version.

By the way, you should put all libraries into B4J Additional folder. MiniORMUtils can put inside B4X Additional folder.

1710000508154.png


1710000681856.png


1710000766689.png


Edit: I have removed the beta version attachments. Sorry for causing any confusion.
 
Last edited:
Top