@@ -3,7 +3,17 @@ Attribute VB_Name = "WebHelpers"
33' WebHelpers v4.0.3
44' (c) Tim Hall - https://github.com/VBA-tools/VBA-Web
55'
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)
717'
818' Errors:
919' 11000 - Error during parsing
@@ -213,11 +223,10 @@ End Type
213223
214224Private web_pDocumentHelper As Object
215225Private web_pElHelper As Object
216- Private web_pAsyncRequests As Dictionary
217226Private web_pConverters As Dictionary
218227
219228' --------------------------------------------- '
220- ' Types
229+ ' Types and Properties
221230' --------------------------------------------- '
222231
223232''
@@ -308,7 +317,26 @@ End Enum
308317'
309318' @example
310319' ```VB.net
320+ ' Dim Client As New WebClient
321+ ' Client.BaseUrl = "https://api.example.com/v1/"
311322'
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 ...
312340' ```
313341'
314342' @property EnableLogging
@@ -317,6 +345,14 @@ End Enum
317345''
318346Public EnableLogging As Boolean
319347
348+ ''
349+ ' Store currently running async requests
350+ '
351+ ' @property AsyncRequests
352+ ' @type Dictionary
353+ ''
354+ Public AsyncRequests As Dictionary
355+
320356' ============================================= '
321357' 1. Logging
322358' ============================================= '
@@ -489,15 +525,15 @@ End Function
489525'
490526' @method ParseJson
491527' @param {String} Json JSON value to parse
492- ' @return {Object }
528+ ' @return {Dictionary|Collection }
493529'
494530' (Implemented in VBA-JSON embedded below)
495531
496532'
497533' Convert `Dictionary`, `Collection`, or `Array` to JSON string.
498534'
499535' @method ConvertToJson
500- ' @param {Dictionary|Collection|Variant } Obj
536+ ' @param {Dictionary|Collection|Array } Obj
501537' @return {String}
502538'
503539' (Implemented in VBA-JSON embedded below)
@@ -1220,7 +1256,7 @@ End Function
12201256'
12211257' @method FindInKeyValues
12221258' @param {Collection} KeyValues
1223- ' @param {String } Key to find
1259+ ' @param {Variant } Key to find
12241260' @return {Variant}
12251261''
12261262Public Function FindInKeyValues (KeyValues As Collection , Key As Variant ) As Variant
@@ -1259,7 +1295,7 @@ End Function
12591295'
12601296' @method AddOrReplaceInKeyValues
12611297' @param {Collection} KeyValues
1262- ' @param {String } Key
1298+ ' @param {Variant } Key
12631299' @param {Variant} Value
12641300' @return {Variant}
12651301''
@@ -1348,84 +1384,10 @@ Public Function MethodToName(Method As WebMethod) As String
13481384 End Select
13491385End Function
13501386
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-
13921387' ============================================= '
13931388' 6. Timing
13941389' ============================================= '
13951390
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-
14291391''
14301392' Handle timeout timers expiring
14311393'
@@ -1434,14 +1396,12 @@ End Sub
14341396' @param {String} RequestId
14351397''
14361398Public 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
14451405 End If
14461406End Sub
14471407
0 commit comments