Wish B4J as a Powerful Web API Development Tool

Do you think this would be useful?

  • No. I don't need an API server.

    Votes: 0 0.0%

  • Total voters
    32

aeric

Expert
Licensed User
Longtime User
First, I want to thank Erel for making B4J in creating server app possible. I am able to create some API server apps (non-UI) using B4J with ease like here and here.
Recently, I need to develop a .NET Web API. While I am learning, I found there are some "Advanced" features that would be great if available for B4X (B4J specifically) to build a complete Web API. Many programming languages are popular for making Web API. If B4X becomes a good tool to create Web API, this could attract more developers to use B4X.

My wish is to have a framework so we can easily build Web API with following features:
1. Create a new project with a basic or boilerplate template to get started.
2. Auto-generate the API based on "Controller" (or Handler) class. Easy scaffolding a skeleton class.
3. Easily set the Accept and Content-Type to receive and output different Media Type e.g JSON or XML (Content Negotiation). Optional: At some point use jRDC.
4. Auto generate documentation using the comments above the RESTful API Verb sub (GET, POST, PUT, DELETE)
5. Able to integrate with swagger, so simple tests can be executed without using Postman or Curl
6. Connection Pool management or easily work with HikariCP or other alternatives. Limit maximum calls per second to prevent server being attack.
7. Security and Authorization (Basic, API Key, Token Based, OAuth, Facebook, Google), Token refresh.
8. Enable or disable Caching
9. Optional: MD5 or HMAC Authentication
10. Optional: Wizard to select a SQL database table as model
11. Optional: Performance monitoring
12: Optional: Compression and optimization
13. Optional: Unit test
14. Optional: API Versioning
15. Using SSL (It is already possible with SslConfiguration so it should be easy to include this feature)

I wish @alwaysbusy can continue his project :)

1628789185951.png
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Create a new project with a basic or boilerplate template to get started.
The server one, is actually good template to start with. it has everything thats needed.

Easily set the Accept and Content-Type to receive and output different Media Type e.g JSON or XML (Content Negotiation). Optional: At some point use jRDC.
You can read the content type from the handler class with:
req.ContentType

per second to prevent server being attack.
you actually use:
server.AddDoSFilter(
Yet, you should use a third party like cloudflare, you will never be able to hold on your own a ddos attack. And you wouldnt do it at database level.

4. Auto generate documentation using the comments above the RESTful API Verb sub (GET, POST, PUT, DELETE)
5. Able to integrate with swagger, so simple tests can be executed without using Postman or Curl
I guess so, The current B4x approach (what i feel) is to not get involved with third party apps and i do like that approach.

7. Security and Authorization (Basic, API Key, Token Based, OAuth, Facebook, Google), Token refresh.
These are all already accepted, may be some tutorials is what is lacking here.

Enable or disable Caching
I guess you can use server.CreateThreadSafeMap
but it may be better to wrap a library like:

14. Optional: API Versioning
this is something trivial, just write /v1/ in your handlers.

13. Optional: Unit test
we need a lot tests, but i believe they are the opposite of RAD.

Optional: Compression and optimization
server.GzipEnabled
 

Mashiane

Expert
Licensed User
Longtime User
There seems to be some stuff here that could be having what is needed here:

 

tchart

Well-Known Member
Licensed User
Longtime User
@aeric this is something that has been on my "to do" list for a long time.

Swagger is great but I dont think this would be easy to integrate from my investigations as it is very gearered toward .net projects.

I have also looked at Slate (https://github.com/slatedocs/slate) which has a very nice UI.

The main thing about all of these platforms (like you say) is that they require a very structured boilerplate/markup to work. If you look at Slate its just a bunch of Ruby scripts that generate HTML - end result is like this https://slatedocs.github.io/slate/#introduction

My approach is to use a post compile (or on demand) action to generate the documentation.

Here is an example generated from B4J code using Skeleton CSS - this is test example Im generating as I plan to use Slate for the CSS once the document generation is working well.

1629104104596.png


The problem here is that in order to generate the docs the handler has to be formatted in a very specific way. I have not been disciplined enough to apply this to all of my project yet but this is the approah I plan to take.

Here is an example handler for the above (the handler is called SomeMethod.bas)

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    'Methods: POST, GET, PUT, PATCH, DELETE
    Dim SupportedMethods As List = Array As String("POST","GET")  
    If SupportedMethods.IndexOf(req.Method) = -1 Then resp.SendError(405,"Method Not Allowed")  
   
    Dim ParameterMap As Map = req.ParameterMap
    If ParameterMap.Size = 0 Then resp.SendError(422,"No parameters supplied")
   
    ' Parameters
    Dim query As String = "1=1"
    Dim host As String
    Dim tags As List
    Dim path As String
   
    Dim RequiredParameters As List = Array As String("query","host")
   
    For Each param As String In RequiredParameters
        If ParameterMap.ContainsKey(param) = False Then resp.SendError(422,$"Parameter is required"$)
    Next
   
    If ParameterMap.ContainsKey("query") Then query = ParameterMap.Get("query")
    If ParameterMap.ContainsKey("host") Then host = ParameterMap.Get("host")
    If ParameterMap.ContainsKey("path") Then path = ParameterMap.Get("path")
    If ParameterMap.ContainsKey("tags") Then tags = ParameterMap.Get("tags")
   
    If query = "" Then resp.SendError(422,$"Parameter "query" cannot be empty"$)
   
    resp.ContentType = "application/json"
   
    Try
        ' Do something to create response
        Dim Response As String
        Response = $"{"Hello":"World"}"$
        resp.Write(Response)
    Catch
        Log(LastException)
        resp.SendError(500,$"Internal Server Error"$)
    End Try  
End Sub

The document generator goes through the project and pulls out the various bits of information to populate the HTML.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that anyone can create new project templates.

 

ilan

Expert
Licensed User
Longtime User
Note that anyone can create new project templates.


it would be nice if there would be an inside feature in b4j to search for templates and add them like in most IDE's where you can add extensions.

1629708706274.png
 

ilan

Expert
Licensed User
Longtime User
You mean something like a Package Manager?
If there is one for Additional Libraries then it maybe good.

something where we can browse from inside b4j (b4a/b4i) for templates people made or libraries. so have a short explanation and download them directly to your project.
like you can find in most IDE's like VSC, Atom,...
 

aeric

Expert
Licensed User
Longtime User
I have uploaded the template. Hope you like it.

 
Top