Share My Creation [Project Template] Web API Server

Version: 1.16
Description: Create Web API Server with CRUD functionalities


1681305482137.png
1681305567463.png
1681305621295.png


Template:
  • Web API Server (1.16).b4xtemplate
Depends on following libraries:
  • ByteConverter
  • JavaObject
  • jServer
  • Json
  • jSQL
Features:
  1. CRUD based - RESTful Web API (GET, POST, PUT, DELETE)
  2. Front-end (HTML, CSS, JS, Bootstrap)
  3. Support MySQL and SQLite database (Can be modified for MS SQL or SQL Express)
  4. Separate SQL queries file (queries-mysql.ini and queries-sqlite.ini)
  5. Sample database auto-generated for first run (Category and Product tables with dummy data)
  6. Versioning (using ROOT_PATH in config.ini, set as "/" if you don't want versioning)
  7. Auto generated documentation with API test.
How to use:
  1. Copy the "Web API Server (1.16).b4xtemplate" file into B4J Additional Libraries folder.
  2. Open B4J and create a new project with "Web API Server" template.
  3. Give your project any name you like, for e.g. WebAPI
  4. Run the project in Debug mode.
    You will see something like this in the Logs:
    B4X:
    Web API Server (version = 1.16) is running on port 19800
    Open the following URL from your web browser
    http://127.0.0.1:19800/v1/
  5. Copy the URL showed in Logs and open it using your web browser.
  6. To connect to MySQL server, go to Objects folder and open "config.ini".
  7. Comment the SQLite section and uncomment the MySQL section.
  8. Edit the root password at line #45 (second last line). Save the file.
    1681306109357.png
  9. In B4J project, comment the line
    '#AdditionalJar: sqlite-jdbc-3.39.3.0
    and uncomment the line
    #AdditionalJar: mysql-connector-java-8.0.30.
    Make sure you are using the correct version of connector.
    B4X:
    #Region AdditionalJar
    ' MySQL connector
    #AdditionalJar: mysql-connector-java-8.0.30
    ' SQLite connector
    '#AdditionalJar: sqlite-jdbc-3.39.3.0
    #End Region
  10. Follow step #3 above.
Client Template:
  • Web API Client (1.05).b4xtemplate
B4A
. .
B4i
. .
B4J


How to use:
  1. Copy the "Web API Client (1.05).b4xtemplate" file into B4X Additional Libraries folder.
  2. Open B4A (B4J or B4i) and create a new project with "Web API Client (1.05)" template.
  3. Edit the URL with your Web API Server host or IP
    B4X:
    Private URL As String = "http://192.168.50.42:17178/v1/" ' Change to your Web API Server URL
  4. Run in debug.
Video:

Comments: I try to make this template as compact as possible (remove user table and email function) but it is still big. If you don't like some features, just remove them. 😄

12/04/2023
- Web API Server - v1.16
- Similar to Web API Template 2.0, it is now less confusing by using #Desc and #Path for documentation
- Web API Client - v1.0.5 (compatible with Web API Server v1.16)

03/10/2022
- Web API Server - v1.15
- JS: Fixed bug point to wrong id for Category dropdown in Edit dialog
- JS: Remove comma in thousand for price textbox in Edit dialog
- Add a Home button in Category page

01/10/2022
- Web API Server - v1.14
- Manage Category in new page
- Dynamic populate Category dropdown in add/edit product dialog
- Improve JS scripts
- SQLite version point to sqlite-jdbc-3.39.3.0

27/08/2022
- Web API Client - v1.0.4 (compatible with Web API Server v1.13)

25/08/2022
- Web API Server - v1.13
- Home page with Add, Edit and Delete Product (using Bootstrap modal and jQuery Validate)
- Help or API documentation page improvements
- Many variables and SQL commands are renamed!
- SQLite version point to sqlite-jdbc-3.39.2.0
- Separate Category and Product handlers
- FindHandler added for more search endpoints
- Endpoints structure changed!
- Warning: This version is not compatible with Web API Client v1.0.3 and below

28/05/2022
- Web API - v1.12 (fixed a bug in HelpHandler.bas when reading commented line)
- Updated to use jServer v4.0
- Web API Client - v1.0.3 (Converted to B4XTemplate)

26/01/2022
- Create Publish.zip using #CustomBuildAction
- Create Publish.zip using Comment Link

19/10/2021 - v1.11
- Better use of Connection Pool for MySQL (do not close pool)
- Change DataUtils module to DataConnector class
- Clean up some redundant codes

08/10/2021
- WebAPI Client - v1.02
- - Some bugs fix and UI improvements

24/9/2021 - v1.10
- Auto generated documentation/help file based on handler classes (Click on the Question mark icon on top right corner to access the help file)

06/09/2021
- WebAPI Client - v1.01
- - Some bugs fix and UI improvements

04/09/2021 - v1.09
- Fixed PutProductByCategoryAndId to update Product as different Category.
- WebAPI Client (B4X) - v1.00

02/09/2021 - v1.08
- Added ConfigureCORS for ajax request.

01/09/2021 - v1.07
- Changes in Utility module (ReturnSuccess and ReturnSuccess2) require Status Code.
- Changes in ProductHandler (PostCategory and PostProductByCategory) return new record as result with Status Code 201.

27/08/2021 - v1.06
- Support for SQLite database

About auto generated help file (version 1.10+)
https://www.b4x.com/android/forum/threads/web-api-template.133764/post-851114
 

Attachments

  • ajax-test.zip
    30.2 KB · Views: 1,123
  • Web API Server (1.16).b4xtemplate
    481.3 KB · Views: 549
  • Web API Client (1.05).b4xtemplate
    443.9 KB · Views: 553
Last edited:

ilan

Expert
Licensed User
Longtime User
looks really nice (and complicated 😁)

i do get an error that a lib is missing:

1629969313888.png


i have some questions to the code but i will start with only 1,

i see that you are using some kind of template variables in your html files. i cannot find where you are updating them except of 'BuildView' function.

what function is responsible for updating the template vars with real data?

thank you
 

ilan

Expert
Licensed User
Longtime User
ok found it, its inside the config.ini file :)

those 2 functions:
B4X:
Public Sub BuildHtml (strHTML As String, Settings As Map) As String
    ' Replace $KEY$ tag with new content from Map
    strHTML = WebUtils.ReplaceMap(strHTML, Settings)
    Return strHTML
End Sub

Public Sub BuildView (strHTML As String, View As String) As String
    ' Replace @VIEW@ tag with new content
    strHTML = strHTML.Replace("@VIEW@", View)
    Return strHTML
End Sub

calls the function webutils:

B4X:
Public Sub ReplaceMap(Base As String, Replacements As Map) As String
    Dim pattern As StringBuilder
    pattern.Initialize
    For Each k As String In Replacements.Keys
        If pattern.Length > 0 Then pattern.Append("|")
        pattern.Append("\$").Append(k).Append("\$")
    Next
    Dim m As Matcher = Regex.Matcher(pattern.ToString, Base)
    Dim result As StringBuilder
    result.Initialize
    Dim lastIndex As Int
    Do While m.Find
        result.Append(Base.SubString2(lastIndex, m.GetStart(0)))
        Dim replace As String = Replacements.Get(m.Match.SubString2(1, m.Match.Length - 1))
        If m.Match.ToLowerCase.StartsWith("$h_") Then replace = EscapeHtml(replace)
        result.Append(replace)
        lastIndex = m.GetEnd(0)
    Loop
    If lastIndex < Base.Length Then result.Append(Base.SubString(lastIndex))
    Return result.ToString
End Sub
 

aeric

Expert
Licensed User
Longtime User
i do get an error that a lib is missing:

those 2 functions:
Yes. The BuildHtml and BuildView functions are responsible to render the HTML page. BuildView is used to merge the index.html with the main.html if you have other page other than index.html where main.html is the parent page.
Note that WebUtils file is provided by Erel.

As you can see, I am using config.ini and queries.ini and the app loads these 2 files when started.
I have separated these files so it is easy to customize your own values without touching the code.
 

ilan

Expert
Licensed User
Longtime User
i cannot test it because i need to run a server with MySQL for that. i will update the code to allow SQLite DB for testing.
i do it a lot in my project where i have conditional symbols for SQLite and MySQL to be able to test it anywhere. :)
 

aeric

Expert
Licensed User
Longtime User
i cannot test it because i need to run a server with MySQL for that. i will update the code to allow SQLite DB for testing.
i do it a lot in my project where i have conditional symbols for SQLite and MySQL to be able to test it anywhere. :)
I believe it is easy to achieve. :)

Edit: I can't imagine using SQLite for the Web API server backend. I know it is possible but I like MySQL and Adminer in Laragon.
 

ilan

Expert
Licensed User
Longtime User
I believe it is easy to achieve. :)

yes, should be.
it would be a great addition to b4j to have a place where people could upload their templates so others can download them. if not inside b4x ide it could be an external app or website.
 

aeric

Expert
Licensed User
Longtime User

aeric

Expert
Licensed User
Longtime User
There is an issue in MySQL (queries-mysql.ini) in version 1.06
The search queries should be same as SQLite script. The MySQL script was for version 1.05 and it is now fixed.
The issue is at FORMAT(P.product_price, 2) where the currency format is now handled by B4X.
B4X:
Else If res.GetColumnName(i) = "ee" Then
    Map2.Put(res.GetColumnName(i), NumberFormat2(res.GetDouble2(i), 1, 2, 2, True))
# SEARCH
SEARCH_PRODUCT_BY_CATEGORY_CODE_AND_NAME_ONEWORD_ORDERED=SELECT P.id AS aa, \
P.product_code AS bb, C.`category_name` AS cc, P.product_name AS dd,
FORMAT(P.product_price, 2) AS ee \
FROM `tbl_products` P JOIN `tbl_category` C ON P.`category_id` = C.`id` \
WHERE C.`category_name` LIKE ? OR P.`product_code` LIKE ? OR P.`product_name` LIKE ?

SEARCH_PRODUCT_BY_CATEGORY_CODE_AND_NAME_TWOWORDS_ORDERED=SELECT P.id AS aa, \
P.product_code AS bb, C.`category_name` AS cc, P.product_name AS dd,
FORMAT(P.product_price, 2) AS ee \
FROM `tbl_products` P JOIN `tbl_category` C ON P.`category_id` = C.`id` \
WHERE C.`category_name` LIKE ? OR P.`product_code` LIKE ? OR P.`product_name` LIKE ? \
OR C.`category_name` LIKE ? OR P.`product_code` LIKE ? OR P.`product_name` LIKE ?
 

swamisantosh

Member
Licensed User
Longtime User
http://127.0.0.1:19800/v1/category/1
{
"r": [],
"s": "error",
"e": "Error Execute Query",
"m": null
}

d:\pyhoon\WebApi\Objects
i removed webapi.db file the above issues got resolved.
using the debuging, what was issue, i found that, if the sqlite database it does not recreate.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
http://127.0.0.1:19800/v1/category/1
{
"r": [],
"s": "error",
"e": "Error Execute Query",
"m": null
}

d:\pyhoon\WebApi\Objects
i removed webapi.db file the above issues got resolved.
using the debuging, what was issue, i found that, if the sqlite database it does not recreate.
I am not sure how deleting the sqlite file would solve the issue. The error was handled by try-catch and you can find the exact problem (LastException) by reading the logs while debugging. As I stated above, I have done a test to send POST request using the payload with invalid comma sign which caused the RequestData read invalid data. Maybe I should have validated the Map initialized from the RequestData. In you case, I am not sure how you produce the error. Since you already able to resolved the issue then it is good and I assume everything is good to go.

Until we found a new bug, I hope the current version is a good template to start your own project. I encourage anyone to modify this template and come out an improved version. For myself, I would add back the user token, API key and SMTP features for my own use. I am not sharing it because a template should be minimalist and serve as a starting point for own customization.

Thanks everyone for the likes and testing this template. 🙏🏻

Don’t worry, I will keep answering your questions if you have one about this template. 😆
 
Top