Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.5.0+

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 B4X Downloads

B4X
Dim success As Boolean = False

'  Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
'  See Global Unlock Sample.
'  Guide: Box API Guide.

success = 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.
Dim curl As ChilkatHttpCurl
curl.Initialize("curl")
curl.SetVar("access_token", "BOX_ACCESS_TOKEN")

Dim state As ChilkatJsonObject
state.Initialize
Dim files As ChilkatFileAccess
files.Initialize
Dim statePath As String = "qa_data/box_events_state.json"
If files.FileExists(statePath) = True Then
    success = state.LoadFile(statePath)
    If success <> True Then
        Log(state.LastErrorText)
        Return
    End If

    If state.HasMember("position") <> True Then
        Log("Invalid checkpoint: position missing.")
        Return
    End If

Else
    '  "now" starts a new subscription. It deliberately skips historical events.
    state.UpdateString("position", "now")
End If

curl.SetVar("position", state.StringOf("position"))
Dim page As ChilkatJsonObject
page.Initialize
Dim seenPath As ChilkatStringBuilder
seenPath.Initialize
Dim eventId As String
Dim nextPosition As String
Dim batch As Int = 0
Dim i As Int
Dim count As Int
Dim more As Boolean = True
Do While (more = True) And (batch < 20)
    curl.ClearTargetOutput("*")
    Dim authorizedCommand1 As ChilkatStringBuilder
    authorizedCommand1.Initialize
    authorizedCommand1.SetString("curl -X GET " & Chr(34) & "https://api.box.com/2.0/events?stream_type=all&stream_position={{position}}&limit=100" & Chr(34) & " -H " & Chr(34) & "Authorization: Bearer {{access_token}}" & Chr(34))
    Dim headerReplacement1 As Int = authorizedCommand1.Replace("{{access_token}}", curl.GetVar("access_token"))
    success = curl.DoYourThing(authorizedCommand1.GetAsString)
    If (success <> True) Or (curl.StatusCode <> 200) Then
        Log("Box request failed. HTTP status: " & curl.StatusCode)
        Log("Failure reason: " & curl.FailReason)
        Log(curl.ResponseBodyStr)
        Return
    End If

    success = curl.GetResponseJson(page)
    If success <> True Then
        Log(curl.LastErrorText)
        Return
    End If

    count = page.SizeOfArray("entries")
    i = 0
    Do While i < count
        page.I = i
        eventId = page.StringOf("entries[i].event_id")
        If eventId = "" Then
            Log("Event without an event_id; checkpoint not advanced.")
            Return
        End If

        seenPath.Clear
        seenPath.Append("seen.")
        seenPath.Append(eventId)
        If state.HasMember(seenPath.GetAsString) = False Then
            '  Replace this logging with your idempotent business operation.
            '  Record success only after that operation succeeds.
            Log(eventId & " " & page.StringOf("entries[i].event_type") & " " & page.StringOf("entries[i].source.id"))
            state.UpdateBool(seenPath.GetAsString, True)
        End If

        i = i + 1
    Loop
    nextPosition = page.StringOf("next_stream_position")
    If nextPosition = "" Then
        Log("Missing next_stream_position; checkpoint not advanced.")
        Return
    End If

    more = (count > 0) And (nextPosition <> curl.GetVar("position"))
    state.UpdateString("position", nextPosition)
    success = state.WriteFile(statePath)
    If success <> True Then
        Log(state.LastErrorText)
        Return
    End If

    curl.SetVar("position", nextPosition)
    batch = batch + 1
Loop
Log("Checkpoint saved. Run again later to continue polling.")