Updates:
29/10/2024
- Template:
- Web API Server (3.00beta4).b4xtemplate *updated
- Depends on:
- WebApiUtils.b4xlib (v3.01) *updated
- MiniORMUtils.b4xlib (v1.14)
- sqlite-jdbc-3.39.3.0.jar (or your preferred version)
- mysql-connector-java-8.0.30.jar (or your preferred version)
Introduction of :
1. ElementMatch (Pattern As String) As Boolean
2. CropElements (FullElements() As String, StartingElementIndex As Int) As String() -> (WebApiUtils.b4xlib)
With these new methods, I am trying to simplify the way writing APIs which I think make more sense to newbie of this Web API framework.
Example:
[CODE lang="b4x" highlight="6,9,13,18"]Sub Handle (req As ServletRequest, resp As ServletResponse)
Request = req
Response = resp
Method = Request.Method.ToUpperCase
Dim FullElements() As String = WebApiUtils.GetUriElements(Request.RequestURI)
Elements = WebApiUtils.CropElements(FullElements, 3) ' 3 For Api handler
Select Method
Case "GET"
If ElementMatch("") Then ' /api/categories
GetCategories
Return
End If
If ElementMatch("id") Then ' /api/categories/1
GetCategoryById(ElementId)
Return
End If
Case "POST"
If ElementMatch("") Then ' /api/categories
PostCategory
Return
End If
Case Else
Log("Unsupported method: " & Method)
ReturnMethodNotAllow
Return
End Select
ReturnBadRequest
End Sub[/CODE]
Instructions: Download the updated template and library files in 1st post.