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

PureBasic 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

 

 

 

(PureBasic) 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 PureBasic Module Download

Chilkat PureBasic Module

IncludeFile "CkBinData.pb"
IncludeFile "CkMime.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkGzip.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkZip.pb"
IncludeFile "CkHttpRequest.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkZipEntry.pb"
IncludeFile "CkStringBuilder.pb"

Procedure ChilkatExample()

    ; 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 ..."
    accessToken.s = "EBAY_ACCESS_TOKEN"

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    req.i = CkHttpRequest::ckCreate()
    If req.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpRequest::setCkHttpVerb(req, "POST")
    CkHttpRequest::setCkPath(req, "/FileTransferService")
    CkHttpRequest::setCkContentType(req, "application/xml")

    ; Build the XML body for the request.
    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml, "downloadFileRequest")
    CkXml::ckAddAttribute(xml,"xmlns","http://www.ebay.com/marketplace/services")
    CkXml::ckUpdateChildContent(xml,"taskReferenceId","50013004806")
    CkXml::ckUpdateChildContent(xml,"fileReferenceId","50015579016")

    CkHttpRequest::ckLoadBodyFromString(req,CkXml::ckGetXml(xml),"utf-8")

    ; 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>

    CkHttpRequest::ckAddHeader(req,"X-EBAY-SOA-OPERATION-NAME","downloadFile")
    CkHttpRequest::ckAddHeader(req,"X-EBAY-SOA-SECURITY-TOKEN",accessToken)

    resp.i = CkHttp::ckSynchronousRequest(http,"storage.sandbox.ebay.com",443,1,req)
    If CkHttp::ckLastMethodSuccess(http) <> 1
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    statusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response status code = " + Str(statusCode)

    responseBody.i = CkBinData::ckCreate()
    If responseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodyBd(resp,responseBody)
    CkHttpResponse::ckDispose(resp)

    ; 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.)
    CkBinData::ckWriteFile(responseBody,"qa_output/response.mime")

    If statusCode <> 200
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkXml::ckDispose(xml)
        CkBinData::ckDispose(responseBody)
        ProcedureReturn
    EndIf

    ; 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.
    mime.i = CkMime::ckCreate()
    If mime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i = CkMime::ckLoadMimeBd(mime,responseBody)
    If success <> 1
        Debug CkMime::ckLastErrorText(mime)
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkXml::ckDispose(xml)
        CkBinData::ckDispose(responseBody)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    ; 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"
    If CkMime::ckNumParts(mime) <> 2
        Debug "Expected the MIME to have 2 parts."
        Debug "NumParts = " + Str(CkMime::ckNumParts(mime))
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkXml::ckDispose(xml)
        CkBinData::ckDispose(responseBody)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    ; Get the XML from the 1st MIME sub-part.
    part0.i = CkMime::ckGetPart(mime,0)
    downloadResponseXml.s = CkMime::ckGetBodyDecoded(part0)
    xmlResp.i = CkXml::ckCreate()
    If xmlResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::ckLoadXml(xmlResp,downloadResponseXml)
    Debug "Download Response XML:"
    Debug CkXml::ckGetXml(xmlResp)
    CkMime::ckDispose(part0)

    Debug "----"

    ; Now get the zip from the second part (index=1), unzip, and examine..
    part1.i = CkMime::ckGetPart(mime,1)
    zipData.i = CkBinData::ckCreate()
    If zipData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; This example requires Chilkat v9.5.0.67 or later.
    ; The GetBodyBd method was added in v9.5.0.67.
    CkMime::ckGetBodyBd(part1,zipData)

    ; Check to see if we have a zip or gzip.
    sbContentType.i = CkStringBuilder::ckCreate()
    If sbContentType.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbContentType,CkMime::ckContentType(part1))
    CkMime::ckDispose(part1)

    xmlFromZip.i = CkXml::ckCreate()
    If xmlFromZip.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    If CkStringBuilder::ckContains(sbContentType,"gzip",0) = 1
        ; This is a gzip compressed file.
        gzip.i = CkGzip::ckCreate()
        If gzip.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        ; in-place uncompress the data.
        ; Note: The UncompressBd method was added in Chilkat v9.5.0.67
        success = CkGzip::ckUncompressBd(gzip,zipData)
        If success <> 1
            Debug CkGzip::ckLastErrorText(gzip)
            CkHttp::ckDispose(http)
            CkHttpRequest::ckDispose(req)
            CkXml::ckDispose(xml)
            CkBinData::ckDispose(responseBody)
            CkMime::ckDispose(mime)
            CkXml::ckDispose(xmlResp)
            CkBinData::ckDispose(zipData)
            CkStringBuilder::ckDispose(sbContentType)
            CkXml::ckDispose(xmlFromZip)
            CkGzip::ckDispose(gzip)
            ProcedureReturn
        EndIf

        CkXml::ckLoadXml(xmlFromZip,CkBinData::ckGetString(zipData,"utf-8"))

    Else
        ; This is a zip archive.

        ; Load the body into a Zip object.
        zip.i = CkZip::ckCreate()
        If zip.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        success = CkZip::ckOpenBd(zip,zipData)
        If success <> 1
            Debug CkZip::ckLastErrorText(zip)
            CkHttp::ckDispose(http)
            CkHttpRequest::ckDispose(req)
            CkXml::ckDispose(xml)
            CkBinData::ckDispose(responseBody)
            CkMime::ckDispose(mime)
            CkXml::ckDispose(xmlResp)
            CkBinData::ckDispose(zipData)
            CkStringBuilder::ckDispose(sbContentType)
            CkXml::ckDispose(xmlFromZip)
            CkGzip::ckDispose(gzip)
            CkZip::ckDispose(zip)
            ProcedureReturn
        EndIf

        ; Save the .zip to a file (so we can examine it for debugging if something is not as expected)
        CkBinData::ckWriteFile(zipData,"qa_output/ebay_data.zip")

        ; The zip should contain a single XML file.
        If CkZip::ckNumEntries(zip) <> 1
            Debug "Expected the .zip to have 1 entry."
            Debug "NumEntries = " + Str(CkZip::ckNumEntries(zip))
            Debug "Failed."
            CkHttp::ckDispose(http)
            CkHttpRequest::ckDispose(req)
            CkXml::ckDispose(xml)
            CkBinData::ckDispose(responseBody)
            CkMime::ckDispose(mime)
            CkXml::ckDispose(xmlResp)
            CkBinData::ckDispose(zipData)
            CkStringBuilder::ckDispose(sbContentType)
            CkXml::ckDispose(xmlFromZip)
            CkGzip::ckDispose(gzip)
            CkZip::ckDispose(zip)
            ProcedureReturn
        EndIf

        entry.i = CkZip::ckGetEntryByIndex(zip,0)

        CkXml::ckLoadXml(xmlFromZip,CkZipEntry::ckUnzipToString(entry,0,"utf-8"))
        CkZipEntry::ckDispose(entry)

    EndIf

    Debug "XML contained in the zip:"
    Debug CkXml::ckGetXml(xmlFromZip)
    Debug "----"

    Debug "Success."


    CkHttp::ckDispose(http)
    CkHttpRequest::ckDispose(req)
    CkXml::ckDispose(xml)
    CkBinData::ckDispose(responseBody)
    CkMime::ckDispose(mime)
    CkXml::ckDispose(xmlResp)
    CkBinData::ckDispose(zipData)
    CkStringBuilder::ckDispose(sbContentType)
    CkXml::ckDispose(xmlFromZip)
    CkGzip::ckDispose(gzip)
    CkZip::ckDispose(zip)


    ProcedureReturn
EndProcedure

 

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