Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.7.0+

Box: Reuse HttpCurl and Invalidate Cached IDs

See more Box Examples

Run the reusable setup once per HttpCurl object, then select names and execute the operation. The setup is repeated here only to make this published example independently runnable. Perform three operations with one configured object: cold file lookup, warm download without lookups, then another download to a different local path after clearing only file state. Functions and output rules are registered only once; their variables are evaluated when used.

See the Box API guide for setup and the complete workflow.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

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

  success = false;

  // BEGIN ONE-TIME SETUP
  // This block is repeated so each published example can run on its own.
  // In an application, run the setup example once and pass the SAME curl object
  // to operation code below. Do not recreate it for every operation.
  // See Reusable Box HttpCurl Setup.
  final curl = CkHttpCurl();
  curl.setVar('access_token', 'BOX_ACCESS_TOKEN');
  curl.setVar('parent_name', '/');
  // Box's fixed root ID is an implementation detail; callers supply names.
  curl.setVar('parent_id', '0');
  final setupCommand = CkStringBuilder();
  var setupReplacements = 0;

  // Register findParent; registration sends no request.
  setupCommand.setString('curl -X GET "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"');
  setupReplacements = setupCommand.replace('ACCESS_TOKEN', curl.getVar('access_token'));
  try {
    curl.addFunction('findParent', setupCommand.getAsString());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Register findFolder; registration sends no request.
  setupCommand.setString('curl -X GET "https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"');
  setupReplacements = setupCommand.replace('ACCESS_TOKEN', curl.getVar('access_token'));
  try {
    curl.addFunction('findFolder', setupCommand.getAsString());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Register findFile; registration sends no request.
  setupCommand.setString('curl -X GET "https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"');
  setupReplacements = setupCommand.replace('ACCESS_TOKEN', curl.getVar('access_token'));
  try {
    curl.addFunction('findFile', setupCommand.getAsString());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Register findDestination; registration sends no request.
  setupCommand.setString('curl -X GET "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"');
  setupReplacements = setupCommand.replace('ACCESS_TOKEN', curl.getVar('access_token'));
  try {
    curl.addFunction('findDestination', setupCommand.getAsString());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Register getFileDetails; registration sends no request.
  setupCommand.setString('curl -X GET "https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version" -H "Authorization: Bearer ACCESS_TOKEN"');
  setupReplacements = setupCommand.replace('ACCESS_TOKEN', curl.getVar('access_token'));
  try {
    curl.addFunction('getFileDetails', setupCommand.getAsString());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Register getEnterprise; registration sends no request.
  setupCommand.setString('curl -X GET "https://api.box.com/2.0/users/me?fields=enterprise" -H "Authorization: Bearer ACCESS_TOKEN"');
  setupReplacements = setupCommand.replace('ACCESS_TOKEN', curl.getVar('access_token'));
  try {
    curl.addFunction('getEnterprise', setupCommand.getAsString());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Register output rules once. Chilkat 11.7.0 evaluates {{name}} variables
  // when extracting each output, using the current value at that time.
  try {
    curl.addOutput2('findParent', 'entries', 'name', '{{parent_name}}', true, 'id', 'parent_id');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    curl.addOutput2('findFolder', 'entries', 'name', '{{folder_name}}', true, 'id', 'folder_id');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    curl.addOutput2('findFile', 'entries', 'name', '{{file_name}}', true, 'id', 'file_id');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    curl.addOutput2('findDestination', 'entries', 'name', '{{destination_name}}', true, 'id', 'destination_id');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    curl.addOutput('getFileDetails', 'file_version.id', 'file_version_id');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    curl.addOutput('getFileDetails', 'sha1', 'file_sha1');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    curl.addOutput('getEnterprise', 'enterprise.id', 'enterprise_id');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // END ONE-TIME SETUP
  // The name lookups below assume each listing fits in one page (1000 items).
  // Parent and destination names select root children; folder_name selects a child
  // of parent_name. "/" selects the root parent. These are not arbitrary paths.

  // BEGIN OPERATION -- keep the configured object when reusing it.
  // Inputs are names. Retain cached IDs when the selected names have not changed.
  final parentName = '/';
  final folderName = 'images';
  final fileName = 'penguins.jpg';
  if ((curl.getVar('parent_name') != parentName) || ((parentName == '/') && (!curl.varDefined('parent_id')))) {
    curl.setVar('parent_name', parentName);
    curl.clearVar('parent_id');
    curl.clearVar('folder_id');
    curl.clearVar('file_id');
    curl.clearVar('file_version_id');
    curl.clearVar('file_sha1');
    if (parentName == '/') {
      curl.setVar('parent_id', '0');
    }
  }

  if (curl.getVar('folder_name') != folderName) {
    curl.setVar('folder_name', folderName);
    curl.clearVar('folder_id');
    curl.clearVar('file_id');
    curl.clearVar('file_version_id');
    curl.clearVar('file_sha1');
  }

  if (curl.getVar('file_name') != fileName) {
    curl.setVar('file_name', fileName);
    curl.clearVar('file_id');
    curl.clearVar('file_version_id');
    curl.clearVar('file_sha1');
  }

  curl.setVar('local_path', 'qa_output/penguins.jpg');
  // Operation 1: get information by name. This resolves folder_id and file_id.
  curl.clearTargetOutput('*');
  final authorizedCommand1 = CkStringBuilder();
  authorizedCommand1.setString('curl -X GET "https://api.box.com/2.0/files/{{file_id}}?fields=id,name" -H "Authorization: Bearer {{access_token}}"');
  final headerReplacement1 = authorizedCommand1.replace('{{access_token}}', curl.getVar('access_token'));
  success = true;
  try {
    curl.doYourThing(authorizedCommand1.getAsString());
  } on ChilkatException {
    success = false;
  }
  if ((!success) || (curl.statusCode != 200)) {
    print('Box request failed. HTTP status: ${curl.statusCode}');
    print('Failure reason: ${curl.failReason}');
    print(curl.responseBodyStr);
    return;
  }

  // Operation 2: keep the same object and IDs. Only the final download is needed.
  final warmPlan = CkJsonObject();
  warmPlan.emitCompact = false;
  final authorizedCommand2 = CkStringBuilder();
  authorizedCommand2.setString('curl -X GET "https://api.box.com/2.0/files/{{file_id}}/content" -H "Authorization: Bearer {{access_token}}" -L -o "{{local_path}}"');
  final headerReplacement2 = authorizedCommand2.replace('{{access_token}}', curl.getVar('access_token'));
  try {
    curl.examinePlan(authorizedCommand2.getAsString(), warmPlan);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print(warmPlan.emit());
  curl.clearTargetOutput('*');
  final authorizedCommand3 = CkStringBuilder();
  authorizedCommand3.setString('curl -X GET "https://api.box.com/2.0/files/{{file_id}}/content" -H "Authorization: Bearer {{access_token}}" -L -o "{{local_path}}"');
  final headerReplacement3 = authorizedCommand3.replace('{{access_token}}', curl.getVar('access_token'));
  success = true;
  try {
    curl.doYourThing(authorizedCommand3.getAsString());
  } on ChilkatException {
    success = false;
  }
  if ((!success) || (curl.statusCode != 200)) {
    print('Box request failed. HTTP status: ${curl.statusCode}');
    print('Failure reason: ${curl.failReason}');
    print(curl.responseBodyStr);
    return;
  }

  // Operation 3: explicitly invalidate file state, retaining the known folder.
  // Here we select the same name so this demonstration needs only one real file.
  // Substitute another filename to switch files in this folder.
  // The existing output rule reads the current file_name; do not register it again.
  curl.clearVar('file_id');
  curl.clearVar('file_version_id');
  curl.clearVar('file_sha1');
  curl.setVar('file_name', 'penguins.jpg');
  curl.setVar('local_path', 'qa_output/penguins-copy.jpg');
  final authorizedCommand4 = CkStringBuilder();
  authorizedCommand4.setString('curl -X GET "https://api.box.com/2.0/files/{{file_id}}/content" -H "Authorization: Bearer {{access_token}}" -L -o "{{local_path}}"');
  final headerReplacement4 = authorizedCommand4.replace('{{access_token}}', curl.getVar('access_token'));
  try {
    curl.examinePlan(authorizedCommand4.getAsString(), warmPlan);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print(warmPlan.emit());
  curl.clearTargetOutput('*');
  final authorizedCommand5 = CkStringBuilder();
  authorizedCommand5.setString('curl -X GET "https://api.box.com/2.0/files/{{file_id}}/content" -H "Authorization: Bearer {{access_token}}" -L -o "{{local_path}}"');
  final headerReplacement5 = authorizedCommand5.replace('{{access_token}}', curl.getVar('access_token'));
  success = true;
  try {
    curl.doYourThing(authorizedCommand5.getAsString());
  } on ChilkatException {
    success = false;
  }
  if ((!success) || (curl.statusCode != 200)) {
    print('Box request failed. HTTP status: ${curl.statusCode}');
    print('Failure reason: ${curl.failReason}');
    print(curl.responseBodyStr);
    return;
  }

  // Changing parent_name invalidates parent_id, folder_id, and file state.
  // Changing folder_name invalidates folder_id and file state.
  // Changing destination_name invalidates destination_id.
}