Downloading an attachment from RTC workitem via REST using Python
Hi All,
I am trying to download an attachment of workitem via REST using python. However, I haven't been able to locate any useful APIs to do so. I found the JAVA API that does the download but is there any REST Api I can use in my python code. I tried with python module called rtcclient but it didn't work for me.
Thanks in advance.
|
Accepted answer
Ian Barnard (2.1k●6●13)
| answered Sep 22 '21, 8:39 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER Hi Yogesh
It is possible to download attachments although TBH I'm not sure if this is formally part of the "OSLC" API, but the steps below do require the header OSLC-Core-Version: 2.0 to work.
Steps:
1. Using perhaps OSLC Query, retrieve the workitem URI e.g. https://jazz.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/38
2. GET from that URI with headers OSLC-Core-Version: 2.0 and Accept: application/rdf+xml
In the returned RDF, for each attachment there is a property like:
<rtc_cm:com.ibm.team.workitem.linktype.attachment.attachment rdf:ID="n6" rdf:resource="https://jazz.ibm.com:9443/ccm/resource/itemOid/com.ibm.team.workitem.Attachment/_IZszcRuREeyjc_YwJfTLJA"/>
3. GET from URI in the rtc_cm:com.ibm.team.workitem.linktype.attachment.attachment tag using the same headers, this gives you the name of the attachment in a dcterms:title tag and a link to its content in a tag rtc_cm:content e.g.:
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:rtc_cm="http://jazz.net/xmlns/prod/jazz/rtc/cm/1.0/"
xmlns:dcterms="http://purl.org/dc/terms/"
<rtc_cm:Attachment rdf:about="https://jazz.ibm.com:9443/ccm/resource/itemOid/com.ibm.team.workitem.Attachment/_IZszcRuREeyjc_YwJfTLJA">
<dcterms:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2021-09-22T10:55:26.203Z</dcterms:modified>
<dcterms:creator rdf:resource="https://jazz.ibm.com:9443/jts/users/ibm"/>
<rtc_cm:contentLength rdf:datatype="http://www.w3.org/2001/XMLSchema#long">1923</rtc_cm:contentLength>
<dcterms:title rdf:datatype="http://www.w3.org/2001/XMLSchema#string">textfile.txt</dcterms:title>
<rtc_cm:modifiedBy rdf:resource="https://jazz.ibm.com:9443/jts/users/ibm"/>
<dcterms:created rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2021-09-22T10:37:49.068Z</dcterms:created>
<rtc_cm:content rdf:resource="https://jazz.ibm.com:9443/ccm/resource/content/_IZCsIRuREeyjc_YwJfTLJA"/>
<dcterms:format rdf:datatype="http://www.w3.org/2001/XMLSchema#string">application/octet-stream</dcterms:format>
<dcterms:identifier rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">1</dcterms:identifier>
<dcterms:description rdf:parseType="Literal">buildexe.bat</dcterms:description>
</rtc_cm:Attachment>
</rdf:RDF>
4. GET from that rtc_cm:content URI using the OSLC-Core-Version: 2.0 header and with the Referer header set to the URI, and you'll get the (binary) content
5. Save to the file named from the dcterms:title name
All this can be done using Python; I use the Requests package using a requests session with ElementTree or lxml to handle XML and it works admirably :-)
HTH
Ian
Yogesh Ranjitkar selected this answer as the correct answer
Comments Nice. Thanks for looking into it.
Yogesh Ranjitkar
commented Sep 22 '21, 6:52 p.m.
Hi Ian Barnard,
Thanks for the reply. I have got this thing working and the process you mentioned was exactly what I did in my script as well. It works perfectly.. Thanks again.
-Yogesh
|
3 other answers
Ralph Schoon (63.4k●3●36●46)
| answered Sep 16 '21, 3:11 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
This question has been asked here several times and answered several times.
To my knowledge the answer is that there is no supported REST API to download work item attachments.
The Java API you found is supported. There is no other API. I know that others have reverse engineered the API used by the Web client. This is possible but not supported. It is also not a REST API as far as I know.
Comments Hi Ralph,
Thanks for your response. So does that mean there is no python way to achieve it. Can I use OSLC approach in python. Not sure though if that helps
Ralph Schoon
commented Sep 17 '21, 2:13 a.m.
| edited Sep 17 '21, 8:40 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
I am unsure what is left open in the answer, but ok.
I am not a Python hero, but I would assume you can call an application e.g. based on the RTC Plain Java Client Libraries in Python. So you could use the Java API and develop a tool like https://rsjazz.wordpress.com/2012/09/21/downloading-attachments-from-work-items/ and use that. Then you would have a way to download attachments in Python, just not using OLSC.
Unrelated to the question, yes, it is possible to use OSLC APIS in Python (use the requests library).
But as there is no OSLC support for downloading attachments, you can't solve that specific task you ask about with OSLC. Not in Python, not in any other language or tool that could be used to create REST/OSLC calls.
|
I also need the same material as you. Hope everybody help please |
@ Ian Barnard @Yogesh Ranjitkar
Nice that you got all details figured out and got it work in Python. Any chance you can share the Python code, or would be interested to add this function to rtcclient, (https://github.com/dixudx/rtcclient), another related OSS package? Thanks!
Comments There's an example of downloading an attachment in my OSS package elmclient - see https://github.com/IBM/ELM-Python-Client - the example code is here https://github.com/IBM/ELM-Python-Client/blob/master/elmclient/examples/ccm_simple_attachmentdownload.py |
Your answer
Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.