Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const uri = "/products";
    const api_version = "2.0";

    const http = try chilkat.Http.init();
    defer http.deinit();
    const req = try chilkat.HttpRequest.init();
    defer req.deinit();

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

    // Get the current date/time in timestamp format.
    const dt = try chilkat.DateTime.init();
    defer dt.deinit();
    dt.setFromCurrentSystemTime() catch {};
    const timestamp = try dt.getAsTimestamp(alloc, true);

    req.addHeader("TIME", timestamp);
    std.debug.print("timestamp = {s}\n", .{timestamp});

    const sb_xml = try chilkat.StringBuilder.init();
    defer sb_xml.deinit();
    try sb_xml.loadFile("qa_data/xml/effectconnect/effconCreate.xml", "utf-8");
    std.debug.print("length = {d}\n", .{sb_xml.getLength()});

    req.setHttpVerb("POST");
    req.setPath(uri);
    req.setContentType("multipart/form-data");
    req.addStringForUpload("payload", "effcon.xml", try sb_xml.getAsString(alloc), "utf-8") catch {
        std.debug.print("{s}\n", .{try req.getLastErrorText(alloc)});
        return;
    };

    // Build a string-to-sign and sign it using our effectconnect private key
    const sb_string_to_sign = try chilkat.StringBuilder.init();
    defer sb_string_to_sign.deinit();
    sb_string_to_sign.appendInt(sb_xml.getLength()) catch {};
    sb_string_to_sign.append("POST") catch {};
    sb_string_to_sign.append(uri) catch {};
    sb_string_to_sign.append(api_version) catch {};
    sb_string_to_sign.append(timestamp) catch {};

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    crypt.setMacAlgorithm("hmac");
    crypt.setHashAlgorithm("sha512");
    crypt.setEncodingMode("base64");
    // Use your effectconnect private key here:
    crypt.setMacKeyString("PRIVATE_KEY") catch {};
    req.addHeader("SIGNATURE", try crypt.macStringENC(alloc, try sb_string_to_sign.getAsString(alloc)));

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpSReq("submit.effectconnect.com", 443, true, req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("response status code = {d}\n", .{resp.getStatusCode()});

    // 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.
    const xml_resp = try chilkat.Xml.init();
    defer xml_resp.deinit();
    xml_resp.loadXml(try resp.getBodyStr(alloc)) catch {};

    std.debug.print("response body:\n", .{});
    std.debug.print("{s}\n", .{try xml_resp.getXml(alloc)});

    // 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...

    _ = try xml_resp.getChildContent(alloc, "Request|RequestType");
    _ = try xml_resp.getChildContent(alloc, "Request|RequestAction");
    _ = try xml_resp.getChildContent(alloc, "Request|RequestVersion");
    _ = try xml_resp.getChildContent(alloc, "Request|ProcessedAt");
    _ = try xml_resp.getChildContent(alloc, "Response|Result");
    _ = try xml_resp.getChildContent(alloc, "Response|ProductsCreateResponseContainer|ProcessID");
}