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

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

Sample project: Support Ticketing System

Web API Server.png


If you don't want to connect to any SQL database, see MinimaList API Server
For older version, see Web API Server (v1.16)


Template:
  • Web API Server (2.05).b4xtemplate
Depends on:
GitHub:
Features:
  • ApiHandler and WebHandler are used for routing to Controller classes
  • HelpHandler (optional)
    • API documentation is generated automatically. You don't need external tools (e.g Postman or Swagger) for testing.
    • Access tokens can be embeded into request header in HelpHandler.
    • Controller classes are added to a list in Main module to show in the documentation.
  • Configuration
    • Web and API paths
    • Versioning
    • Simple JSON Response (Map or List)
    • Session
    • Cookies
    • Welcome message
  • Endpoint
    • Endpoint name is based on controller's name by default e.g ProductsController produces /web/api/v2/products
    • Endpoint name can be overridden by using #Name tag e.g /web/api/v2/product
    • Custom version name using #Version tag e.g v2, live, demo, dev, staging
    • Description in documentation is set using #Desc tag (in Web API v1, it was set by #Desc1, #Desc2 or Literals that was very confusing)
    • API endpoint can be hidden using #Hide tag
  • Clients
Code example:
B4X:
Private Sub GetCategory (id As Long)
    ' #Version = v2
    ' #Desc = Read one Category by id
    ' #Elements = [":id"]

    DB.Table = "tbl_category"
    DB.Find(id)
    If DB.Found Then
        HRM.ResponseCode = 200
        HRM.ResponseObject = DB.First
    Else
        HRM.ResponseCode = 404
        HRM.ResponseError = "Category not found"
    End If
    DB.Close
    ReturnApiResponse
End Sub

Tutorials:
  1. Getting Started
  2. Download File Example
  3. Vue 3 CRUD Example
Live:

Version History:
  • (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.
  • (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
This project has been tested many many times. Please report bugs if you found any
Post your question in a new thread.




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


 

Attachments

  • Web API Server (2.05).b4xtemplate
    442.2 KB · Views: 108
  • WebApiUtils.b4xlib
    9.7 KB · Views: 116
  • MiniORMUtils.b4xlib
    13.8 KB · Views: 103
  • WebApiController.jar
    1.5 KB · Views: 112
  • WebApiController.xml
    413 bytes · Views: 106
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:

aeric

Expert
Licensed User
Longtime User
In version 2.06, Main module is more compact.

Most of the code has moved to 2 new classes, ServerConfiguration and DatabaseConfiguration.
In most cases, you don't need to modify ServerConfiguration.
To create the database tables, edit CreateDatabase sub inside DatabaseConfiguration.

The code for Main is as follow:

Main:
Sub Process_Globals
    Public Server                 As Server
    Public Config                 As Map
    Public Element                As Element
    Public Controllers            As List
    Public SimpleResponse         As SimpleResponse
    Public DBConnector            As DatabaseConnector
    Public PRINT_FULL_REQUEST_URL As Boolean
    Public SESSIONS_ENABLED       As Boolean
    Public COOKIES_ENABLED        As Boolean
    Public COOKIES_EXPIRATION     As Long
    Public Const VERSION          As String = "2.06"
    'Public Const PREFIX          As String = "WebAPI_v2_" ' use for cookies and sessions
End Sub

' <link>Open in browser|http://localhost:19800/web</link>
Sub AppStart (Args() As String)
    Server.Initialize("")
    Config.Initialize
    SimpleResponse.Initialize
   
    ' Read environment settings
    If Not(File.Exists(File.DirApp, "config.ini")) Then
        File.Copy(File.DirAssets, "config.example", File.DirApp, "config.ini")
    End If
    Config = WebApiUtils.ReadMapFile(File.DirApp, "config.ini")
    Config.Put("VERSION", VERSION)
    'Config.Put("PREFIX", PREFIX)
   
    Dim sv As ServerConfiguration
    sv.Initialize
    sv.EnableCORS = True
    sv.StaticFilesFolder = File.Combine(File.DirApp, "www")
    sv.Update
    sv.ShowWelcomeText
   
    Dim db As DatabaseConfiguration
    db.Initialize
    db.ConfigureDatabase
   
    Server.Start
   
    ' Show Controllers in Documentation
    Controllers.Initialize
    Controllers.Add("CategoriesController")
    Controllers.Add("ProductsController")
    Controllers.Add("FindController")
   
    PRINT_FULL_REQUEST_URL = True
    StartMessageLoop
End Sub

Reminder: Please do not send me PM. Post your question in a new thread.
 

aeric

Expert
Licensed User
Longtime User
Web API Server
version: 2.06 beta 2
Depends on MiniORMUtils (version 1.10)

What's New
  • (clean) Main module
  • (new) ServerConfiguration class
  • (new) Database Configuration class
  • (new) Controllers are created using WebAPIController v1.05
  • (new) FindController - case insensitive and wildcard search
  • (change) Table tbl_category renamed to tbl_categories
  • (fix) bug in v2.06 beta 1, no database set by default in config.ini
edit: a bug just found in PostProduct line#233 in ProductsController. Will be fix on next version.
B4X:
'Values.Add(WebApiUtils.CurrentDateTime) ' bug
Values.Add(data.Get(key)) ' fix
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Web API Server
version: 2.06 beta 3
Depends on MiniORMUtils (version 1.10)

What's New
  • (clean) Main module
  • (new) ServerConfiguration class
  • (new) Database Configuration class
  • (new) Controllers are created using WebAPIController v1.05
  • (new) FindController - case insensitive and wildcard search
  • (change) Table tbl_category renamed to tbl_categories
  • (fix) bug in v2.06 beta 1, no database set by default in config.ini
  • (fix) bug in v2.06 beta 2, PostProduct line#233 in ProductsController causing all data inserted as Timestamp
 

Attachments

  • Web API Server (2.06 beta 3).b4xtemplate
    443.7 KB · Views: 2
Top