orthancInterface.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import urllib3
  2. #requests doesnt work as it lacks key password checking
  3. import json
  4. def setContentType(headers,dataType):
  5. if dataType=='text':
  6. headers['Content-Type']="text/plain"
  7. return
  8. headers['Content-Type']="application/"+dataType
  9. class orthancInterface:
  10. def init(self,fname):
  11. #fname is a file with configuration
  12. try:
  13. f=open(fname)
  14. except OSError as e:
  15. print("Confgiuration error: OS error({0}): {1}").format(e.errno, e.strerror)
  16. raise
  17. self.connectionConfig=json.load(f)
  18. self.http=urllib3.PoolManager()
  19. if 'SSL' in self.connectionConfig['orthanc']:
  20. sslConfig=self.connectionConfig['orthanc']['SSL']
  21. self.http = urllib3.PoolManager(\
  22. cert_file=sslConfig['user'],\
  23. cert_reqs='CERT_REQUIRED',\
  24. key_file=sslConfig['key'],\
  25. ca_certs=sslConfig['ca'])
  26. #password=self.connectionConfig['SSL']['keyPwd'],\ doesnt work until 1.25
  27. def store(self):
  28. #store current orthanc connection settings (server/user/password)
  29. obj=self.connectionConfig['orthanc']
  30. return {x:obj[x] for x in obj}
  31. def set(self,obj):
  32. #overload connection settings from object (server/user/password)
  33. for x in obj:
  34. self.connectionConfig['orthanc'][x]=obj[x]
  35. def getBasicAuth(self):
  36. user=self.connectionConfig['orthanc']['user']
  37. pwd=self.connectionConfig['orthanc']['password']
  38. return user+":"+pwd
  39. def getCoreURL(self):
  40. return self.connectionConfig['orthanc']['server']
  41. def get(self,url,binary=False):
  42. debug=False
  43. if debug:
  44. print("GET: {0}").format(url)
  45. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  46. try:
  47. if not binary:
  48. return self.http.request('GET',url,headers=headers)
  49. else:
  50. return self.http.request('GET',url,headers=headers,preload_content=False)
  51. #f contains json as a return value
  52. except urllib3.exceptions.HTTPError as e:
  53. print(e)
  54. def post(self,url,data,dataType):
  55. #dataType is typically json or octet-stream
  56. debug=False
  57. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  58. setContentType(headers,dataType)
  59. #add csrf;also sets self.cookie
  60. #headers["X-LABKEY-CSRF"]=self.getCSRF()
  61. #doesn't seem like orthanc is big on security
  62. #headers["Cookie"]=self.cookie
  63. try:
  64. return self.http.request('POST',url,headers=headers,body=data)
  65. #f contains json as a return value
  66. except urllib3.exceptions.HTTPError as e:
  67. print(e)
  68. def put(self,url,data,dataType='octet-stream',timeout=None):
  69. debug=False
  70. if debug:
  71. print("PUT: {}").format(url)
  72. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  73. setContentType(headers,dataType)
  74. #add csrf
  75. #headers["X-LABKEY-CSRF"]=self.getCSRF()
  76. #headers["Cookie"]=self.cookie
  77. try:
  78. return self.http.request('PUT',url,headers=headers,body=data,timeout=timeout)
  79. #f contains json as a return value
  80. except urllib3.exceptions.HTTPError as e:
  81. print(e)
  82. def delete(self,url,timeout=None):
  83. debug=False
  84. if debug:
  85. print("DELETE: {0}").format(url)
  86. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  87. try:
  88. return self.http.request('DELETE',url,headers=headers,timeout=timeout)
  89. except urllib3.exceptions.HTTPError as e:
  90. print(e)
  91. return None