HomeGuidesAPI ReferenceGraphQL
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

Batcher

This topic describes how to batch API calls using Batcher in Optimizely Connect Platform (OCP).

Batcher makes it easy to batch API calls that support arrays of records or call structures. See SDK Docs.

Sample Batcher

import { z } from '@zaius/node-sdk';
import {Transformer} from './Transformer';
import { logger, Batcher } from '@zaius/app-sdk';
import {Contact} from '../lib/Vendor';

export class ContactTransformer implements Transformer<Contact> {
  private customerBatch = new Batcher(z.customer);

  public async transform(contact: BrontoContact): Promise<boolean> {
    await this.customerBatch.append(this.transformContactToCustomer(contact));
    return false;
  }

  public async flush() {
    try {
      await this.customerBatch.flush();
    } catch (e) {
      logger.error('Error in sending to Zaius:', e);
    }
  }

  private async transformContactToCustomer(contact: Contact): CustomerPayload {
  return {
    identifiers: {
      email: contact.email,
      vendor_id: contact.id,
      phone: contact.mobileNumber
    },
    attributes: {
      // customer attributes to map
    }
  };
}

Next