Share My Creation Web API Template 2 (Beta)

Web API Template 2.0​

Version: 2.00 beta 3

Build REST API Server Using B4X Template

1673798103055.png

More screens:
1673799024280.png

1673798980163.png

1673798200396.png

1673798225035.png

1673798323186.png

1673798368664.png

1673798781924.png

1673798830464.png

1673798884827.png

1673798914908.png

Depends on following libraries:
  • ByteConverter
  • Encryption (external library)
  • JavaObject
  • jNet
  • jRandomAccessFile
  • jServer
  • Json
  • jSQL
  • jStringUtils
For older version, see webapi-b4j (v1.15) https://www.b4x.com/android/forum/threads/web-api-template.133764/

Features​

  • Security is main focus in this version
    • JSON Web Tokens (JWT) Authentication (provides access and refresh tokens)
    • Basic Authentication (username-password pair)
    • Token Authentication (testing)
    • Cross-site request forgery (CSRF) protection
    • Cross-Origin Resource Sharing (CORS) filter
    • Secure Sockets Layer (SSL) providing HTTPS redirection
    • Authentication can be achieved using server filters to protect entire api family or ValidateToken sub for single API endpoint
    • Hashing methods e.g MD5, SHA1, SHA256, HMACSHA256
  • Redesign Architecture
    • The core handlers (ApiHandler and Web Handler) act like BaseController or Routes
    • ApiHandler routes the RequestURI to controllers e.g /web/api/v2/posts
    • WebHandler routes the RequestURI for front-end page e.g /web/login
    • HelpHandler 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 API 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 as object (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
    • MiniORM, an Object Relational Mapper to generate database queries without writing SQL commands
    • Queries map is still supported for SQLite and MySQL database queries
    • Default endpoint name is based on controller's name e.g /web/api/v2/post for PostController
    • Overide endpoint name using #Plural e.g /web/api/v2/posts
    • Custom version name e.g v2, live, demo, dev, staging using #Version
    • Description is set using #Desc, no more using #Desc1, #Desc2 or Literals that was very confusing in version 1.x
    • Use a Model map to pass data to html template e.g passing user's name variable to Dashboard in AccountController
    • API endpoint can be hidden using #Hide
  • Build-in Web Client
    • New blog front-end (using Bootstrap card layout)
    • Based on Bootstrap, jQuery, FontAwesome icons and Responsive layout suitable for different devices screen size
    • User account registration, activation through email link, forgot password, change password, login, logout
    • Integration with AdminLTE3 dashboard template (simplified)
    • SMTP email server

Code Example​

B4X:
Public Sub GetPost (pid As Int) As HttpResponseMessage
    #region Documentation
    ' #Version = v2
    ' #Desc = Get a post by id
    ' #Elements = [":pid"]
    #End region
    Dim con As SQL = Main.DB.GetConnection
    Dim strSQL As String
    Dim List1 As List
    List1.Initialize
    Try
        strSQL = Main.DB.Queries.Get("SELECT_POST_BY_ID")
        Dim res As ResultSet = con.ExecQuery2(strSQL, Array As String(pid))
        Do While res.NextRow
            Dim Map2 As Map
            Map2.Initialize
            For i = 0 To res.ColumnCount - 1
                If res.GetColumnName(i) = "id" Or _
                    res.GetColumnName(i) = "category_id" Or _
                    res.GetColumnName(i) = "post_status" Then
                    Map2.Put(res.GetColumnName(i), res.GetInt2(i))
                Else
                    Map2.Put(res.GetColumnName(i), res.GetString2(i))
                End If
            Next
            List1.Add(Map2)
        Loop
        If List1.Size > 0 Then
            HRM.ResponseCode = 200
            HRM.ResponseData = List1
        Else
            HRM.ResponseCode = 404
            HRM.ResponseError = "Post Not Found"
        End If
    Catch
        LogError(LastException)
        HRM.ResponseCode = 422
        HRM.ResponseError = "Error Execute Query"
    End Try
    Main.DB.CloseDB(con)
    Return HRM
End Sub

GitHub: https://github.com/pyhoon/webapi-2-b4j
Template: https://github.com/pyhoon/webapi-2-b4j/blob/main/Web API Server (2.00 beta 3).b4xtemplate
 
Last edited:

aeric

Expert
Licensed User
Longtime User
I don’t want to elaborate much what’s new in version 2 but I am sure there are a lot of significant improvements against version 1. Please try it out and start asking. 😊
 

aeric

Expert
Licensed User
Longtime User
I am making little progress in token authentication. Still figuring out how to properly use access token and refresh token in browser based Web app, mobile and desktop app.
 

aeric

Expert
Licensed User
Longtime User
This project has become more complex to handle non-api routes and multi version. I am scrapping beta 2 and rebuild as beta 3.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Web API Server (2.00 beta 3).b4xtemplate is released!

No More #Desc1 / #Desc2 ! No More Literals ! Use #Plural and #Elements ! Multi Versions !

Separate Controllers ! Less Handlers ! More Manageable Web / API routes ! Much Cleaner Codes !

JSON Web Tokens ! Basic Authentication ! Csrf Token ! Multi Clients !

New Blog Front-ends !

MiniORM examples ! Simple JSON response as List or Map !

It's Open Source !

1673800380644.png


1673800444727.png
 
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:
Top