-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiControllerClass.bas
126 lines (116 loc) · 3 KB
/
ApiControllerClass.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
B4J=true
Group=Controllers
ModulesStructureVersion=1
Type=Class
Version=10
@EndOfDesignText@
' Api Controller
' Version 1.07
Sub Class_Globals
Private Request As ServletRequest
Private Response As ServletResponse
Private HRM As HttpResponseMessage
Private DB As MiniORM
Private Method As String
Private Version As String
Private RequestURI As String 'ignore
Private Elements() As String
Private ElementLastIndex As Int
Private ApiVersionIndex As Int
Private ControllerIndex As Int
Private ControllerElement As String 'ignore
Private FirstIndex As Int
Private FirstElement As String
Private SecondIndex As Int 'ignore
Private SecondElement As String 'ignore
End Sub
Public Sub Initialize (req As ServletRequest, resp As ServletResponse)
Request = req
Response = resp
HRM.Initialize
DB.Initialize(Main.DBOpen, Main.DBEngine)
End Sub
Private Sub ReturnApiResponse 'ignore
HRM.SimpleResponse = Main.SimpleResponse
WebApiUtils.ReturnHttpResponse(HRM, Response)
End Sub
Private Sub ReturnBadRequest
WebApiUtils.ReturnBadRequest(Response)
End Sub
Private Sub ReturnMethodNotAllow
WebApiUtils.ReturnMethodNotAllow(Response)
End Sub
Private Sub ReturnErrorUnprocessableEntity
WebApiUtils.ReturnErrorUnprocessableEntity(Response)
End Sub
Public Sub RouteApi
Method = Request.Method.ToUpperCase
RequestURI = Request.RequestURI
Elements = WebApiUtils.GetUriElements(RequestURI)
ApiVersionIndex = Main.Element.ApiVersionIndex
ControllerIndex = Main.Element.ApiControllerIndex
ControllerElement = Elements(ControllerIndex)
Version = Elements(ApiVersionIndex)
ElementLastIndex = Elements.Length - 1
If ElementLastIndex > ControllerIndex Then
FirstIndex = ControllerIndex + 1
FirstElement = Elements(FirstIndex)
End If
Select Method
Case "GET"
RouteGet
Case "POST"
'RoutePost
Case "PUT"
'RoutePut
Case "DELETE"
'RouteDelete
Case Else
Log("Unsupported method: " & Method)
ReturnMethodNotAllow
End Select
End Sub
Public Sub RouteWeb
Method = Request.Method.ToUpperCase
RequestURI = Request.RequestURI
Elements = WebApiUtils.GetUriElements(RequestURI)
ElementLastIndex = Elements.Length - 1
ControllerIndex = Main.Element.WebControllerIndex
ControllerElement = Elements(ControllerIndex)
If ElementLastIndex > ControllerIndex Then
FirstIndex = ControllerIndex + 1
FirstElement = Elements(FirstIndex)
End If
Select Method
Case "GET"
Select ElementLastIndex
Case ControllerIndex
'ShowPage
Return
End Select
Case Else
Log("Unsupported method: " & Method)
ReturnMethodNotAllow
End Select
End Sub
' GET
Private Sub RouteGet
Select Version
Case "v2"
Select ElementLastIndex
Case ControllerIndex
' Snippet: Code_MiniORMUtils_01 Get Resources as List
'GetPlural
Return
Case FirstIndex
If IsNumber(FirstElement) = False Then
ReturnErrorUnprocessableEntity
Return
End If
' Snippet: Code_MiniORMUtils_02 Get Resource as Map by Id
'GetSingular(FirstElement)
Return
End Select
End Select
ReturnBadRequest
End Sub