orthancInterface.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 getBasicAuth(self):
  28. user=self.connectionConfig['orthanc']['user']
  29. pwd=self.connectionConfig['orthanc']['password']
  30. return user+":"+pwd
  31. def getCoreURL(self):
  32. return self.connectionConfig['orthanc']['server']
  33. def get(self,url,binary=False):
  34. debug=False
  35. if debug:
  36. print("GET: {0}").format(url)
  37. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  38. try:
  39. if not binary:
  40. return self.http.request('GET',url,headers=headers)
  41. else:
  42. return self.http.request('GET',url,headers=headers,preload_content=False)
  43. #f contains json as a return value
  44. except urllib3.exceptions.HTTPError as e:
  45. print(e)
  46. def post(self,url,data,dataType):
  47. #dataType is typically json or octet-stream
  48. debug=False
  49. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  50. setContentType(headers,dataType)
  51. #add csrf;also sets self.cookie
  52. #headers["X-LABKEY-CSRF"]=self.getCSRF()
  53. #doesn't seem like orthanc is big on security
  54. #headers["Cookie"]=self.cookie
  55. try:
  56. return self.http.request('POST',url,headers=headers,body=data)
  57. #f contains json as a return value
  58. except urllib3.exceptions.HTTPError as e:
  59. print(e)
  60. def put(self,url,data,dataType='octet-stream'):
  61. debug=False
  62. if debug:
  63. print("PUT: {}").format(url)
  64. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  65. setContentType(headers,dataType)
  66. #add csrf
  67. #headers["X-LABKEY-CSRF"]=self.getCSRF()
  68. #headers["Cookie"]=self.cookie
  69. try:
  70. return self.http.request('PUT',url,headers=headers,body=data)
  71. #f contains json as a return value
  72. except urllib3.exceptions.HTTPError as e:
  73. print(e)
  74. def delete(self,url):
  75. debug=False
  76. if debug:
  77. print("DELETE: {0}").format(url)
  78. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  79. try:
  80. return self.http.request('DELETE',url,headers=headers)
  81. except urllib3.exceptions.HTTPError as e:
  82. print(e)
  83. return None