Sample code for 30+ languages & platforms
DataFlex 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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCurl
    Handle hoState
    Handle hoFiles
    String sStatePath
    Variant vPage
    Handle hoPage
    Handle hoSeenPath
    String sEventId
    String sNextPosition
    Integer iBatch
    Integer i
    Integer iCount
    Boolean iMore
    Handle hoAuthorizedCommand1
    Integer iHeaderReplacement1
    String sTemp1
    String sTemp2
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Move False To iSuccess

    //  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.
    Get Create (RefClass(cComChilkatHttpCurl)) To hoCurl
    If (Not(IsComObjectCreated(hoCurl))) Begin
        Send CreateComObject of hoCurl
    End
    Send ComSetVar To hoCurl "access_token" "BOX_ACCESS_TOKEN"

    Get Create (RefClass(cComChilkatJsonObject)) To hoState
    If (Not(IsComObjectCreated(hoState))) Begin
        Send CreateComObject of hoState
    End
    Get Create (RefClass(cComCkFileAccess)) To hoFiles
    If (Not(IsComObjectCreated(hoFiles))) Begin
        Send CreateComObject of hoFiles
    End
    Move "qa_data/box_events_state.json" To sStatePath
    Get ComFileExists Of hoFiles sStatePath To bTemp1
    If (bTemp1 = True) Begin
        Get ComLoadFile Of hoState sStatePath To iSuccess
        If (iSuccess <> True) Begin
            Get ComLastErrorText Of hoState To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComHasMember Of hoState "position" To bTemp1
        If (bTemp1 <> True) Begin
            Showln "Invalid checkpoint: position missing."
            Procedure_Return
        End

    End
    Else Begin
        //  "now" starts a new subscription. It deliberately skips historical events.
        Get ComUpdateString Of hoState "position" "now" To iSuccess
    End

    Get ComStringOf Of hoState "position" To sTemp1
    Send ComSetVar To hoCurl "position" sTemp1
    Get Create (RefClass(cComChilkatJsonObject)) To hoPage
    If (Not(IsComObjectCreated(hoPage))) Begin
        Send CreateComObject of hoPage
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSeenPath
    If (Not(IsComObjectCreated(hoSeenPath))) Begin
        Send CreateComObject of hoSeenPath
    End

    Move 0 To iBatch

    Move True To iMore
    While ((iMore = True) And (iBatch < 20))
        Send ComClearTargetOutput To hoCurl "*"
        Get Create (RefClass(cComChilkatStringBuilder)) To hoAuthorizedCommand1
        If (Not(IsComObjectCreated(hoAuthorizedCommand1))) Begin
            Send CreateComObject of hoAuthorizedCommand1
        End
        Get ComSetString Of hoAuthorizedCommand1 'curl -X GET "https://api.box.com/2.0/events?stream_type=all&stream_position={{position}}&limit=100" -H "Authorization: Bearer {{access_token}}"' To iSuccess
        Get ComGetVar Of hoCurl "access_token" To sTemp1
        Get ComReplace Of hoAuthorizedCommand1 "{{access_token}}" sTemp1 To iHeaderReplacement1
        Get ComGetAsString Of hoAuthorizedCommand1 To sTemp1
        Get ComDoYourThing Of hoCurl sTemp1 To iSuccess
        Get ComStatusCode Of hoCurl To iTemp1
        If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
            Get ComStatusCode Of hoCurl To iTemp1
            Showln "Box request failed. HTTP status: " iTemp1
            Get ComFailReason Of hoCurl To iTemp1
            Showln "Failure reason: " iTemp1
            Get ComResponseBodyStr Of hoCurl To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get pvComObject of hoPage to vPage
        Get ComGetResponseJson Of hoCurl vPage To iSuccess
        If (iSuccess <> True) Begin
            Get ComLastErrorText Of hoCurl To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComSizeOfArray Of hoPage "entries" To iCount
        Move 0 To i
        While (i < iCount)
            Set ComI Of hoPage To i
            Get ComStringOf Of hoPage "entries[i].event_id" To sEventId
            If (sEventId = "") Begin
                Showln "Event without an event_id; checkpoint not advanced."
                Procedure_Return
            End

            Send ComClear To hoSeenPath
            Get ComAppend Of hoSeenPath "seen." To iSuccess
            Get ComAppend Of hoSeenPath sEventId To iSuccess
            Get ComHasMember Of hoState (ComGetAsString(hoSeenPath)) To bTemp1
            If (bTemp1 = False) Begin
                //  Replace this logging with your idempotent business operation.
                //  Record success only after that operation succeeds.
                Get ComStringOf Of hoPage "entries[i].event_type" To sTemp1
                Get ComStringOf Of hoPage "entries[i].source.id" To sTemp2
                Showln sEventId " " sTemp1 " " sTemp2
                Get ComGetAsString Of hoSeenPath To sTemp1
                Get ComUpdateBool Of hoState sTemp1 True To iSuccess
            End

            Move (i + 1) To i
        Loop

        Get ComStringOf Of hoPage "next_stream_position" To sNextPosition
        If (sNextPosition = "") Begin
            Showln "Missing next_stream_position; checkpoint not advanced."
            Procedure_Return
        End

        Get ComGetVar Of hoCurl "position" To sTemp1
        Move ((iCount > 0) And (sNextPosition <> sTemp1)) To iMore
        Get ComUpdateString Of hoState "position" sNextPosition To iSuccess
        Get ComWriteFile Of hoState sStatePath To iSuccess
        If (iSuccess <> True) Begin
            Get ComLastErrorText Of hoState To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Send ComSetVar To hoCurl "position" sNextPosition
        Move (iBatch + 1) To iBatch
    Loop

    Showln "Checkpoint saved. Run again later to continue polling."


End_Procedure