Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" Connection to DARIAH-DE storage 

2 

3 Storage API docs: http://hdl.handle.net/11858/00-1734-0000-0009-FEA1-D 

4""" 

5import logging 

6 

7import requests 

8from django.core.exceptions import ImproperlyConfigured, ValidationError 

9from django.core.validators import URLValidator 

10 

11from discuss_data.dhrep import settings 

12from discuss_data.dhrep.token import Token 

13 

14logger = logging.getLogger(__name__) 

15 

16 

17class Storage: 

18 """ 

19 Methods to create and update data objects in DARIAH-DE storage 

20 """ 

21 

22 def __init__(self) -> None: 

23 validator = URLValidator(["https"]) 

24 try: 

25 validator(settings.STORAGE_URL) 

26 except ValidationError as e: 

27 raise ImproperlyConfigured from e 

28 

29 storage_url = settings.STORAGE_URL 

30 if not storage_url.endswith("/"): 

31 storage_url += "/" 

32 self._storage_url = storage_url 

33 

34 @property 

35 def storage_url(self) -> str: 

36 """DARIAH-DE storage API endpoint 

37 """ 

38 return self._storage_url 

39 

40 def get(self, token: Token, storage_id: str): 

41 response = requests.get( 

42 self._storage_url + storage_id, 

43 headers={"Authorization": token.token_type + " " + token.access_token}, 

44 ) 

45 

46 if response.status_code != 200: 

47 raise StorageException( 

48 "Not yet uploaded " 

49 + storage_id 

50 + ". " 

51 + str(response.status_code) 

52 + ": " 

53 + response.reason 

54 ) 

55 return response 

56 

57 def create_object(self, token: Token) -> str: 

58 """Create a new object in dariahstorage 

59 

60 :param token: authentication token 

61 :type token: Token 

62 :raises StorageException: if response from storage had no 201 status code 

63 :return: the storage id of created object 

64 :rtype: str 

65 

66 """ 

67 logger.debug(token) 

68 

69 response = requests.post( 

70 self._storage_url, 

71 headers={ 

72 "Authorization": token.token_type + " " + token.access_token, 

73 "Content-type": "text/plain", 

74 }, 

75 ) 

76 

77 if response.status_code != 201: 

78 raise StorageException( 

79 "Error creating new cdstar object: " 

80 + response.text 

81 + " - " 

82 + str(response.status_code) 

83 ) 

84 

85 storage_id = response.headers["location"].rsplit("/", 1)[-1] 

86 return storage_id 

87 

88 def update(self, token: Token, storage_id: str, content) -> None: 

89 """Update an object in dariahstorage (DEPRECATED) 

90 

91 :param token: authentication token 

92 :type token: Token 

93 :param storageid: the storage id 

94 :type storageid: str 

95 :param content: the data to store 

96 :type content: ContentFile 

97 :raises StorageException: if response from storage had no 201 status code 

98 

99 """ 

100 

101 response = requests.put( 

102 self._storage_url + storage_id, 

103 headers={ 

104 "Authorization": token.token_type + " " + token.access_token, 

105 "Content-type": content.file.content_type, 

106 }, 

107 data=content.read(), 

108 ) 

109 

110 if response.status_code != 201: 

111 raise StorageException( 

112 "Error updating cdstar object " 

113 + storage_id 

114 + ": " 

115 + response.text 

116 + " - " 

117 + str(response.status_code) 

118 ) 

119 

120 

121class StorageException(Exception): 

122 """Thrown in case of problems with the storage""" 

123 

124 

125# an instance of storage 

126# deprecated 

127storage = Storage()