Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Web API Examples

Primary Categories

AWS Translate
Activix CRM
Adyen
Amazon MWS
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Bluzone
CardConnect
ClickBank
Clickatell
Cloudfare
DocuSign
ETrade
Etsy
Facebook
GeoOp
GetHarvest
Jira
MYOB

Magento
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Okta OAuth/OIDC
OneLogin OIDC
PayPal
Peoplevox
Populi
QuickBooks
SOAP finkok.com
Shippo
Shopify
Stripe
SugarCRM
Trello
Twitter
VoiceBase
Vonage
Walmart
Walmart v3
Xero
eBay
effectconnect

 

 

 

(DataFlex) eBay -- Download Data using FileTransferService

Demonstrates how to download a data file using the eBay File Transfer API.

Note: This example requires Chilkat v9.5.0.67 or later.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    String sAccessToken
    Handle hoHttp
    Variant vReq
    Handle hoReq
    Handle hoXml
    Variant vResp
    Handle hoResp
    Integer iStatusCode
    Variant vResponseBody
    Handle hoResponseBody
    Handle hoMime
    Boolean iSuccess
    Variant vPart0
    Handle hoPart0
    String sDownloadResponseXml
Resp    Handle hoXmlResp
    Variant vPart1
    Handle hoPart1
Data    Handle hoZipData
    Handle hoSbContentType
FromZip    Handle hoXmlFromZip
    Handle hoGzip
    Handle hoZip
    Variant vEntry
    Handle hoEntry
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Use a previously obtained access token.  The token should look something like this:
    // "AgAAAA**AQA ..."
    Move "EBAY_ACCESS_TOKEN" To sAccessToken

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End

    Set ComHttpVerb Of hoReq To "POST"
    Set ComPath Of hoReq To "/FileTransferService"
    Set ComContentType Of hoReq To "application/xml"

    // Build the XML body for the request.
    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "downloadFileRequest"
    Get ComAddAttribute Of hoXml "xmlns" "http://www.ebay.com/marketplace/services" To iSuccess
    Send ComUpdateChildContent To hoXml "taskReferenceId" "50013004806"
    Send ComUpdateChildContent To hoXml "fileReferenceId" "50015579016"

    Get ComGetXml Of hoXml To sTemp1
    Get ComLoadBodyFromString Of hoReq sTemp1 "utf-8" To iSuccess

    // The XML body looks like this:

    // 	<?xml version="1.0" encoding="UTF-8"?>
    // 	<downloadFileRequest xmlns="http://www.ebay.com/marketplace/services">
    // 	<taskReferenceId>50013004806</taskReferenceId>
    // 	<fileReferenceId>50015579016</fileReferenceId>
    // 	</downloadFileRequest>

    Send ComAddHeader To hoReq "X-EBAY-SOA-OPERATION-NAME" "downloadFile"
    Send ComAddHeader To hoReq "X-EBAY-SOA-SECURITY-TOKEN" sAccessToken

    Get pvComObject of hoReq to vReq
    Get ComSynchronousRequest Of hoHttp "storage.sandbox.ebay.com" 443 True vReq To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iStatusCode
    Showln "Response status code = " iStatusCode

    Get Create (RefClass(cComChilkatBinData)) To hoResponseBody
    If (Not(IsComObjectCreated(hoResponseBody))) Begin
        Send CreateComObject of hoResponseBody
    End
    Get pvComObject of hoResponseBody to vResponseBody
    Get ComGetBodyBd Of hoResp vResponseBody To iSuccess
    Send Destroy of hoResp

    // We can save the response body to a file for examination if we get an unanticipated response.  
    // (It's binary, so it won't open well in a text editor.)
    Get ComWriteFile Of hoResponseBody "qa_output/response.mime" To iSuccess

    If (iStatusCode <> 200) Begin
        Showln "Failed."
        Procedure_Return
    End

    // The response body looks like this:

    // 	--MIMEBoundaryurn_uuid_2B668636C1E17A4D4114925305818684241
    // 	Content-Type: application/xop+xml; charset=utf-8; type="text/xml"
    // 	Content-Transfer-Encoding: binary
    // 	Content-ID: <0.urn:uuid:2B668636C1E17A4D4114925305818684242>
    // 
    // 	<?xml version='1.0' encoding='UTF-8'?>
    // 	<downloadFileResponse xmlns="http://www.ebay.com/marketplace/services">
    // 	<ack>Success</ack>
    // 	<version>1.1.0</version>
    // 	<timestamp>2017-04-18T15:49:41.868Z</timestamp>
    // 	<fileAttachment>
    // 	    <Size>587</Size>
    // 	    <Data>
    //                 <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:urn:uuid:A37C3C73E994C267F11492530585522"/>
    // 	    </Data>
    // 	</fileAttachment>
    // 	</downloadFileResponse>
    // 	--MIMEBoundaryurn_uuid_2B668636C1E17A4D4114925305818684241
    // 	Content-Type: application/zip
    // 	Content-Transfer-Encoding: binary
    // 	Content-ID: <urn:uuid:A37C3C73E994C267F11492530585522>
    // 
    // 	<the binary bytes of the zip start here...>
    // 

    // Load the binary response into a MIME object.
    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End
    Get pvComObject of hoResponseBody to vResponseBody
    Get ComLoadMimeBd Of hoMime vResponseBody To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Make sure we have 2 sub-parts.  The 1st sub-part is the XML response, the 2nd sub-part
    // is the zip containing the data.

    // Note: The 2nd sub-part can be a "zip" or "gzip".  These are two different file formats.
    // A zip is indicated with a Content-Type header equal to "application/zip",
    // whereas a gzip is indicated with a Content-Type header equal to "application/x-gzip"
    Get ComNumParts Of hoMime To iTemp1
    If (iTemp1 <> 2) Begin
        Showln "Expected the MIME to have 2 parts."
        Get ComNumParts Of hoMime To iTemp1
        Showln "NumParts = " iTemp1
        Showln "Failed."
        Procedure_Return
    End

    // Get the XML from the 1st MIME sub-part.
    Get ComGetPart Of hoMime 0 To vPart0
    If (IsComObject(vPart0)) Begin
        Get Create (RefClass(cComChilkatMime)) To hoPart0
        Set pvComObject Of hoPart0 To vPart0
    End
    Get ComGetBodyDecoded Of hoPart0 To sDownloadResponseXml
    Get Create (RefClass(cComChilkatXml)) To hoXmlResp
    If (Not(IsComObjectCreated(hoXmlResp))) Begin
        Send CreateComObject of hoXmlResp
    End
    Get ComLoadXml Of hoXmlResp sDownloadResponseXml To iSuccess
    Showln "Download Response XML:"
    Get ComGetXml Of hoXmlResp To sTemp1
    Showln sTemp1
    Send Destroy of hoPart0

    Showln "----"

    // Now get the zip from the second part (index=1), unzip, and examine..
    Get ComGetPart Of hoMime 1 To vPart1
    If (IsComObject(vPart1)) Begin
        Get Create (RefClass(cComChilkatMime)) To hoPart1
        Set pvComObject Of hoPart1 To vPart1
    End
    Get Create (RefClass(cComChilkatBinData)) To hoZipData
    If (Not(IsComObjectCreated(hoZipData))) Begin
        Send CreateComObject of hoZipData
    End
    // This example requires Chilkat v9.5.0.67 or later.
    // The GetBodyBd method was added in v9.5.0.67.
    Get pvComObject of hoZipData to vZipData
    Get ComGetBodyBd Of hoPart1 vZipData To iSuccess

    // Check to see if we have a zip or gzip.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbContentType
    If (Not(IsComObjectCreated(hoSbContentType))) Begin
        Send CreateComObject of hoSbContentType
    End
    Get ComContentType Of hoPart1 To sTemp1
    Get ComAppend Of hoSbContentType sTemp1 To iSuccess
    Send Destroy of hoPart1

    Get Create (RefClass(cComChilkatXml)) To hoXmlFromZip
    If (Not(IsComObjectCreated(hoXmlFromZip))) Begin
        Send CreateComObject of hoXmlFromZip
    End

    Get ComContains Of hoSbContentType "gzip" False To bTemp1
    If (bTemp1 = True) Begin
        // This is a gzip compressed file.
        Get Create (RefClass(cComChilkatGzip)) To hoGzip
        If (Not(IsComObjectCreated(hoGzip))) Begin
            Send CreateComObject of hoGzip
        End

        // in-place uncompress the data.
        // Note: The UncompressBd method was added in Chilkat v9.5.0.67
        Get pvComObject of hoZipData to vZipData
        Get ComUncompressBd Of hoGzip vZipData To iSuccess
        If (iSuccess <> True) Begin
            Get ComLastErrorText Of hoGzip To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComGetString Of hoZipData "utf-8" To sTemp1
        Get ComLoadXml Of hoXmlFromZip sTemp1 To iSuccess

    End
    Else Begin
        // This is a zip archive.

        // Load the body into a Zip object.
        Get Create (RefClass(cComChilkatZip)) To hoZip
        If (Not(IsComObjectCreated(hoZip))) Begin
            Send CreateComObject of hoZip
        End
        Get pvComObject of hoZipData to vZipData
        Get ComOpenBd Of hoZip vZipData To iSuccess
        If (iSuccess <> True) Begin
            Get ComLastErrorText Of hoZip To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        // Save the .zip to a file (so we can examine it for debugging if something is not as expected)
        Get ComWriteFile Of hoZipData "qa_output/ebay_data.zip" To iSuccess

        // The zip should contain a single XML file.
        Get ComNumEntries Of hoZip To iTemp1
        If (iTemp1 <> 1) Begin
            Showln "Expected the .zip to have 1 entry."
            Get ComNumEntries Of hoZip To iTemp1
            Showln "NumEntries = " iTemp1
            Showln "Failed."
            Procedure_Return
        End

        Get ComGetEntryByIndex Of hoZip 0 To vEntry
        If (IsComObject(vEntry)) Begin
            Get Create (RefClass(cComChilkatZipEntry)) To hoEntry
            Set pvComObject Of hoEntry To vEntry
        End

        Get ComUnzipToString Of hoEntry 0 "utf-8" To sTemp1
        Get ComLoadXml Of hoXmlFromZip sTemp1 To iSuccess
        Send Destroy of hoEntry
    End

    Showln "XML contained in the zip:"
    Get ComGetXml Of hoXmlFromZip To sTemp1
    Showln sTemp1
    Showln "----"

    Showln "Success."


End_Procedure

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.