Sample code for 30+ languages & platforms
Zig

Amazon SP-API Get Feeds

See more Amazon SP-API Examples

Returns feed details for the feeds that match the filters that you specify.

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 requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const auth_aws = try chilkat.AuthAws.init();
    defer auth_aws.deinit();
    auth_aws.setAccessKey("AWS_ACCESS_KEY");
    auth_aws.setSecretKey("AWS_SECRET_KEY");
    auth_aws.setServiceName("execute-api");
    // Use the region that is correct for your needs.
    auth_aws.setRegion("eu-west-1");

    const rest = try chilkat.Rest.init();
    defer rest.deinit();
    rest.connect("sandbox.sellingpartnerapi-eu.amazon.com", 443, true, true) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    try rest.setAuthAws(auth_aws);

    // Load the previously obtained LWA access token.
    // See Fetch SP-API LWA Access Token
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/sp_api_lwa_token.json") catch {
        std.debug.print("Failed to load LWA access token.\n", .{});
        return;
    };

    // Add the x-amz-access-token request header.
    const lwa_token = try json_token.stringOf(alloc, "access_token");

    rest.clearAllHeaders() catch {};
    rest.addHeader("x-amz-access-token", lwa_token) catch {};

    rest.clearAllQueryParams() catch {};
    rest.addQueryParam("feedTypes", "POST_PRODUCT_DATA") catch {};
    rest.addQueryParam("pageSize", "10") catch {};
    rest.addQueryParam("processingStatuses", "CANCELLED,DONE") catch {};

    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    const path = "/feeds/2021-06-30/feeds";
    rest.fullRequestNoBodySb("GET", path, sb_response) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Examine the response status.
    const status_code = rest.getResponseStatusCode();
    if (status_code != 200) {
        std.debug.print("Response status text: {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("Response body: \n", .{});
        std.debug.print("{s}\n", .{try sb_response.getAsString(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    std.debug.print("{s}\n", .{try sb_response.getAsString(alloc)});

    // If successful, gets a JSON response such as the following:

    // {
    //   "feeds": [
    //     {
    //       "feedId": "FeedId1",
    //       "feedType": "POST_PRODUCT_DATA",
    //       "createdTime": "2019-12-11T13:16:24.630Z",
    //       "processingStatus": "CANCELLED",
    //       "processingStartTime": "2019-12-11T13:16:24.630Z",
    //       "processingEndTime": "2019-12-11T13:16:24.630Z"
    //     }
    //   ],
    //   "nextToken": "VGhpcyB0b2tlbiBpcyBvcGFxdWUgYW5kIGludGVudGlvbmFsbHkgb2JmdXNjYXRlZA=="
    // }

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    const json = try chilkat.JsonObject.init();
    defer json.deinit();

    json.loadSb(sb_response) catch {};

    var feed_id: [:0]const u8 = "";
    var feed_type: [:0]const u8 = "";
    var created_time: [:0]const u8 = "";
    var processing_status: [:0]const u8 = "";
    var processing_start_time: [:0]const u8 = "";
    var processing_end_time: [:0]const u8 = "";

    _ = try json.stringOf(alloc, "nextToken");
    var i: i32 = 0;
    const count_i = json.sizeOfArray("feeds");
    while (i < count_i) {
        json.setI(i);
        feed_id = try json.stringOf(alloc, "feeds[i].feedId");
        feed_type = try json.stringOf(alloc, "feeds[i].feedType");
        created_time = try json.stringOf(alloc, "feeds[i].createdTime");
        processing_status = try json.stringOf(alloc, "feeds[i].processingStatus");
        processing_start_time = try json.stringOf(alloc, "feeds[i].processingStartTime");
        processing_end_time = try json.stringOf(alloc, "feeds[i].processingEndTime");
        i = i + 1;
    }

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