Zig
Zig
Amazon SP-API Sellers Get Marketplace Participations
See more Amazon SP-API Examples
Demonstrates Amazon SP-API Sellers API -- get marketplace participations.Chilkat Zig Downloads
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();
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
// Make sure to use the correct domain.
// In this example, we are using "sandbox.sellingpartnerapi-eu.amazon.com"
rest.connect("sandbox.sellingpartnerapi-eu.amazon.com", port, b_tls, b_auto_reconnect) 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 {};
const sb_response = try chilkat.StringBuilder.init();
defer sb_response.deinit();
const uri = "/sellers/v1/marketplaceParticipations";
rest.fullRequestNoBodySb("GET", uri, 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:
// {
// "payload": [
// {
// "marketplace": {
// "id": "ATVPDKIKX0DER",
// "countryCode": "US",
// "name": "Amazon.com",
// "defaultCurrencyCode": "USD",
// "defaultLanguageCode": "en_US",
// "domainName": "www.amazon.com"
// },
// "participation": {
// "isParticipating": true,
// "hasSuspendedListings": false
// }
// }
// ]
// }
// 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 id: [:0]const u8 = "";
var country_code: [:0]const u8 = "";
var name: [:0]const u8 = "";
var default_currency_code: [:0]const u8 = "";
var default_language_code: [:0]const u8 = "";
var domain_name: [:0]const u8 = "";
var is_participating: bool = false;
var has_suspended_listings: bool = false;
var i: i32 = 0;
const count_i = json.sizeOfArray("payload");
while (i < count_i) {
json.setI(i);
id = try json.stringOf(alloc, "payload[i].marketplace.id");
country_code = try json.stringOf(alloc, "payload[i].marketplace.countryCode");
name = try json.stringOf(alloc, "payload[i].marketplace.name");
default_currency_code = try json.stringOf(alloc, "payload[i].marketplace.defaultCurrencyCode");
default_language_code = try json.stringOf(alloc, "payload[i].marketplace.defaultLanguageCode");
domain_name = try json.stringOf(alloc, "payload[i].marketplace.domainName");
is_participating = json.boolOf("payload[i].participation.isParticipating");
has_suspended_listings = json.boolOf("payload[i].participation.hasSuspendedListings");
i = i + 1;
}
std.debug.print("Success!\n", .{});
}