Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Walmart - Update Inventory for an ItemDemonstrates how to update inventory for an item.
#include <C_CkStringBuilderW.h> #include <C_CkAuthUtilW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpW.h> #include <C_CkXmlW.h> void ChilkatSample(void) { HCkStringBuilderW sbUrl; BOOL success; int numReplaced; const wchar_t *requestMethod; HCkAuthUtilW authUtil; const wchar_t *wmConsumerId; const wchar_t *wmPrivateKey; const wchar_t *jsonStr; HCkJsonObjectW json; HCkHttpW http; int originalAmount; HCkXmlW xmlBody; const wchar_t *xmlStr; HCkXmlW xml; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // Sends the following PUT request: // PUT https://marketplace.walmartapis.com/v2/inventory?sku={SKU} sbUrl = CkStringBuilderW_Create(); success = CkStringBuilderW_Append(sbUrl,L"https://marketplace.walmartapis.com/v2/inventory?sku={SKU}"); numReplaced = CkStringBuilderW_Replace(sbUrl,L"{SKU}",L"HOPP~904~51041"); requestMethod = L"PUT"; // First we need to generate a signature for our request. // The signature needs to be re-generated for each new Walmart HTTP request. authUtil = CkAuthUtilW_Create(); wmConsumerId = L"WALMART_CONSUMER_ID"; wmPrivateKey = L"WALMART_PRIVATE_KEY"; jsonStr = CkAuthUtilW_walmartSignature(authUtil,CkStringBuilderW_getAsString(sbUrl),wmConsumerId,wmPrivateKey,requestMethod); if (CkAuthUtilW_getLastMethodSuccess(authUtil) != TRUE) { wprintf(L"%s\n",CkAuthUtilW_lastErrorText(authUtil)); CkStringBuilderW_Dispose(sbUrl); CkAuthUtilW_Dispose(authUtil); return; } // The JSON returned by WalmartSignature contains the values to be used in the following // header fields: WM_SEC.AUTH_SIGNATURE, WM_SEC.TIMESTAMP, and WM_QOS.CORRELATION_ID json = CkJsonObjectW_Create(); CkJsonObjectW_Load(json,jsonStr); http = CkHttpW_Create(); CkHttpW_SetRequestHeader(http,L"WM_SVC.NAME",L"Walmart Marketplace"); CkHttpW_SetRequestHeader(http,L"WM_QOS.CORRELATION_ID",CkJsonObjectW_stringOf(json,L"correlation_id")); CkHttpW_SetRequestHeader(http,L"WM_SEC.TIMESTAMP",CkJsonObjectW_stringOf(json,L"timestamp")); CkHttpW_SetRequestHeader(http,L"WM_SEC.AUTH_SIGNATURE",CkJsonObjectW_stringOf(json,L"signature")); CkHttpW_SetRequestHeader(http,L"WM_CONSUMER.ID",wmConsumerId); CkHttpW_putAccept(http,L"application/xml"); // Note: Do not explicitly set the "Host" header. Chilkat will set it automatically. // The body of the PUT request will be XML that looks like this: // <?xml version="1.0" encoding="utf-8" ?> // <wm:inventory xmlns:wm="http://walmart.com/"> // <wm:sku>HOPP~904~51041</wm:sku> // <wm:quantity> // <wm:unit>EACH</wm:unit> // <wm:amount>235</wm:amount> // </wm:quantity> // <wm:fulfillmentLagTime>5</wm:fulfillmentLagTime> // </wm:inventory> // Build the XML request body: originalAmount = 234; xmlBody = CkXmlW_Create(); CkXmlW_putTag(xmlBody,L"wm:inventory"); CkXmlW_AddAttribute(xmlBody,L"xmlns:wm",L"http://walmart.com/"); CkXmlW_UpdateChildContent(xmlBody,L"wm:sku",L"HOPP~904~51041"); CkXmlW_UpdateChildContent(xmlBody,L"wm:quantity|wm:unit",L"EACH"); CkXmlW_UpdateChildContentInt(xmlBody,L"wm:quantity|wm:amount",originalAmount + 1); CkXmlW_UpdateChildContent(xmlBody,L"wm:fulfillmentLagTime",L"5"); wprintf(L"PUT request body:\n"); wprintf(L"%s\n",CkXmlW_getXml(xmlBody)); wprintf(L"--\n"); // This is a simple PUT that can be sent w/ the PutText method. xmlStr = CkHttpW_putText(http,CkStringBuilderW_getAsString(sbUrl),CkXmlW_getXml(xmlBody),L"utf-8",L"application/xml",FALSE,FALSE); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkStringBuilderW_Dispose(sbUrl); CkAuthUtilW_Dispose(authUtil); CkJsonObjectW_Dispose(json); CkHttpW_Dispose(http); CkXmlW_Dispose(xmlBody); return; } xml = CkXmlW_Create(); CkXmlW_LoadXml(xml,xmlStr); // A successful response should have a 200 response status if (CkHttpW_getLastStatus(http) != 200) { wprintf(L"%s\n",CkXmlW_getXml(xml)); wprintf(L"Response Status Code: %d\n",CkHttpW_getLastStatus(http)); wprintf(L"Failed.\n"); CkStringBuilderW_Dispose(sbUrl); CkAuthUtilW_Dispose(authUtil); CkJsonObjectW_Dispose(json); CkHttpW_Dispose(http); CkXmlW_Dispose(xmlBody); CkXmlW_Dispose(xml); return; } // A sample XML response is shown below.. wprintf(L"%s\n",CkXmlW_getXml(xml)); wprintf(L"--\n"); wprintf(L"Success!\n"); // --------------------------------------- // Sample XML response // --------------------------------------- // <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> // <inventory xmlns="http://walmart.com/"> // <sku>HOPP~904~51041</sku> // <quantity> // <unit>EACH</unit> // <amount>235</amount> // </quantity> // <fulfillmentLagTime>5</fulfillmentLagTime> // </inventory> CkStringBuilderW_Dispose(sbUrl); CkAuthUtilW_Dispose(authUtil); CkJsonObjectW_Dispose(json); CkHttpW_Dispose(http); CkXmlW_Dispose(xmlBody); CkXmlW_Dispose(xml); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.