Delphi DLL
Delphi DLL
Amazon SP-API Create Feed Doc
See more Amazon SP-API Examples
Creates a feed document. Amazon returns a feedDocumentId value, encryption details, and a URL for uploading the feed contents.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, AuthAws, Rest, JsonObject, StringBuilder;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
authAws: HCkAuthAws;
rest: HCkRest;
jsonToken: HCkJsonObject;
lwa_token: PWideChar;
jsonReq: HCkJsonObject;
sbRequest: HCkStringBuilder;
sbResponse: HCkStringBuilder;
path: PWideChar;
statusCode: Integer;
json: HCkJsonObject;
feedDocumentId: PWideChar;
url: PWideChar;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
authAws := CkAuthAws_Create();
CkAuthAws_putAccessKey(authAws,'AWS_ACCESS_KEY');
CkAuthAws_putSecretKey(authAws,'AWS_SECRET_KEY');
CkAuthAws_putServiceName(authAws,'execute-api');
// Use the region that is correct for your needs.
CkAuthAws_putRegion(authAws,'eu-west-1');
rest := CkRest_Create();
success := CkRest_Connect(rest,'sellingpartnerapi-eu.amazon.com',443,True,True);
if (success = False) then
begin
Memo1.Lines.Add(CkRest__lastErrorText(rest));
Exit;
end;
success := CkRest_SetAuthAws(rest,authAws);
// Load the previously obtained LWA access token.
// See Fetch SP-API LWA Access Token
jsonToken := CkJsonObject_Create();
success := CkJsonObject_LoadFile(jsonToken,'qa_data/tokens/sp_api_lwa_token.json');
if (success = False) then
begin
Memo1.Lines.Add('Failed to load LWA access token.');
Exit;
end;
// Add the x-amz-access-token request header.
lwa_token := CkJsonObject__stringOf(jsonToken,'access_token');
jsonReq := CkJsonObject_Create();
CkJsonObject_UpdateString(jsonReq,'contentType','text/tab-separated-values; charset=UTF-8');
sbRequest := CkStringBuilder_Create();
CkJsonObject_EmitSb(jsonReq,sbRequest);
CkRest_ClearAllQueryParams(rest);
CkRest_ClearAllHeaders(rest);
CkRest_AddHeader(rest,'x-amz-access-token',lwa_token);
sbResponse := CkStringBuilder_Create();
path := '/feeds/2021-06-30/documents';
success := CkRest_FullRequestSb(rest,'POST',path,sbRequest,sbResponse);
if (success = False) then
begin
Memo1.Lines.Add(CkRest__lastErrorText(rest));
Exit;
end;
// Examine the response status.
statusCode := CkRest_getResponseStatusCode(rest);
Memo1.Lines.Add('statusCode: ' + IntToStr(statusCode));
if (statusCode <> 201) then
begin
Memo1.Lines.Add('Response status text: ' + CkRest__responseStatusText(rest));
Memo1.Lines.Add('Response body: ');
Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponse));
Memo1.Lines.Add('Failed.');
Exit;
end;
Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponse));
// If successful, gets a JSON response such as the following:
// {
// "feedDocumentId": "3d4e42b5-1d6e-44e8-a89c-2abfca0625bb",
// "url": "https://d34o8swod1owfl.cloudfront.net/Feed_101__POST_PRODUCT_DATA_.xml"
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
json := CkJsonObject_Create();
CkJsonObject_LoadSb(json,sbResponse);
feedDocumentId := CkJsonObject__stringOf(json,'feedDocumentId');
url := CkJsonObject__stringOf(json,'url');
// Save the JSON to a file for the example that uploads the feed..
success := CkJsonObject_WriteFile(json,'qa_data/json/sp_api_feed_upload_info.json');
Memo1.Lines.Add('Success!');
CkAuthAws_Dispose(authAws);
CkRest_Dispose(rest);
CkJsonObject_Dispose(jsonToken);
CkJsonObject_Dispose(jsonReq);
CkStringBuilder_Dispose(sbRequest);
CkStringBuilder_Dispose(sbResponse);
CkJsonObject_Dispose(json);
end;