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
Web API Template 2 (MinimaList Edition) - NO Database Needed
version: 2.00
Uploaded in post #1 (only 261KB)

This is a very minimum template where you don't even need to connect to a database to work. Optionally you can start with keyValueStore to persist the model. This will be a foundation to more complex Web API projects.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Web API Template 2
version: 2.01
Uploaded in post #1 (only 260KB)

What's New?
  • Added: IndexController - routes to default home page (index.html template is also added)
  • Added: WebHandler - call IndexController
  • Added: CorsFilter
  • Improved: ApiHandler is now easier to use - A new RouteData sub can be use to route to other methods (e.g RouteDataGet and RouteDataPost)
  • Note: MinimaList data is now not persist using KeyValueStore by default. Meaning, data is reset when server is restarted. You can uncomment ConfigureKeyValueStore in ConfigureServer sub to persist the data.

1683916091359.png
 

byz

Member
Licensed User
虽然还没有用你的webapi,但是一直有关注进度的。从帖子里给出的代码看,好像需要配置很多,不知道是否能够简单易用。
 

aeric

Expert
Licensed User
Longtime User
虽然还没有用你的webapi,但是一直有关注进度的。从帖子里给出的代码看,好像需要配置很多,不知道是否能够简单易用。
Please start a new thread in Chinese forum.
I will provide more examples and tutorials in the future.

请在中文论坛开一个新帖。
以后我会提供更多的例子和教程。
 

aeric

Expert
Licensed User
Longtime User
A tutorial is added.
 

aeric

Expert
Licensed User
Longtime User
Web API Template 2
version: 2.02
Uploaded in post #1 (432KB)

What's New?
  • Improved: ConfigureHandlers in Main module
  • Improved: More comments and integration guide in Main module
  • Improved: Removed unused global variables in Main module
  • Improved: Group unused Configurations in Main module
  • Improved: ApiHandler minor fixes
  • Improved: DataController minor fixes
  • Added: ReadConfig sub in Main module
  • Added: Shortcut link in AppStart by hovering mouse pointer to open the default page
  • Added: 2 fontawesome woff files to fix warnings in web dev tools (increases file size)
1684315402848.png
 

aeric

Expert
Licensed User
Longtime User
There is a bug in v2.02
Will update it soon Updated v2.02.1 in post #1

Note: DataController is now reading the item id instead of the list item index. This is easier to work with the vue app in Tutorial Example 2.

I need to add
B4X:
Minima.Initialize
before
B4X:
ConfigureServer
or else the object is not initialized.

B4X:
Sub AppStart (Args() As String)
    srvr.Initialize("")       ' Create the server
    ReadConfig                ' Read environment settings
    Minima.Initialize         ' Initialize MinimaList
    ConfigureServer           ' Configure the server
    srvr.Start                ' Start the server
    StartMessageLoop
End Sub
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Web API Controller (use with MinimaList and KeyValueStore)
version: 1.00
Attached in post #2 (2KB)

Deprecated: Check the new tutorial [Web API 2] Tutorial - Using Web API Controller

How to use:
  1. Unzip the jar and xml file to B4J Additional Library folder
  2. Find and check "WebAPIController" in B4J Libraries Manager
  3. Go to menu Project > Add New Module > Class Module and select "Web API Controller"
  4. Give a name e.g "UserController"
  5. A boilerplate controller is added with subs contains the word "Item"
  6. You can use Quick Search to rename the word "Item" to "User"

Working with MinimaList
  1. You need to create a new MinimaList object in Main module
  2. In Process_Globals sub of Main module, add the following line:
    B4X:
    Public MinimaUser As MinimaList
  3. Inside AppStart sub, initialize the object
    B4X:
    MinimaUser.Initialize
  4. In ConfigureControllers sub, add the controller so you can check with the API documentation
    B4X:
    Public Sub ConfigureControllers
        Controllers.Initialize
        Controllers.Add("DataController")
        Controllers.Add("UserController")
    End Sub
  5. Inside UserController class, using Quick Search tab, replace the word "Item" to "User", "Minima" to "MinimaUser"
  6. Go to ApiHandler, inside ProcessRequest sub, add a new route
    B4X:
    Select ControllerElement
        Case "data"
            RouteData(ApiVersionElement, ControllerIndex, FirstIndex, SecondIndex)
            Return
        Case "user"
            RouteUser(ApiVersionElement, ControllerIndex, FirstIndex)
            Return
    End Select
  7. You can clone the code for RouteData (and other related subs) to RouteUser
  8. You can use Quick search, highlight the copied code and replace the word "Data" to "User" using the "In Selection" button
  9. Adjust the parameter or delete SecondIndex if it is not used.
  10. Test the new API using the documentation page.
This is some work. Hopefully the steps are more simplified in the future.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Web API Template 2
version: 2.03
Uploaded in post #1 (434KB)

What's New?
  • Improved: ApiHandler is now more cleaner. All logic should put in controller class. ApiHandler will call the Route method without any parameter of the controller.
  • Changed: Sub ConfigureResponse is now disabled. JSON Response is now follow "standard" format by default, instead of "simple" format. You can still enable it.
B4X:
Select ControllerElement
    Case "data"
        Dim Data As DataController
        Data.Initialize(Request, Response)
        Data.Route
        Return
    Case "item"
        Dim Item As ItemController
        Item.Initialize(Request, Response)
        Item.Route
        Return
End Select
 

Xfood

Expert
Licensed User
I already answered in post #13.
I don't share the blog system as Web API Template 2.0 but will consider to publish as another product.
always with the same logic you could create a support ticket system, to manage helpdesk calls, I would be willing to buy such a source, and I also believe many in this community would benefit from it
 

aeric

Expert
Licensed User
Longtime User
always with the same logic you could create a support ticket system, to manage helpdesk calls, I would be willing to buy such a source, and I also believe many in this community would benefit from it
 

aeric

Expert
Licensed User
Longtime User
A new version of MinimaList is posted in code snippets
 

aeric

Expert
Licensed User
Longtime User
Web API Template 2
version: 2.04 beta1
(443KB)
Note: This is still in beta version

What's New?
  • Support B4X Web API Client
  • Web Front-end with CRUD Demo
  • MinimaListController replaced WebAPIController

For Web API Client (1.05), modify the following line in B4XMainPage class:
B4X:
'Private URL As String = "http://192.168.50.42:19800/v1/"
Private URL As String = "http://192.168.50.42:19800/web/api/v2/"
 

Attachments

  • Web API Server (2.04 beta1).b4xtemplate
    442.9 KB · Views: 80

aeric

Expert
Licensed User
Longtime User
Web API Template 2
version: 2.04 beta2
(442KB)
Note: This is still in beta version

What's New?
  • Support B4X Web API Client 1.05
  • Web Front-end with CRUD Demo
  • MinimaListController replaced WebAPIController
  • 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
Seed dummy data by calling:
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Web API Template 2
version: 2.04 beta3
(435KB)
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
Edit: Check version 2.04 beta 4
 
Last edited:
Top