Jump to content

Using Python to Invoke a webservice to enable drill-down to DOC and PDF files


Robert Cain

Recommended Posts

I subscribe to a web-service that can return Doc and PDF files based on ID fields that I maintain in spotfire. I want the users to be able to drill down to these DOC and PDF files. I'd prefer to do this in Python without calling another application. I do not want or need to load any of this data into the analysis.

This Proof of concept script seems to inoke the URL to download the file as evidencesd by the fact that the print command returns the first few lines from the PDF...

from System.Net import WebClient,NetworkCredential

from System.IO import StreamReader, Path, File, StreamWriter

from Spotfire.Dxp.Data import DataTableSaveSettings

from Spotfire.Dxp.Data.Import import TextFileDataSource

from Spotfire.Dxp.Data.Import import TextDataReaderSettings

from Spotfire.Dxp.Data import AddRowsSettings

from Spotfire.Dxp.Data import DataType

 

from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers

from Spotfire.Dxp.Application import DocumentSaveSettings

writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.StdfDataWriter)

 

 

url = 'https://api.cortellis.com/api-ws/ws/rs/drugs-v2/drug/ZZZZZZZfmt=PDF'

wc = WebClient(Credentials = NetworkCredential('XXXXXXX', 'YYYYYYY'))

wc.Headers.Add('Content-Type', 'application/x-www-form-urlencoded')

requestData= wc.OpenRead(url)

reader = StreamReader (requestData);

response = reader.ReadToEnd()

 

print response

#Application.SaveAs("C:Userscain_robertDesktopdeleteme.pdf",DocumentSaveSettings())

 

 

#f = open(tempFilename, 'w')

#f.write(response)

#f.close()I'd like to save the PDF and/orallow the user to open it. But I can't figure out how to do it.

Link to comment
Share on other sites

Hi Robert,

Please try the following script. It will download a pdf to a temporary file, then open the file using the default application for the file type. You'll need to fill in your credentials on the commented out request.Credentials line.

 

from System.Diagnostics import Process

from System.Net import NetworkCredential, HttpWebRequest, HttpWebResponse

from System.IO import Path, File

from Spotfire.Dxp.Data import DataTableSaveSettings

from Spotfire.Dxp.Data.Import import TextFileDataSource

from Spotfire.Dxp.Data.Import import TextDataReaderSettings

from Spotfire.Dxp.Data import AddRowsSettings

from Spotfire.Dxp.Data import DataType

 

from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers

from Spotfire.Dxp.Application import DocumentSaveSettings

 

url = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf'

 

filePath = Path.GetTempFileName()+".pdf"

 

request = HttpWebRequest.Create(url)

request.Method = "GET"

request.ContentType = "application/pdf"

#request.Credentials = ...

response = request.GetResponse()

stream = File.Create(filePath)

response.GetResponseStream().CopyTo(stream)

 

response.Dispose()

stream.Dispose()

 

process = Process.Start(filePath)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...