client.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import os
  2. import requests
  3. import logging
  4. import contextlib
  5. from xml.etree import ElementTree
  6. from requests_oauthlib import OAuth1
  7. from .models import (Object, Download, Ingest, Investigation, User,
  8. AccessPoint)
  9. from .exceptions import (Timeout, PermissionError, NotFoundError,
  10. ArbitraryError, InvalidInput)
  11. BASE_URL = 'http://kitdm.anka.kit.edu:8080/KITDM/rest'
  12. KEY = os.getenv('ASTOR_RECO_KEY') or 'secret'
  13. SECRET = os.getenv('ASTOR_RECO_SECRET') or 'secret'
  14. TOKEN = os.getenv('ASTOR_RECO_TOKEN')
  15. TOKEN_SECRET = os.getenv('ASTOR_RECO_TOKEN_SECRET')
  16. def check_response(response):
  17. if response.status_code == 403:
  18. raise PermissionError()
  19. elif response.status_code == 404:
  20. raise NotFoundError()
  21. elif response.status_code == 412:
  22. raise InvalidInput()
  23. elif response.status_code != 200:
  24. raise ArbitraryError(response)
  25. @contextlib.contextmanager
  26. def check_timeouts():
  27. try:
  28. yield
  29. except requests.exceptions.Timeout:
  30. raise Timeout()
  31. class Client(object):
  32. def __init__(self, base_url=BASE_URL, key=KEY, secret=SECRET,
  33. token=TOKEN, token_secret=TOKEN_SECRET):
  34. self.session = requests.Session()
  35. self.session.auth = OAuth1(key, client_secret=secret,
  36. resource_owner_key=token,
  37. resource_owner_secret=token_secret,
  38. signature_method='PLAINTEXT')
  39. self.base_url = base_url
  40. self.log = logging.getLogger(__name__)
  41. def url(self, *args):
  42. return '/'.join([self.base_url] + [str(arg) for arg in args])
  43. def get_ingest_ids(self, limit=None):
  44. return self.get_collection('staging/ingests', 'id', limit)
  45. def get_ingests(self, limit=-1):
  46. return [Ingest(self, oid) for oid in self.get_ingest_ids(limit)]
  47. def get_investigation_ids(self, limit=None):
  48. return self.get_collection('basemetadata/investigations',
  49. 'investigationId', limit)
  50. def get_investigations(self, limit=-1):
  51. return [Investigation(self, oid) for oid
  52. in self.get_investigation_ids(limit)]
  53. def get_object_ids(self, limit=None, since=None):
  54. return self.get_collection('basemetadata/digitalObjects',
  55. 'baseId', limit)
  56. def get_objects(self, limit=-1, predicate=lambda o: True):
  57. objects = (Object(self, oid) for oid in self.get_object_ids(limit))
  58. return [o for o in objects if predicate(o)]
  59. def create_object(self, investigation_id, uploader, label=None, note=None):
  60. url = self.url('basemetadata/investigations', investigation_id,
  61. 'digitalObjects')
  62. root = self.post(url, label=label, uploaderId=uploader.id, note=note)
  63. return Object(self, -1, root=root)
  64. def delete_object(self, obj):
  65. self.delete(self.url('basemetadata/digitalObjects', obj.id))
  66. def get_download_ids(self, limit=None):
  67. return self.get_collection('staging/downloads', 'id', limit)
  68. def get_downloads(self, limit=-1):
  69. return [Download(self, oid) for oid in self.get_download_ids(limit)]
  70. def delete_ingest(self, ingest):
  71. self.delete(self.url('staging/ingests', ingest.id))
  72. def get_accesspoint_ids(self):
  73. return self.get_collection('staging/accesspoints', 'id')
  74. def get_accesspoints(self, scheme=None):
  75. aps = (AccessPoint(self, oid) for oid in self.get_accesspoint_ids())
  76. if scheme:
  77. return [ap for ap in aps if ap.url.startswith(scheme)]
  78. return list(aps)
  79. def get_collection(self, url, id_name, limit=None):
  80. params = {'results': limit} if limit else {}
  81. root = self.get(self.url(url), **params)
  82. entities = root.findall('./entities/entity/{}'.format(id_name))
  83. return [int(e.text) for e in entities]
  84. def get_user(self, oid):
  85. return User(self, oid)
  86. def delete(self, url):
  87. self.log.debug("DELETE {}".format(url))
  88. with check_timeouts():
  89. response = self.session.delete(url, timeout=5)
  90. check_response(response)
  91. def get(self, url, **params):
  92. self.log.debug("GET {}".format(url))
  93. with check_timeouts():
  94. response = self.session.get(url, params=params, timeout=5)
  95. check_response(response)
  96. return ElementTree.fromstring(response.text)
  97. def post(self, url, **params):
  98. self.log.debug("POST {}".format(url))
  99. with check_timeouts():
  100. response = self.session.post(url, data=params, timeout=5)
  101. check_response(response)
  102. return ElementTree.fromstring(response.text)
  103. def put(self, url, **params):
  104. self.log.debug("PUT {}".format(url))
  105. with check_timeouts():
  106. response = self.session.put(url, data=params, timeout=5)
  107. check_response(response)