![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Walmart - Update Inventory for an ItemNote: This example uses classes, properties, or methods discontinued in Chilkat v11.0.0 or greater. Demonstrates how to update inventory for an item.
IncludeFile "CkXml.pb" IncludeFile "CkHttp.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkAuthUtil.pb" IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() ; --------------------------------------------------------------------------------------------------------- ; Note: This example is deprecated. The Walmart API no longer uses the Signature method of authenticating. ; Walmart now uses OAuth2. ; --------------------------------------------------------------------------------------------------------- ; 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.i = CkStringBuilder::ckCreate() If sbUrl.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkStringBuilder::ckAppend(sbUrl,"https://marketplace.walmartapis.com/v2/inventory?sku={SKU}") numReplaced.i = CkStringBuilder::ckReplace(sbUrl,"{SKU}","HOPP~904~51041") requestMethod.s = "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.i = CkAuthUtil::ckCreate() If authUtil.i = 0 Debug "Failed to create object." ProcedureReturn EndIf wmConsumerId.s = "WALMART_CONSUMER_ID" wmPrivateKey.s = "WALMART_PRIVATE_KEY" jsonStr.s = CkAuthUtil::ckWalmartSignature(authUtil,CkStringBuilder::ckGetAsString(sbUrl),wmConsumerId,wmPrivateKey,requestMethod) If CkAuthUtil::ckLastMethodSuccess(authUtil) <> 1 Debug CkAuthUtil::ckLastErrorText(authUtil) CkStringBuilder::ckDispose(sbUrl) CkAuthUtil::ckDispose(authUtil) ProcedureReturn EndIf ; 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.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoad(json,jsonStr) http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttp::ckSetRequestHeader(http,"WM_SVC.NAME","Walmart Marketplace") CkHttp::ckSetRequestHeader(http,"WM_QOS.CORRELATION_ID",CkJsonObject::ckStringOf(json,"correlation_id")) CkHttp::ckSetRequestHeader(http,"WM_SEC.TIMESTAMP",CkJsonObject::ckStringOf(json,"timestamp")) CkHttp::ckSetRequestHeader(http,"WM_SEC.AUTH_SIGNATURE",CkJsonObject::ckStringOf(json,"signature")) CkHttp::ckSetRequestHeader(http,"WM_CONSUMER.ID",wmConsumerId) CkHttp::setCkAccept(http, "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.i = 234 xmlBody.i = CkXml::ckCreate() If xmlBody.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xmlBody, "wm:inventory") CkXml::ckAddAttribute(xmlBody,"xmlns:wm","http://walmart.com/") CkXml::ckUpdateChildContent(xmlBody,"wm:sku","HOPP~904~51041") CkXml::ckUpdateChildContent(xmlBody,"wm:quantity|wm:unit","EACH") CkXml::ckUpdateChildContentInt(xmlBody,"wm:quantity|wm:amount",originalAmount + 1) CkXml::ckUpdateChildContent(xmlBody,"wm:fulfillmentLagTime","5") Debug "PUT request body:" Debug CkXml::ckGetXml(xmlBody) Debug "--" ; This is a simple PUT that can be sent w/ the PutText method. xmlStr.s = CkHttp::ckPutText(http,CkStringBuilder::ckGetAsString(sbUrl),CkXml::ckGetXml(xmlBody),"utf-8","application/xml",0,0) If CkHttp::ckLastMethodSuccess(http) <> 1 Debug CkHttp::ckLastErrorText(http) CkStringBuilder::ckDispose(sbUrl) CkAuthUtil::ckDispose(authUtil) CkJsonObject::ckDispose(json) CkHttp::ckDispose(http) CkXml::ckDispose(xmlBody) ProcedureReturn EndIf xml.i = CkXml::ckCreate() If xml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::ckLoadXml(xml,xmlStr) ; A successful response should have a 200 response status If CkHttp::ckLastStatus(http) <> 200 Debug CkXml::ckGetXml(xml) Debug "Response Status Code: " + Str(CkHttp::ckLastStatus(http)) Debug "Failed." CkStringBuilder::ckDispose(sbUrl) CkAuthUtil::ckDispose(authUtil) CkJsonObject::ckDispose(json) CkHttp::ckDispose(http) CkXml::ckDispose(xmlBody) CkXml::ckDispose(xml) ProcedureReturn EndIf ; A sample XML response is shown below.. Debug CkXml::ckGetXml(xml) Debug "--" Debug "Success!" ; --------------------------------------- ; 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> CkStringBuilder::ckDispose(sbUrl) CkAuthUtil::ckDispose(authUtil) CkJsonObject::ckDispose(json) CkHttp::ckDispose(http) CkXml::ckDispose(xmlBody) CkXml::ckDispose(xml) ProcedureReturn EndProcedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.