@@ -3,7 +3,17 @@ Attribute VB_Name = "WebHelpers"
3
3
' WebHelpers v4.0.3
4
4
' (c) Tim Hall - https://github.com/VBA-tools/VBA-Web
5
5
'
6
- ' Common helpers VBA-Web
6
+ ' Contains general-purpose helpers that are used throughout VBA-Web. Includes:
7
+ '
8
+ ' - Logging
9
+ ' - Converters and encoding
10
+ ' - Url handling
11
+ ' - Object/Dictionary/Collection/Array helpers
12
+ ' - Request preparation / handling
13
+ ' - Timing
14
+ ' - Mac
15
+ ' - Cryptography
16
+ ' - Converters (JSON, XML, Url-Encoded)
7
17
'
8
18
' Errors:
9
19
' 11000 - Error during parsing
@@ -213,11 +223,10 @@ End Type
213
223
214
224
Private web_pDocumentHelper As Object
215
225
Private web_pElHelper As Object
216
- Private web_pAsyncRequests As Dictionary
217
226
Private web_pConverters As Dictionary
218
227
219
228
' --------------------------------------------- '
220
- ' Types
229
+ ' Types and Properties
221
230
' --------------------------------------------- '
222
231
223
232
''
@@ -308,7 +317,26 @@ End Enum
308
317
'
309
318
' @example
310
319
' ```VB.net
320
+ ' Dim Client As New WebClient
321
+ ' Client.BaseUrl = "https://api.example.com/v1/"
311
322
'
323
+ ' Dim RequestWithTypo As New WebRequest
324
+ ' RequestWithTypo.Resource = "peeple/{id}"
325
+ ' RequestWithType.AddUrlSegment "idd", 123
326
+ '
327
+ ' ' Enable logging before the request is executed
328
+ ' WebHelpers.EnableLogging = True
329
+ '
330
+ ' Dim Response As WebResponse
331
+ ' Set Response = Client.Execute(Request)
332
+ '
333
+ ' ' Immediate window:
334
+ ' ' --> Request - (Time)
335
+ ' ' GET https://api.example.com/v1/peeple/{id}
336
+ ' ' Headers...
337
+ ' '
338
+ ' ' <-- Response - (Time)
339
+ ' ' 404 ...
312
340
' ```
313
341
'
314
342
' @property EnableLogging
@@ -317,6 +345,14 @@ End Enum
317
345
''
318
346
Public EnableLogging As Boolean
319
347
348
+ ''
349
+ ' Store currently running async requests
350
+ '
351
+ ' @property AsyncRequests
352
+ ' @type Dictionary
353
+ ''
354
+ Public AsyncRequests As Dictionary
355
+
320
356
' ============================================= '
321
357
' 1. Logging
322
358
' ============================================= '
@@ -489,15 +525,15 @@ End Function
489
525
'
490
526
' @method ParseJson
491
527
' @param {String} Json JSON value to parse
492
- ' @return {Object }
528
+ ' @return {Dictionary|Collection }
493
529
'
494
530
' (Implemented in VBA-JSON embedded below)
495
531
496
532
'
497
533
' Convert `Dictionary`, `Collection`, or `Array` to JSON string.
498
534
'
499
535
' @method ConvertToJson
500
- ' @param {Dictionary|Collection|Variant } Obj
536
+ ' @param {Dictionary|Collection|Array } Obj
501
537
' @return {String}
502
538
'
503
539
' (Implemented in VBA-JSON embedded below)
@@ -1220,7 +1256,7 @@ End Function
1220
1256
'
1221
1257
' @method FindInKeyValues
1222
1258
' @param {Collection} KeyValues
1223
- ' @param {String } Key to find
1259
+ ' @param {Variant } Key to find
1224
1260
' @return {Variant}
1225
1261
''
1226
1262
Public Function FindInKeyValues (KeyValues As Collection , Key As Variant ) As Variant
@@ -1259,7 +1295,7 @@ End Function
1259
1295
'
1260
1296
' @method AddOrReplaceInKeyValues
1261
1297
' @param {Collection} KeyValues
1262
- ' @param {String } Key
1298
+ ' @param {Variant } Key
1263
1299
' @param {Variant} Value
1264
1300
' @return {Variant}
1265
1301
''
@@ -1348,84 +1384,10 @@ Public Function MethodToName(Method As WebMethod) As String
1348
1384
End Select
1349
1385
End Function
1350
1386
1351
- ''
1352
- ' Add request to watched requests
1353
- '
1354
- ' @internal
1355
- ' @method AddAsyncRequest
1356
- ' @param {RestAsyncWrapper} AsyncWrapper
1357
- ''
1358
- Public Sub AddAsyncRequest (web_AsyncWrapper As Object )
1359
- If web_pAsyncRequests Is Nothing Then : Set web_pAsyncRequests = New Dictionary
1360
- If Not web_AsyncWrapper.Request Is Nothing Then
1361
- web_pAsyncRequests.Add web_AsyncWrapper.Request.Id, web_AsyncWrapper
1362
- End If
1363
- End Sub
1364
-
1365
- ''
1366
- ' Get watched request
1367
- '
1368
- ' @internal
1369
- ' @method GetAsyncRequest
1370
- ' @param {String} RequestId
1371
- ' @return {RestAsyncWrapper}
1372
- ''
1373
- Public Function GetAsyncRequest (web_RequestId As String ) As Object
1374
- If web_pAsyncRequests.Exists(web_RequestId) Then
1375
- Set GetAsyncRequest = web_pAsyncRequests(web_RequestId)
1376
- End If
1377
- End Function
1378
-
1379
- ''
1380
- ' Remove request from watched requests
1381
- '
1382
- ' @internal
1383
- ' @method RemoveAsyncRequest
1384
- ' @param {String} RequestId
1385
- ''
1386
- Public Sub RemoveAsyncRequest (web_RequestId As String )
1387
- If Not web_pAsyncRequests Is Nothing Then
1388
- If web_pAsyncRequests.Exists(web_RequestId) Then : web_pAsyncRequests.Remove web_RequestId
1389
- End If
1390
- End Sub
1391
-
1392
1387
' ============================================= '
1393
1388
' 6. Timing
1394
1389
' ============================================= '
1395
1390
1396
- ''
1397
- ' Start timeout timer for request
1398
- '
1399
- ' @internal
1400
- ' @method StartTimeoutTimer
1401
- ' @param {RestAsyncWrapper} AsyncWrapper
1402
- ' @param {Long} TimeoutMS
1403
- ''
1404
- Public Sub StartTimeoutTimer (web_AsyncWrapper As Object , web_TimeoutMs As Long )
1405
- ' Round ms to seconds with minimum of 1 second if ms > 0
1406
- Dim web_TimeoutS As Long
1407
- web_TimeoutS = Round(web_TimeoutMs / 1000 , 0 )
1408
- If web_TimeoutMs > 0 And web_TimeoutS = 0 Then
1409
- web_TimeoutS = 1
1410
- End If
1411
-
1412
- AddAsyncRequest web_AsyncWrapper
1413
- Application.OnTime Now + TimeValue("00:00:" & web_TimeoutS), "'WebHelpers.OnTimeoutTimerExpired """ & web_AsyncWrapper.Request.Id & """'"
1414
- End Sub
1415
-
1416
- ''
1417
- ' Stop timeout timer for request
1418
- '
1419
- ' @internal
1420
- ' @method StopTimeoutTimer
1421
- ' @param {RestAsyncWrapper} AsyncWrapper
1422
- ''
1423
- Public Sub StopTimeoutTimer (web_AsyncWrapper As Object )
1424
- If Not web_AsyncWrapper.Request Is Nothing Then
1425
- RemoveAsyncRequest web_AsyncWrapper.Request.Id
1426
- End If
1427
- End Sub
1428
-
1429
1391
''
1430
1392
' Handle timeout timers expiring
1431
1393
'
@@ -1434,14 +1396,12 @@ End Sub
1434
1396
' @param {String} RequestId
1435
1397
''
1436
1398
Public Sub OnTimeoutTimerExpired (web_RequestId As String )
1437
- Dim web_AsyncWrapper As Object
1438
- Set web_AsyncWrapper = GetAsyncRequest(web_RequestId)
1439
-
1440
- If Not web_AsyncWrapper Is Nothing Then
1441
- StopTimeoutTimer web_AsyncWrapper
1442
-
1443
- LogDebug "Async Timeout: " & web_AsyncWrapper.Request.FormattedResource, "WebHelpers.OnTimeoutTimerExpired"
1444
- web_AsyncWrapper.TimedOut
1399
+ If Not AsyncRequests Is Nothing Then
1400
+ If AsyncRequests.Exists(web_RequestId) Then
1401
+ Dim web_AsyncWrapper As Object
1402
+ Set web_AsyncWrapper = AsyncRequests(web_RequestId)
1403
+ web_AsyncWrapper.TimedOut
1404
+ End If
1445
1405
End If
1446
1406
End Sub
1447
1407
0 commit comments