labkeyInterface.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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("Confgiuration error: OS error({0}): {1}").format(e.errno, e.strerror)
  12. raise
  13. self.connectionConfig=json.load(f)
  14. self.http=urllib3.PoolManager()
  15. if 'SSL' in self.connectionConfig:
  16. self.http = urllib3.PoolManager(\
  17. cert_file=self.connectionConfig['SSL']['user'],\
  18. cert_reqs='CERT_REQUIRED',\
  19. key_file=self.connectionConfig['SSL']['key'],\
  20. ca_certs=self.connectionConfig['SSL']['ca'])
  21. #password=self.connectionConfig['SSL']['keyPwd'],\ doesnt work until 1.25
  22. def GetLabkeyUrl(self):
  23. return self.connectionConfig['host']+"/labkey"
  24. def GetLabkeyWebdavUrl(self):
  25. return self.GetLabkeyUrl()+"/_webdav"
  26. def getBasicAuth(self):
  27. user=self.connectionConfig['labkey']['user']
  28. pwd=self.connectionConfig['labkey']['password']
  29. return user+":"+pwd
  30. def get(self,url):
  31. debug=False
  32. if debug:
  33. print("GET: {0}").format(url)
  34. print("as {0}").format(user)
  35. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  36. try:
  37. return self.http.request('GET',url,headers=headers)
  38. #f contains json as a return value
  39. except urllib3.exceptions.HTTPError as e:
  40. print(e)
  41. def post(self,url,data):
  42. debug=False
  43. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  44. headers["Content-Type"]="application/json"
  45. #add csrf;also sets self.cookie
  46. headers["X-LABKEY-CSRF"]=self.getCSRF()
  47. headers["Cookie"]=self.cookie
  48. try:
  49. return self.http.request('POST',url,headers=headers,body=data)
  50. #f contains json as a return value
  51. except urllib3.exceptions.HTTPError as e:
  52. print(e)
  53. def put(self,url,data):
  54. debug=False
  55. if debug:
  56. print("PUT: {}").format(url)
  57. headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
  58. headers["Content-Type"]="application/octet-stream"
  59. #add csrf
  60. headers["X-LABKEY-CSRF"]=self.getCSRF()
  61. headers["Cookie"]=self.cookie
  62. try:
  63. return self.http.request('PUT',url,headers=headers,body=data)
  64. #f contains json as a return value
  65. except urllib3.exceptions.HTTPError as e:
  66. print(e)
  67. def getCSRF(self):
  68. url=self.GetLabkeyUrl()+'/login/whoAmI.view'
  69. response=self.get(url)
  70. self.cookie=response.getheader('Set-Cookie')
  71. encoding=chardet.detect(response.data)['encoding']
  72. jsonData=json.loads(response.data.decode(encoding))
  73. return jsonData["CSRF"]