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 - Get Inventory for an ItemDemonstrates how to get inventory for an item and parse the result.
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 GET request: ; GET 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 = "GET" ; 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. xmlStr.s = CkHttp::ckQuickGetStr(http,CkStringBuilder::ckGetAsString(sbUrl)) If CkHttp::ckLastMethodSuccess(http) <> 1 Debug CkHttp::ckLastErrorText(http) CkStringBuilder::ckDispose(sbUrl) CkAuthUtil::ckDispose(authUtil) CkJsonObject::ckDispose(json) CkHttp::ckDispose(http) 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(xml) ProcedureReturn EndIf ; A sample XML response is shown below.. Debug CkXml::ckGetXml(xml) Debug "--" ; To get the quantity: quantity.i = CkXml::ckGetChildIntValue(xml,"quantity|amount") Debug "quantity = " + Str(quantity) ; --------------------------------------- ; 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>234</amount> ; </quantity> ; <fulfillmentLagTime>5</fulfillmentLagTime> ; </inventory> CkStringBuilder::ckDispose(sbUrl) CkAuthUtil::ckDispose(authUtil) CkJsonObject::ckDispose(json) CkHttp::ckDispose(http) CkXml::ckDispose(xml) ProcedureReturn EndProcedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.