AutoIt Requires Chilkat v11.5.0+
AutoIt
Box: Poll User Events and Save a Checkpoint
See more Box Examples
Poll the authenticated user event stream in bounded batches. The local JSON checkpoint keeps processed event IDs and the next stream position. This single-process demonstration logs events; a production worker should commit its business action, deduplication record, and cursor transactionally and prune old IDs.
See the Box API guide for setup and the complete workflow.
Chilkat AutoIt Downloads
Local $bSuccess = False
; Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
; See Global Unlock Sample.
; Guide: Box API Guide.
$bSuccess = False
; Supply an unexpired token obtained with one of the Box authentication examples.
; A token represents a particular user or service account. IDs must be accessible to it.
$oCurl = ObjCreate("Chilkat.HttpCurl")
$oCurl.SetVar "access_token","BOX_ACCESS_TOKEN"
$oState = ObjCreate("Chilkat.JsonObject")
$oFiles = ObjCreate("Chilkat.FileAccess")
Local $statePath = "qa_data/box_events_state.json"
If ($oFiles.FileExists($statePath) = True) Then
$bSuccess = $oState.LoadFile($statePath)
If ($bSuccess <> True) Then
ConsoleWrite($oState.LastErrorText & @CRLF)
Exit
EndIf
If ($oState.HasMember("position") <> True) Then
ConsoleWrite("Invalid checkpoint: position missing." & @CRLF)
Exit
EndIf
Else
; "now" starts a new subscription. It deliberately skips historical events.
$oState.UpdateString("position","now")
EndIf
$oCurl.SetVar "position",$oState.StringOf("position")
$oPage = ObjCreate("Chilkat.JsonObject")
$oSeenPath = ObjCreate("Chilkat.StringBuilder")
Local $sEventId
Local $sNextPosition
Local $iBatch = 0
Local $i
Local $iCount
Local $bMore = True
While ($bMore = True) And ($iBatch < 20)
$oCurl.ClearTargetOutput "*"
$oAuthorizedCommand1 = ObjCreate("Chilkat.StringBuilder")
$oAuthorizedCommand1.SetString("curl -X GET ""https://api.box.com/2.0/events?stream_type=all&stream_position={{position}}&limit=100"" -H ""Authorization: Bearer {{access_token}}""")
Local $iHeaderReplacement1 = $oAuthorizedCommand1.Replace("{{access_token}}",$oCurl.GetVar("access_token"))
$bSuccess = $oCurl.DoYourThing($oAuthorizedCommand1.GetAsString())
If (($bSuccess <> True) Or ($oCurl.StatusCode <> 200)) Then
ConsoleWrite("Box request failed. HTTP status: " & $oCurl.StatusCode & @CRLF)
ConsoleWrite("Failure reason: " & $oCurl.FailReason & @CRLF)
ConsoleWrite($oCurl.ResponseBodyStr & @CRLF)
Exit
EndIf
$bSuccess = $oCurl.GetResponseJson($oPage)
If ($bSuccess <> True) Then
ConsoleWrite($oCurl.LastErrorText & @CRLF)
Exit
EndIf
$iCount = $oPage.SizeOfArray("entries")
$i = 0
While $i < $iCount
$oPage.I = $i
$sEventId = $oPage.StringOf("entries[i].event_id")
If ($sEventId = "") Then
ConsoleWrite("Event without an event_id; checkpoint not advanced." & @CRLF)
Exit
EndIf
$oSeenPath.Clear
$oSeenPath.Append("seen.")
$oSeenPath.Append($sEventId)
If ($oState.HasMember($oSeenPath.GetAsString()) = False) Then
; Replace this logging with your idempotent business operation.
; Record success only after that operation succeeds.
ConsoleWrite($sEventId & " " & $oPage.StringOf("entries[i].event_type") & " " & $oPage.StringOf("entries[i].source.id") & @CRLF)
$oState.UpdateBool($oSeenPath.GetAsString(),True)
EndIf
$i = $i + 1
Wend
$sNextPosition = $oPage.StringOf("next_stream_position")
If ($sNextPosition = "") Then
ConsoleWrite("Missing next_stream_position; checkpoint not advanced." & @CRLF)
Exit
EndIf
$bMore = ($iCount > 0) And ($sNextPosition <> $oCurl.GetVar("position"))
$oState.UpdateString("position",$sNextPosition)
$bSuccess = $oState.WriteFile($statePath)
If ($bSuccess <> True) Then
ConsoleWrite($oState.LastErrorText & @CRLF)
Exit
EndIf
$oCurl.SetVar "position",$sNextPosition
$iBatch = $iBatch + 1
Wend
ConsoleWrite("Checkpoint saved. Run again later to continue polling." & @CRLF)