-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJsonBase.bas
More file actions
270 lines (219 loc) · 7.57 KB
/
JsonBase.bas
File metadata and controls
270 lines (219 loc) · 7.57 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
Attribute VB_Name = "JsonBase"
' JsonBase v1.2.0
' (c) Gustav Brock, Cactus Data ApS, CPH
' https://github.com/CactusData/VBA.CVRAPI
'
' Supporting functions for retrieval of data from a Json service.
'
' License: MIT (http://opensource.org/licenses/mit-license.php)
'
Option Compare Text
Option Explicit
' Enum for HTTP methods.
Public Enum HttpVerb
hvDelete = 8 ' Requests that a specified URI be deleted.
hvGet = 1 ' Retrieves the information or entity that is identified by the URI of the request.
hvHead = 16 ' Retrieves the message headers for the information or entity that is identified by the URI of the request.
hvOptions = 64 ' Represents a request for information about the communication options available on the request/response chain identified by the Request-URI.
hvPatch = 32 ' Requests that a set of changes described in the request entity be applied to the resource identified by the Request- URI.
hvPost = 2 ' Posts a new entity as an addition to a URI.
hvPut = 4 ' Replaces an entity that is identified by a URI.
End Enum
Public Sub AppendContentKeyValue( _
ByRef Body As String, _
ByVal Key As String, _
ByVal Value As String, _
Optional ByVal UseNull As Boolean)
Const Delimiter As String = ","
Const Separator As String = ":"
Dim Pair As String
Dim Pairs() As String
If Key <> "" And (Value <> "" Or UseNull) Then
If Value <> "" Then
Pair = """" & Key & """" & Separator & """" & Value & """"
ElseIf UseNull Then
Pair = """" & Key & """" & Separator & "null"
End If
Pairs = Split(Body, Delimiter)
ReDim Preserve Pairs(UBound(Pairs) + 1)
Pairs(UBound(Pairs)) = Pair
Body = Join(Pairs, Delimiter)
End If
End Sub
Public Sub AppendSubPath( _
ByRef Url As String, _
ByVal SubPath As String)
Const SeparatorFirst As String = "?"
Const SeparatorPath As String = "/"
Dim UrlParts() As String
If SubPath <> "" Then
UrlParts = Split(Url, SeparatorFirst)
If Right(UrlParts(0), 1) <> SeparatorPath Then
SubPath = SeparatorPath & SubPath
End If
UrlParts(0) = UrlParts(0) & SubPath
' Return modified URL including the appended subpath.
Url = Join(UrlParts, SeparatorFirst)
End If
End Sub
' Break one-lined multiple elements into separate lines.
' For better read-out when debugging.
' Returns the modified Json by reference.
'
' Example input:
' {"taxCodes": [{"id": "bd1e66f3","code": "U-00"},{"id": "453c1e3b","code": "U-01"}]}
' Example output:
' {"taxCodes": [{"id": "bd1e66f3","code": "U-00"},
' {"id": "453c1e3b","code": "U-01"}]}
'
' 2022-05-24. Gustav Brock, Cactus Data ApS, CPH.
'
Public Sub BreakJson(ByRef Json As String)
Const BreakMark As String = "},{"
Const BreakLine As String = "}," & vbNewLine & "{"
Dim Text As String
' Break one-lined multiple elements into separate lines.
Text = Replace(Json, BreakMark, BreakLine)
' Return the sanitised json.
Json = Text
End Sub
' Build a URL string from its components to call a service.
' Parameter Query must be URL encoded.
'
' Returns: URL string.
'
Public Function BuildServiceUrl( _
Optional ByVal Scheme As String = "http", _
Optional ByVal Host As String = "localhost", _
Optional ByVal Path As String, _
Optional ByVal Query As String) _
As String
Dim ServiceUrl As String
' Verify scheme.
If Scheme = "" Then
Scheme = "http"
End If
' Append scheme separator.
If Right(Scheme, 3) <> "://" Then
Scheme = Scheme & "://"
End If
' Verify host.
If Host = "" Then
Host = "localhost"
End If
' Append a trailing slash.
If Right(Host, 1) <> "/" Then
Host = Host & "/"
End If
' Verify path.
If Path <> "" Then
' Remove a leading slash.
If Left(Path, 1) = "/" Then
Path = Mid(Path, 2)
End If
' Remove a trailing slash.
If Right(Path, 1) = "/" Then
Path = Mid(Path, 1, Len(Path) - 1)
End If
' Remove an empty path.
If Replace(Path, "/", "") = "" Then
Path = ""
End If
End If
' Verify query.
If Left(Query, 1) <> "?" Then
Query = ""
End If
ServiceUrl = Scheme & Host & Path & Query
BuildServiceUrl = ServiceUrl
End Function
' Build the query element of a URL string from a
' parameter array of key/value pairs.
'
' Returns: String of encoded query elements
'
Public Function BuildUrlQuery( _
ParamArray QueryElements() As Variant) _
As String
' Key/Value pairs of QueryElements must be URL encoded.
Const SeparatorFirst As String = "?"
Const SeparatorNext As String = "&"
Dim QueryString As String
If UBound(QueryElements) > -1 Then
QueryString = SeparatorFirst & Join(QueryElements, SeparatorNext)
End If
BuildUrlQuery = QueryString
End Function
' Build a URL encoded query element from its key/value pairs.
'
' Returns a key/value string: key=value.
'
Public Function BuildUrlQueryParameter( _
ByVal Key As String, _
ByVal Value As Variant) _
As String
Const Separator As String = "="
Dim QueryElement As String
Dim ValueString As String
' Trim and URL encode the key/value pair.
If Trim(Key) <> "" And IsEmpty(Value) = False Then
ValueString = Trim(CStr(Nz(Value)))
If ValueString = "" Then
ValueString = "''"
End If
QueryElement = Trim(Key) & Separator & ValueString
End If
BuildUrlQueryParameter = QueryElement
End Function
' Converts a name to general proper case leaving the company type abreviation intact.
'
' Note: Often company and city names are received in uppercase only.
'
' Example:
' CACTUS DATA APS -> Cactus Data ApS
' BERGEN -> Bergen
'
Public Function FormatCompany( _
ByVal Company As String) _
As String
Dim CompanyTypes() As Variant
Dim ProperCompany As String
Dim Index As Integer
CompanyTypes() = Array("AmbA", "A.m.b.A", "ApS", "AS", "A/S", "I/S", "IVS", "K/S", "P/S")
ProperCompany = Replace(StrConv(Replace(Company, "v/", "¤v/ "), vbProperCase), "¤v/ ", "v/")
For Index = LBound(CompanyTypes) To UBound(CompanyTypes)
If Left(ProperCompany, Len(CompanyTypes(Index)) + 1) = CompanyTypes(Index) & " " Then
Mid(ProperCompany, 1) = CompanyTypes(Index)
End If
If Right(ProperCompany, Len(CompanyTypes(Index)) + 1) = " " & CompanyTypes(Index) Then
Mid(ProperCompany, Len(ProperCompany) - Len(CompanyTypes(Index)) + 1) = CompanyTypes(Index)
End If
Next
FormatCompany = ProperCompany
End Function
' Create the literal uppercased HTTP method from a passed HTTP verb.
'
' 2022-04-28. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function HttpMethod( _
ByVal Method As HttpVerb) _
As String
Dim LiteralMethod As String
Select Case Method
Case hvDelete
LiteralMethod = "DELETE"
Case hvGet
LiteralMethod = "GET"
Case hvHead
LiteralMethod = "HEAD"
Case hvOptions
LiteralMethod = "OPTIONS"
Case hvPatch
LiteralMethod = "PATCH"
Case hvPost
LiteralMethod = "POST"
Case hvPut
LiteralMethod = "PUT"
End Select
HttpMethod = LiteralMethod
End Function