dspace-api-toolkit 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ # -*- coding: UTF-8 -*-
2
+ """
3
+ Custom exceptions for DSpace API client
4
+ """
5
+
6
+ class DSpaceError(Exception):
7
+ """Base exception class for DSpace API errors"""
8
+
9
+ def __init__(self, message, status_code=None, response_text=None):
10
+ super().__init__(message)
11
+ self.status_code = status_code
12
+ self.response_text = response_text
13
+
14
+ class AuthenticationError(DSpaceError):
15
+ """Exception raised for authentication-related errors"""
16
+ pass
17
+
18
+ class AuthorizationError(DSpaceError):
19
+ """Exception raised for authorization/permission errors"""
20
+ pass
21
+
22
+ class UserAgreementRequired(AuthorizationError):
23
+ """
24
+ DSpace-CRIS refused the request because the logged-in user has not
25
+ accepted the end-user agreement (403 "No user agreement accepted").
26
+
27
+ Accept it once with client.epersons.accept_end_user_agreement(...),
28
+ mark service accounts with ignore_end_user_agreement(...), or set
29
+ DSpaceConfig(accept_user_agreement=True) / DSPACE_ACCEPT_USER_AGREEMENT=true
30
+ to let the toolkit accept it for the logged-in user automatically.
31
+ """
32
+ pass
33
+
34
+ class NotFoundError(DSpaceError):
35
+ """Exception raised when a resource is not found"""
36
+ pass
37
+
38
+ class BadRequestError(DSpaceError):
39
+ """Exception raised for bad request errors (400)"""
40
+ pass
41
+
42
+ class ValidationError(DSpaceError):
43
+ """Exception raised for validation errors"""
44
+ pass
45
+
46
+ class ServerError(DSpaceError):
47
+ """Exception raised for server errors (5xx)"""
48
+ pass
49
+
50
+ class SessionError(DSpaceError):
51
+ """Exception raised for session-related errors"""
52
+ pass
53
+
54
+ def handle_response_error(response, operation_name="API operation"):
55
+ """
56
+ Handle HTTP response errors and raise appropriate exceptions
57
+
58
+ Parameters:
59
+ response: requests.Response object
60
+ operation_name: Description of the operation for error context
61
+
62
+ Raises:
63
+ Appropriate DSpaceError subclass based on status code
64
+ """
65
+ status_code = response.status_code
66
+
67
+ if status_code == 400:
68
+ raise BadRequestError(
69
+ f"{operation_name} failed: Bad Request",
70
+ status_code=status_code,
71
+ response_text=response.text
72
+ )
73
+ elif status_code == 401:
74
+ raise AuthenticationError(
75
+ f"{operation_name} failed: Unauthorized",
76
+ status_code=status_code,
77
+ response_text=response.text
78
+ )
79
+ elif status_code == 403 and "No user agreement accepted" in (response.text or ""):
80
+ raise UserAgreementRequired(
81
+ f"{operation_name} failed: Forbidden - the logged-in user has not accepted the end-user "
82
+ "agreement (DSpace-CRIS). Accept it, or set accept_user_agreement=True / "
83
+ "DSPACE_ACCEPT_USER_AGREEMENT=true",
84
+ status_code=status_code,
85
+ response_text=response.text
86
+ )
87
+ elif status_code == 403:
88
+ raise AuthorizationError(
89
+ f"{operation_name} failed: Forbidden",
90
+ status_code=status_code,
91
+ response_text=response.text
92
+ )
93
+ elif status_code == 404:
94
+ raise NotFoundError(
95
+ f"{operation_name} failed: Not Found",
96
+ status_code=status_code,
97
+ response_text=response.text
98
+ )
99
+ elif 500 <= status_code < 600:
100
+ raise ServerError(
101
+ f"{operation_name} failed: Server Error",
102
+ status_code=status_code,
103
+ response_text=response.text
104
+ )
105
+ else:
106
+ raise DSpaceError(
107
+ f"{operation_name} failed: Unexpected status {status_code}",
108
+ status_code=status_code,
109
+ response_text=response.text
110
+ )