Sample code for 30+ languages & platforms
Dart

effectconnect Create or Replace Product Catalog

See more effectconnect Examples

Use this call to create or replace a product catalog in EffectConnect. This is always a purge and replace action for the entire catalog.

Chilkat Dart Downloads

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

void main() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final uri = '/products';
  final apiVersion = '2.0';

  final http = CkHttp();
  final req = CkHttpRequest();

  // Use your effectconnect public key here...
  req.addHeader('KEY', 'PUBLIC_KEY');
  req.addHeader('VERSION', apiVersion);
  req.addHeader('URI', uri);
  req.addHeader('RESPONSETYPE', 'XML');
  req.addHeader('RESPONSELANGUAGE', 'en');

  // Get the current date/time in timestamp format.
  final dt = CkDateTime();
  dt.setFromCurrentSystemTime();
  final timestamp = dt.getAsTimestamp(true);

  req.addHeader('TIME', timestamp);
  print('timestamp = $timestamp');

  final sbXml = CkStringBuilder();
  sbXml.loadFile('qa_data/xml/effectconnect/effconCreate.xml', 'utf-8');
  print('length = ${sbXml.length}');

  req.httpVerb = 'POST';
  req.path = uri;
  req.contentType = 'multipart/form-data';
  try {
    req.addStringForUpload('payload', 'effcon.xml', sbXml.getAsString(), 'utf-8');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Build a string-to-sign and sign it using our effectconnect private key
  final sbStringToSign = CkStringBuilder();
  sbStringToSign.appendInt(sbXml.length);
  sbStringToSign.append('POST');
  sbStringToSign.append(uri);
  sbStringToSign.append(apiVersion);
  sbStringToSign.append(timestamp);

  final crypt = CkCrypt2();
  crypt.macAlgorithm = 'hmac';
  crypt.hashAlgorithm = 'sha512';
  crypt.encodingMode = 'base64';
  // Use your effectconnect private key here:
  crypt.setMacKeyString('PRIVATE_KEY');
  req.addHeader('SIGNATURE', crypt.macStringENC(sbStringToSign.getAsString()));

  final resp = CkHttpResponse();
  try {
    http.httpSReq('submit.effectconnect.com', 443, true, req, resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('response status code = ${resp.statusCode}');

  // Examine the response.  The response status code can be 200 for both errors and success.
  // The success or error is based on the XML returned in the response body.
  final xmlResp = CkXml();
  xmlResp.loadXml(resp.bodyStr);

  print('response body:');
  print(xmlResp.getXml());

  // A sample response:

  // <?xml version="1.0" encoding="utf-8"?>
  // <ApiResponseContainer>
  //     <Request>
  //         <RequestType>Products</RequestType>
  //         <RequestAction>Create</RequestAction>
  //         <RequestVersion>2.0</RequestVersion>
  //         <RequestIdentifier/>
  //         <ProcessedAt>2019-04-18T15:28:55+02:00</ProcessedAt>
  //     </Request>
  //     <Response>
  //         <Result>Success</Result>
  //         <ProductsCreateResponseContainer>
  //             <ProcessID><![CDATA[J048hgS4OkNn0JnH]]></ProcessID>
  //         </ProductsCreateResponseContainer>
  //     </Response>
  // </ApiResponseContainer>

  // Parsing the response...
  final tagPath = '';

  final requestType = xmlResp.getChildContent('Request|RequestType');
  final requestAction = xmlResp.getChildContent('Request|RequestAction');
  final requestVersion = xmlResp.getChildContent('Request|RequestVersion');
  final processedAt = xmlResp.getChildContent('Request|ProcessedAt');
  final result = xmlResp.getChildContent('Response|Result');
  final processID = xmlResp.getChildContent('Response|ProductsCreateResponseContainer|ProcessID');
}