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

Web API Template 2​

Version: 2.03

1683916091359-png.141987
1683827359648.png


Build REST API Server Using B4X Template
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.

Depends on following libraries:
  • ByteConverter
  • jServer
  • Json
  • JavaObject
  • KeyValueStore

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/item for ItemController)
    • Overide endpoint name using #Plural (e.g /web/api/v2/items)
    • 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 GetOneItem (id As Long)
    ' #Version = v2
    ' #Desc = Read one Item in MinimaList
    ' #Elements = [":id"]
    Dim M1 As Map = Main.MinimaItem.Find(id)
    HRM.ResponseCode = 200
    HRM.ResponseData.Initialize
    HRM.ResponseData.Add(M1)
    Utility.ReturnHttpResponse(HRM, Response)
End Sub



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


 

Attachments

  • Web API Server (2.03).b4xtemplate
    433.6 KB · Views: 121
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. MinimaListController
 

Attachments

  • DataConnector.bas
    8.4 KB · Views: 75
  • JSONWebToken.bas
    4.6 KB · Views: 66
  • Encryption.bas
    1.6 KB · Views: 68
  • CorsFilter.bas
    2.2 KB · Views: 63
  • BasicAuthFilter.bas
    1.8 KB · Views: 63
  • JWTAuthFilter.bas
    1.4 KB · Views: 64
  • MiniORM.bas
    12.2 KB · Views: 41
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