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

Unicode C++
#include <CkHttpCurlW.h>
#include <CkJsonObjectW.h>
#include <CkFileAccessW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = 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.
    CkHttpCurlW curl;
    curl.SetVar(L"access_token",L"BOX_ACCESS_TOKEN");

    CkJsonObjectW state;
    CkFileAccessW files;
    const wchar_t *statePath = L"qa_data/box_events_state.json";
    if (files.FileExists(statePath) == true) {
        success = state.LoadFile(statePath);
        if (success != true) {
            wprintf(L"%s\n",state.lastErrorText());
            return;
        }

        if (state.HasMember(L"position") != true) {
            wprintf(L"Invalid checkpoint: position missing.\n");
            return;
        }

    }
    else {
        //  "now" starts a new subscription. It deliberately skips historical events.
        state.UpdateString(L"position",L"now");
    }

    curl.SetVar(L"position",state.stringOf(L"position"));
    CkJsonObjectW page;
    CkStringBuilderW seenPath;
    const wchar_t *eventId = 0;
    const wchar_t *nextPosition = 0;
    int batch = 0;
    int i;
    int count;
    bool more = true;
    while ((more == true) && (batch < 20)) {
        curl.ClearTargetOutput(L"*");
        CkStringBuilderW authorizedCommand1;
        authorizedCommand1.SetString(L"curl -X GET \"https://api.box.com/2.0/events?stream_type=all&stream_position={{position}}&limit=100\" -H \"Authorization: Bearer {{access_token}}\"");
        int headerReplacement1 = authorizedCommand1.Replace(L"{{access_token}}",curl.getVar(L"access_token"));
        success = curl.DoYourThing(authorizedCommand1.getAsString());
        if ((success != true) || (curl.get_StatusCode() != 200)) {
            wprintf(L"Box request failed. HTTP status: %d\n",curl.get_StatusCode());
            wprintf(L"Failure reason: %d\n",curl.get_FailReason());
            wprintf(L"%s\n",curl.responseBodyStr());
            return;
        }

        success = curl.GetResponseJson(page);
        if (success != true) {
            wprintf(L"%s\n",curl.lastErrorText());
            return;
        }

        count = page.SizeOfArray(L"entries");
        i = 0;
        while (i < count) {
            page.put_I(i);
            eventId = page.stringOf(L"entries[i].event_id");
            if (eventId == L"") {
                wprintf(L"Event without an event_id; checkpoint not advanced.\n");
                return;
            }

            seenPath.Clear();
            seenPath.Append(L"seen.");
            seenPath.Append(eventId);
            if (state.HasMember(seenPath.getAsString()) == false) {
                //  Replace this logging with your idempotent business operation.
                //  Record success only after that operation succeeds.
                wprintf(L"%s %s %s\n",eventId,page.stringOf(L"entries[i].event_type"),page.stringOf(L"entries[i].source.id"));
                state.UpdateBool(seenPath.getAsString(),true);
            }

            i = i + 1;
        }

        nextPosition = page.stringOf(L"next_stream_position");
        if (nextPosition == L"") {
            wprintf(L"Missing next_stream_position; checkpoint not advanced.\n");
            return;
        }

        more = (count > 0) && (nextPosition != curl.getVar(L"position"));
        state.UpdateString(L"position",nextPosition);
        success = state.WriteFile(statePath);
        if (success != true) {
            wprintf(L"%s\n",state.lastErrorText());
            return;
        }

        curl.SetVar(L"position",nextPosition);
        batch = batch + 1;
    }

    wprintf(L"Checkpoint saved. Run again later to continue polling.\n");
    }