batchUpsertCatalog method

Future<CatalogResponse> batchUpsertCatalog({
  1. required BatchUpsertCatalogRequest request,
  2. String? authToken,
})

Creates or updates up to 10,000 target objects based on the provided list of objects.

The target objects are grouped into batches and each batch is inserted/updated in an all-or-nothing manner. If an object within a batch is malformed in some way, or violates a database constraint, the entire batch containing that item will be disregarded. However, other batches in the same request may still succeed. Each batch may contain up to 1,000 objects, and batches will be processed in order as long as the total object count for the request (items, variations, modifier lists, discounts, and taxes) is no more than 10,000.

Implementation

Future<CatalogResponse> batchUpsertCatalog({
  required BatchUpsertCatalogRequest request,
  String? authToken,
}) async {

  authToken ??= authenticationService.getCachedToken()?.accessToken;

  Map<String, String> headers = {
    "Authorization": "Bearer ${authToken ?? ""}",
    'Content-Type': 'application/json; charset=UTF-8',
    'Accept': 'application/json',

  };

  Uri endpoint = Uri.https(
      baseUrl, "/v2/catalog/batch-upsert");

  //print (endpoint.toString());

  var response = await
  http.post(endpoint, body: jsonEncode(request),  headers: headers);

  if (response.statusCode == 200) {
    print (jsonDecode(response.body));
    return CatalogResponse.fromJson(jsonDecode(response.body));
  }
  else {
    print (response.body);
    throw CatalogException(statusCode: response.statusCode, message: CatalogResponse.fromJson(jsonDecode(response.body)).errors?[0].detail?.toString());
  }
}