labkeyInterface.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import urllib3
  2. #requests doesnt work as it lacks key password checking
  3. import chardet
  4. import json
  5. class labkeyInterface:
  6. def init(self,fname):
  7. #fname is a file with configuration
  8. try:
  9. f=open(fname)
  10. except OSError as e:
  11. print("Configuration error: OS error({}): {} {}".format(e.errno,fname, e.strerror))
  12. raise
  13. self.connectionConfig=json.load(f)
  14. self.initRemote()
  15. def initRemote(self):
  16. self.http=urllib3.PoolManager()
  17. if 'SSL' in self.connectionConfig:
  18. try:
  19. self.http = urllib3.PoolManager(\
  20. cert_file=self.connectionConfig['SSL']['user'],\
  21. cert_reqs='CERT_REQUIRED',\
  22. key_file=self.connectionConfig['SSL']['key'],\
  23. ca_certs=self.connectionConfig['SSL']['ca'])
  24. except KeyError:
  25. self.http = urllib3.PoolManager(\
  26. cert_reqs='CERT_REQUIRED',\
  27. ca_certs=self.connectionConfig['SSL']['ca'])
  28. #password=self.connectionConfig['SSL']['keyPwd'],\ doesnt work until 1.25
  29. def GetLabkeyUrl(self):
  30. return self.connectionConfig['host']+"/labkey"
  31. def GetLabkeyWebdavUrl(self):
  32. return self.GetLabkeyUrl()+"/_webdav"
  33. def getBasicAuth(self):
  34. user=self.connectionConfig['labkey']['user']
  35. pwd=self.connectionConfig['labkey']['password']
  36. return user+":"+pwd
  37. def get(self,url,binary=False):
  38. debug=False
  39. if debug:
  40. print("GET: {0}".format(url))
  41. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  42. try:
  43. if not binary:
  44. return self.http.request('GET',url,headers=headers)
  45. else:
  46. return self.http.request('GET',url,headers=headers,preload_content=False)
  47. #f contains json as a return value
  48. #f contains json as a return value
  49. except urllib3.exceptions.HTTPError as e:
  50. print(e)
  51. #useful for querying code
  52. return e
  53. def head(self,url):
  54. debug=False
  55. if debug:
  56. print("HEAD: {0}".format(url))
  57. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  58. try:
  59. return self.http.request('HEAD',url,headers=headers)
  60. except urllib3.exceptions.HTTPError as e:
  61. print(e)
  62. #usefule for querying code
  63. return e
  64. def post(self,url,data):
  65. debug=False
  66. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  67. headers["Content-Type"]="application/json"
  68. #add csrf;also sets self.cookie
  69. headers["X-LABKEY-CSRF"]=self.getCSRF()
  70. headers["Cookie"]=self.cookie
  71. try:
  72. return self.http.request('POST',url,headers=headers,body=data)
  73. #f contains json as a return value
  74. except urllib3.exceptions.HTTPError as e:
  75. print(e)
  76. def mkcol(self,url):
  77. debug=False
  78. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  79. #add csrf;also sets self.cookie
  80. headers["X-LABKEY-CSRF"]=self.getCSRF()
  81. headers["Cookie"]=self.cookie
  82. try:
  83. return self.http.request('MKCOL',url,headers=headers)
  84. #f contains json as a return value
  85. except urllib3.exceptions.HTTPError as e:
  86. print(e)
  87. def put(self,url,data):
  88. debug=False
  89. if debug:
  90. print("PUT: {}").format(url)
  91. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  92. headers["Content-Type"]="application/octet-stream"
  93. #add csrf
  94. headers["X-LABKEY-CSRF"]=self.getCSRF()
  95. headers["Cookie"]=self.cookie
  96. try:
  97. return self.http.request('PUT',url,headers=headers,body=data)
  98. #f contains json as a return value
  99. except urllib3.exceptions.HTTPError as e:
  100. print(e)
  101. def getCSRF(self):
  102. url=self.GetLabkeyUrl()+'/login/whoAmI.view'
  103. try:
  104. response=self.get(url)
  105. encoding=chardet.detect(response.data)['encoding']
  106. #print(f'Got encoding: {encoding}')
  107. jsonData=json.loads(response.data.decode(encoding))
  108. except AttributeError:
  109. print('Failed')
  110. return None
  111. except UnicodeDecodeError:
  112. try:
  113. encoding='ASCII'
  114. jsonData=json.loads(response.data.decode(encoding))
  115. except UnicodeDecodeError:
  116. print(response.data)
  117. return None
  118. self.cookie=response.getheader('Set-Cookie')
  119. print('User: {} CSRF: {}'.format(jsonData['displayName'],jsonData['CSRF']))
  120. return jsonData["CSRF"]
  121. def getUserId(self):
  122. url=self.GetLabkeyUrl()+'/login/whoAmI.view'
  123. try:
  124. response=self.get(url)
  125. self.cookie=response.getheader('Set-Cookie')
  126. encoding=chardet.detect(response.data)['encoding']
  127. jsonData=json.loads(response.data.decode(encoding))
  128. return jsonData
  129. except AttributeError:
  130. return None