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
(PowerBuilder) Xero Get Attachment (Download a Xero Attachment)Demonstrates how to get the content of an attachment in Xero. Note: This example requires Chilkat v9.5.0.64 or greater.
integer li_rc oleobject loo_Rest integer li_Success string ls_EndPoint string ls_ReceiptID oleobject loo_SbPath integer li_NumReplaced string ls_ResponseXml oleobject loo_Xml integer li_NumRecords oleobject loo_SbSaveFilePath oleobject loo_SbAttachmentPath oleobject loo_AttachmentData integer i string ls_AttachmentID string ls_Filename integer li_StatusCode string ls_ResponseBody // Note: Requires Chilkat v9.5.0.64 or greater. // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Rest = create oleobject li_rc = loo_Rest.ConnectToNewObject("Chilkat_9_5_0.Rest") if li_rc < 0 then destroy loo_Rest MessageBox("Error","Connecting to COM object failed") return end if // Before sending REST API calls, the REST object needs to be // initialized for OAuth1. // See Xero 2-Legged OAuth1 Setup for sample code. // Assuming the REST object's OAuth1 authenticator is setup, and the initial // connection was made, we may now send REST HTTP requests.. // -------------------------------------------------------------- // First get a list of attachments for a given document (in this case a Receipt). ls_EndPoint = "Receipts" ls_ReceiptID = "c4f40e59-c390-0001-caff-ce731c707d00" loo_SbPath = create oleobject li_rc = loo_SbPath.ConnectToNewObject("Chilkat_9_5_0.StringBuilder") loo_SbPath.Append("/api.xro/2.0/{Endpoint}/{Guid}/Attachments/") li_NumReplaced = loo_SbPath.Replace("{Endpoint}",ls_EndPoint) li_NumReplaced = loo_SbPath.Replace("{Guid}",ls_ReceiptID) ls_ResponseXml = loo_Rest.FullRequestNoBody("GET",loo_SbPath.GetAsString()) if loo_Rest.LastMethodSuccess <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_Rest destroy loo_SbPath return end if // A 200 response is expected for actual success. if loo_Rest.ResponseStatusCode <> 200 then Write-Debug ls_ResponseXml destroy loo_Rest destroy loo_SbPath return end if // Examine the response. Write-Debug ls_ResponseXml // A sample response looks like this: // <Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> // <Id>b235646f-34ac-4b15-90ce-d63267f7cd33</Id> // <Status>OK</Status> // <ProviderName>ChilkatPrivate</ProviderName> // <DateTimeUTC>2016-11-11T14:32:17.0908971Z</DateTimeUTC> // <Attachments> // <Attachment> // <AttachmentID>0edcddc8-325f-40c7-b950-8c71f14afc7c</AttachmentID> // <FileName>penguins.jpg</FileName> // <Url>http://api.xero.com/api.xro/2.0/Receipts/c4f40e59-c390-0001-caff-ce731c707d00/Attachments/penguins.jpg</Url> // <MimeType>image/jpg</MimeType> // <ContentLength>777835</ContentLength> // </Attachment> // <Attachment> // <AttachmentID>0adffdc8-325f-65c7-b950-4391f14af908</AttachmentID> // <FileName>starfish.jpg</FileName> // <Url>http://api.xero.com/api.xro/2.0/Receipts/c4f40e59-c390-0001-caff-ce731c707d00/Attachments/starfish.jpg</Url> // <MimeType>image/jpg</MimeType> // <ContentLength>24537</ContentLength> // </Attachment> // </Attachments> // </Response> loo_Xml = create oleobject li_rc = loo_Xml.ConnectToNewObject("Chilkat_9_5_0.Xml") loo_Xml.LoadXml(ls_ResponseXml) // Iterate over the attachments and download each. li_NumRecords = loo_Xml.NumChildrenAt("Attachments") Write-Debug "Number of Attachments = " + string(li_NumRecords) loo_SbSaveFilePath = create oleobject li_rc = loo_SbSaveFilePath.ConnectToNewObject("Chilkat_9_5_0.StringBuilder") loo_SbAttachmentPath = create oleobject li_rc = loo_SbAttachmentPath.ConnectToNewObject("Chilkat_9_5_0.StringBuilder") loo_AttachmentData = create oleobject li_rc = loo_AttachmentData.ConnectToNewObject("Chilkat_9_5_0.BinData") i = 0 do while i < li_NumRecords loo_Xml.I = i ls_AttachmentID = loo_Xml.GetChildContent("Attachments|Attachment[i]|AttachmentID") ls_Filename = loo_Xml.GetChildContent("Attachments|Attachment[i]|FileName") Write-Debug "AttachmentID: " + ls_AttachmentID Write-Debug "Filename: " + ls_Filename Write-Debug "----" // Download this attachment. // First build the path for this particular attachment by appending the FileName to the path used to get the list of attachments. loo_SbAttachmentPath.Clear() loo_SbAttachmentPath.AppendSb(loo_SbPath) loo_SbAttachmentPath.Append(ls_Filename) // Send the GET request in one call, and then get the response in the next two. li_Success = loo_Rest.SendReqNoBody("GET",loo_SbAttachmentPath.GetAsString()) if li_Success <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_Rest destroy loo_SbPath destroy loo_Xml destroy loo_SbSaveFilePath destroy loo_SbAttachmentPath destroy loo_AttachmentData return end if // Get the response header. If it's not a 200 success status code, then the response body does NOT contain // the attachment data. li_StatusCode = loo_Rest.ReadResponseHeader() if li_StatusCode = -1 then // We didn't get any response.. Write-Debug loo_Rest.LastErrorText destroy loo_Rest destroy loo_SbPath destroy loo_Xml destroy loo_SbSaveFilePath destroy loo_SbAttachmentPath destroy loo_AttachmentData return end if if li_StatusCode <> 200 then Write-Debug "Response Status: " + string(li_StatusCode) ls_ResponseBody = loo_Rest.ReadRespBodyString() if loo_Rest.LastMethodSuccess <> 1 then Write-Debug "Failed to read the response body." else Write-Debug ls_ResponseBody end if Write-Debug "Failed." destroy loo_Rest destroy loo_SbPath destroy loo_Xml destroy loo_SbSaveFilePath destroy loo_SbAttachmentPath destroy loo_AttachmentData return end if // OK, the response header indicates the attachment content is forthcoming... // There are a few ways to get the response body. If it is a very large attachment, it can be streamed directly // to a file. We'll assume it's not, so we'll get the response into a BinData object, and then save it to a file. loo_AttachmentData.Clear() li_Success = loo_Rest.ReadRespBd(loo_AttachmentData) if li_Success <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_Rest destroy loo_SbPath destroy loo_Xml destroy loo_SbSaveFilePath destroy loo_SbAttachmentPath destroy loo_AttachmentData return end if // Save the data to the file. loo_SbSaveFilePath.SetString("qa_output/") loo_SbSaveFilePath.Append(ls_Filename) li_Success = loo_AttachmentData.WriteFile(loo_SbSaveFilePath.GetAsString()) if li_Success <> 1 then Write-Debug "Failed to save to output file." else Write-Debug "Saved to " + loo_SbSaveFilePath.GetAsString() end if i = i + 1 loop destroy loo_Rest destroy loo_SbPath destroy loo_Xml destroy loo_SbSaveFilePath destroy loo_SbAttachmentPath destroy loo_AttachmentData |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.