Share My Creation [Project Template] Web API Server 2

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

Database: MySQL, SQLite

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.08).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
Tutorials:
  1. Getting Started
  2. Download File Example
  3. Vue 3 CRUD Example
Live:
Learn:

Version History:
  • (new) WebApiUtils v2.08
  • (new) MiniORMUtils v1.13
  • (new) WebAPIController v1.07
  • (change) Use DbFile instead of DbName for SQLite in config.ini
  • (change) Move code from ConfigureDatabase sub to Initialize sub in DatabaseConfiguration class
  • (remove) Delete build configurations and code for Firebird, PostgreSQL and MS SQL
  • (new) WebApiUtils v2.07
  • (new) MiniORMUtils v1.11
  • (new) WebAPIController v1.06
  • (new) Main module
    • SHOW_API_ICON to control API icon visible on frontend top navbar
  • (new) ServerConfiguration class
    • setEnableHelp to disable API Documentation and icon in frontend top navbar
    • setSimpleResponse replaces 3 old variables
  • (change) ServerConfiguration class
    • rename some variables
    • change some Public subs to Private
  • (change) Some index.html files deleted
  • (new) WebApiUtils v2.06
  • (new) MiniORMUtils v1.10
  • (new) WebAPIController v1.05
  • (new) Main module is cleaner
  • (new) Added ServerConfiguration class
  • (new) Added DatabaseConfiguration class
  • (new) Added empty index.html files into assets folders
  • (new) FindController - case insensitive and wildcard search
  • (change) Table tbl_category renamed to tbl_categories
  • (change) ServerConfiguration Update sub renamed to Finalize
  • (change) better handling of default values in config.ini
  • (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
  • (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

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

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.08).b4xtemplate
    441.2 KB · Views: 154
  • WebApiUtils.b4xlib
    9.4 KB · Views: 141
  • MiniORMUtils.b4xlib
    14.3 KB · Views: 143
  • WebApiController.b4xlib
    4.5 KB · Views: 92
Last edited:

aeric

Expert
Licensed User
Longtime User
Web API Server
version: 2.06

What's New
(new) WebApiUtils v2.06
(new) MiniORMUtils v1.10
(new) WebAPIController v1.05
(new) Main module is cleaner
(new) Added ServerConfiguration class
(new) Added DatabaseConfiguration class
(new) Added empty index.html files into assets folders
(new) FindController - case insensitive and wildcard search
(change) Table tbl_category renamed to tbl_categories
(change) ServerConfiguration Update sub renamed to Finalize
(change) better handling of default values in config.ini
(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
 

aeric

Expert
Licensed User
Longtime User
Web API Server
version: 2.07

What's New
  • (new) WebApiUtils v2.07
  • (new) MiniORMUtils v1.11
  • (new) WebAPIController v1.06
  • (new) Main module
    • SHOW_API_ICON to control API icon visible on frontend top navbar
  • (new) ServerConfiguration class
    • setEnableHelp to disable API Documentation and icon in frontend top navbar
    • setSimpleResponse replaces 3 old variables
  • (change) ServerConfiguration class
    • rename some variables
    • change some Public subs to Private
  • (change) Some index.html files deleted
For more details, check GitHub changes
 

AHilton

Active Member
Licensed User
Longtime User
I'm just now looking into this and have added the Optional Filters: BasicAuthFilter.bas and JWTAuthFilter.bas. They have a dependency on a Utility module but I can't find it.
 

AHilton

Active Member
Licensed User
Longtime User
Then what is the equivalent of Utility.RequestBasicAuth(Auths) in WebApiUtils (v2.07) ? I'm not seeing it and there are no Sub comments in WebApiUtils.
 

aeric

Expert
Licensed User
Longtime User
Then what is the equivalent of Utility.RequestBasicAuth(Auths) in WebApiUtils (v2.07) ? I'm not seeing it and there are no Sub comments in WebApiUtils.
It has been commented.

You can modify WebApiUtils.bas inside the b4xlib. It will depends on StringUtils.

Take note that once authenticated in Web browser, the session is difficult to unauthenticate.

It is not suitable for Web app but can be use for mobile or desktop clients.
 

aeric

Expert
Licensed User
Longtime User
Web API Server
version: 2.08

What's New
  • (new) WebApiUtils v2.08
  • (new) MiniORMUtils v1.13
  • (new) WebAPIController v1.07
  • (change) Use DbFile instead of DbName for SQLite in config.ini
  • (change) Move code from ConfigureDatabase sub to Initialize sub in DatabaseConfiguration class
  • (remove) Delete build configurations and code for Firebird, PostgreSQL and MS SQL
Updates:
RequestBasicAuth(Auths) is now available in WebApiUtils (v2.08)
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Optional Modules
  1. JSON Web Tokens
  2. Encryption

Optional Filters​

  1. BasicAuth
  2. JWTAuth
Updates: These modules have been deprecated. They are no longer working without modifications. If you want to work with these modules, please post to a new thread.

If anyone need these classes and filters, I will update them in the future when I find time.

You can also check [Project Template] User Login Server which implements API Key + Access Token.
 
Top