Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

eBay -- Download Data using FileTransferService

See more eBay Examples

Demonstrates how to download a data file using the eBay File Transfer API.

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.

    // Use a previously obtained access token.  The token should look something like this:
    // "AgAAAA**AQA ..."
    const access_token = "EBAY_ACCESS_TOKEN";

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

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

    req.setHttpVerb("POST");
    req.setPath("/FileTransferService");
    req.setContentType("application/xml");

    // Build the XML body for the request.
    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("downloadFileRequest");
    xml.addAttribute("xmlns", "http://www.ebay.com/marketplace/services") catch {};
    xml.updateChildContent("taskReferenceId", "50013004806");
    xml.updateChildContent("fileReferenceId", "50015579016");

    req.loadBodyFromString(try xml.getXml(alloc), "utf-8") catch {};

    // The XML body looks like this:

    //     <?xml version="1.0" encoding="UTF-8"?>
    //     <downloadFileRequest xmlns="http://www.ebay.com/marketplace/services">
    //     <taskReferenceId>50013004806</taskReferenceId>
    //     <fileReferenceId>50015579016</fileReferenceId>
    //     </downloadFileRequest>

    req.addHeader("X-EBAY-SOA-OPERATION-NAME", "downloadFile");
    req.addHeader("X-EBAY-SOA-SECURITY-TOKEN", access_token);

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

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

    const response_body = try chilkat.BinData.init();
    defer response_body.deinit();
    resp.getBodyBd(response_body) catch {};

    // We can save the response body to a file for examination if we get an unanticipated response.
    // (It's binary, so it won't open well in a text editor.)
    response_body.writeFile("qa_output/response.mime") catch {};

    if (status_code != 200) {
        std.debug.print("Failed.\n", .{});
        return;
    }

    // The response body looks like this:

    //     --MIMEBoundaryurn_uuid_2B668636C1E17A4D4114925305818684241
    //     Content-Type: application/xop+xml; charset=utf-8; type="text/xml"
    //     Content-Transfer-Encoding: binary
    //     Content-ID: <0.urn:uuid:2B668636C1E17A4D4114925305818684242>
    //
    //     <?xml version='1.0' encoding='UTF-8'?>
    //     <downloadFileResponse xmlns="http://www.ebay.com/marketplace/services">
    //     <ack>Success</ack>
    //     <version>1.1.0</version>
    //     <timestamp>2017-04-18T15:49:41.868Z</timestamp>
    //     <fileAttachment>
    //         <Size>587</Size>
    //         <Data>
    //                 <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:urn:uuid:A37C3C73E994C267F11492530585522"/>
    //         </Data>
    //     </fileAttachment>
    //     </downloadFileResponse>
    //     --MIMEBoundaryurn_uuid_2B668636C1E17A4D4114925305818684241
    //     Content-Type: application/zip
    //     Content-Transfer-Encoding: binary
    //     Content-ID: <urn:uuid:A37C3C73E994C267F11492530585522>
    //
    //     <the binary bytes of the zip start here...>
    //

    // Load the binary response into a MIME object.
    const mime = try chilkat.Mime.init();
    defer mime.deinit();
    mime.loadMimeBd(response_body) catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    // Make sure we have 2 sub-parts.  The 1st sub-part is the XML response, the 2nd sub-part
    // is the zip containing the data.

    // Note: The 2nd sub-part can be a "zip" or "gzip".  These are two different file formats.
    // A zip is indicated with a Content-Type header equal to "application/zip",
    // whereas a gzip is indicated with a Content-Type header equal to "application/x-gzip"
    if (mime.getNumParts() != 2) {
        std.debug.print("Expected the MIME to have 2 parts.\n", .{});
        std.debug.print("NumParts = {d}\n", .{mime.getNumParts()});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Get the XML from the 1st MIME sub-part.

    const part0 = try chilkat.Mime.init();
    defer part0.deinit();
    mime.partAt(0, part0) catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    const download_response_xml = try part0.getBodyDecoded(alloc);
    const xml_resp = try chilkat.Xml.init();
    defer xml_resp.deinit();
    xml_resp.loadXml(download_response_xml) catch {};
    std.debug.print("Download Response XML:\n", .{});
    std.debug.print("{s}\n", .{try xml_resp.getXml(alloc)});

    std.debug.print("----\n", .{});

    // Now get the zip from the second part (index=1), unzip, and examine..

    const part1 = try chilkat.Mime.init();
    defer part1.deinit();
    mime.partAt(1, part1) catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    const zip_data = try chilkat.BinData.init();
    defer zip_data.deinit();
    part1.getBodyBd(zip_data) catch {};

    // Check to see if we have a zip or gzip.
    const sb_content_type = try chilkat.StringBuilder.init();
    defer sb_content_type.deinit();
    sb_content_type.append(try part1.getContentType(alloc)) catch {};

    const xml_from_zip = try chilkat.Xml.init();
    defer xml_from_zip.deinit();

    if (sb_content_type.contains("gzip", false)) {
        // This is a gzip compressed file.
        const gzip = try chilkat.Gzip.init();
        defer gzip.deinit();

        // in-place uncompress the data.
        // Note: The UncompressBd method was added in Chilkat v9.5.0.67
        gzip.uncompressBd(zip_data) catch {
            std.debug.print("{s}\n", .{try gzip.getLastErrorText(alloc)});
            return;
        };

        xml_from_zip.loadXml(try zip_data.getString(alloc, "utf-8")) catch {};
    } else {
        // This is a zip archive.

        // Load the body into a Zip object.
        const zip = try chilkat.Zip.init();
        defer zip.deinit();
        zip.openBd(zip_data) catch {
            std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
            return;
        };

        // Save the .zip to a file (so we can examine it for debugging if something is not as expected)
        zip_data.writeFile("qa_output/ebay_data.zip") catch {};

        // The zip should contain a single XML file.
        if (zip.getNumEntries() != 1) {
            std.debug.print("Expected the .zip to have 1 entry.\n", .{});
            std.debug.print("NumEntries = {d}\n", .{zip.getNumEntries()});
            std.debug.print("Failed.\n", .{});
            return;
        }

        const entry = try chilkat.ZipEntry.init();
        defer entry.deinit();
        zip.entryAt(0, entry) catch {
            std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
            return;
        };

        xml_from_zip.loadXml(try entry.unzipToString(alloc, 0, "utf-8")) catch {};
    }

    std.debug.print("XML contained in the zip:\n", .{});
    std.debug.print("{s}\n", .{try xml_from_zip.getXml(alloc)});
    std.debug.print("----\n", .{});

    std.debug.print("Success.\n", .{});
}