Sample code for 30+ languages & platforms
Swift

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

Swift

func chilkatTest() {
    var success: Bool = false

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

    var uri: String? = "/products"
    var apiVersion: String? = "2.0"

    let http = CkoHttp()!
    let req = CkoHttpRequest()!

    // Use your effectconnect public key here...
    req.addHeader(name: "KEY", value: "PUBLIC_KEY")
    req.addHeader(name: "VERSION", value: apiVersion)
    req.addHeader(name: "URI", value: uri)
    req.addHeader(name: "RESPONSETYPE", value: "XML")
    req.addHeader(name: "RESPONSELANGUAGE", value: "en")

    // Get the current date/time in timestamp format.
    let dt = CkoDateTime()!
    dt.setFromCurrentSystemTime()
    var timestamp: String? = dt.get(asTimestamp: true)

    req.addHeader(name: "TIME", value: timestamp)
    print("timestamp = \(timestamp!)")

    let sbXml = CkoStringBuilder()!
    success = sbXml.loadFile(path: "qa_data/xml/effectconnect/effconCreate.xml", charset: "utf-8")
    print("length = \(sbXml.length.intValue)")

    req.httpVerb = "POST"
    req.path = uri
    req.contentType = "multipart/form-data"
    success = req.addString(forUpload: "payload", filename: "effcon.xml", strData: sbXml.getAsString(), charset: "utf-8")
    if success == false {
        print("\(req.lastErrorText!)")
        return
    }

    // Build a string-to-sign and sign it using our effectconnect private key
    let sbStringToSign = CkoStringBuilder()!
    sbStringToSign.appendInt(value: sbXml.length.intValue)
    sbStringToSign.append(value: "POST")
    sbStringToSign.append(value: uri)
    sbStringToSign.append(value: apiVersion)
    sbStringToSign.append(value: timestamp)

    let crypt = CkoCrypt2()!
    crypt.macAlgorithm = "hmac"
    crypt.hashAlgorithm = "sha512"
    crypt.encodingMode = "base64"
    // Use your effectconnect private key here:
    crypt.setMacKeyString(key: "PRIVATE_KEY")
    req.addHeader(name: "SIGNATURE", value: crypt.macStringENC(inText: sbStringToSign.getAsString()))

    let resp = CkoHttpResponse()!
    success = http.httpSReq(domain: "submit.effectconnect.com", port: 443, ssl: true, request: req, response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("response status code = \(resp.statusCode.intValue)")

    // 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.
    let xmlResp = CkoXml()!
    xmlResp.load(xmlData: 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...
    var tagPath: String?
    var RequestType: String?
    var RequestAction: String?
    var RequestVersion: String?
    var ProcessedAt: String?
    var Result: String?
    var ProcessID: String?

    RequestType = xmlResp.getChildContent(tagPath: "Request|RequestType")
    RequestAction = xmlResp.getChildContent(tagPath: "Request|RequestAction")
    RequestVersion = xmlResp.getChildContent(tagPath: "Request|RequestVersion")
    ProcessedAt = xmlResp.getChildContent(tagPath: "Request|ProcessedAt")
    Result = xmlResp.getChildContent(tagPath: "Response|Result")
    ProcessID = xmlResp.getChildContent(tagPath: "Response|ProductsCreateResponseContainer|ProcessID")

}