-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path__init__.py
More file actions
272 lines (233 loc) · 9.16 KB
/
Copy path__init__.py
File metadata and controls
272 lines (233 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""DSpace REST API client for Python.
A comprehensive Python client for the DSpace REST API with version-aware
compatibility checking and automatic documentation management.
Key Features:
- Version-first initialization with automatic documentation fetching
- Pre-execution validation for all API operations
- Multi-version compatibility support
- Git-based documentation management with auto-updates
- Rich console output for beautiful user experience
- Batch operations with adaptive concurrency control
Example:
# DEVELOPER DECLARES: This script is compatible with DSpace 8.0 and 9.0
TARGET_VERSIONS = ["8.0", "9.0"]
SCRIPT_AUTHORS = "Bram Luyten (Atmire)"
from dspace_client import create_validated_client, ServerVersionMismatchError, show_script_attribution
async def main():
show_script_attribution(SCRIPT_AUTHORS)
base_url = input("DSpace base URL: ")
username = input("Username: ")
password = input("Password: ")
try:
auth, client = await create_validated_client(
base_url=base_url,
username=username,
password=password,
target_versions=TARGET_VERSIONS,
)
community = await client.create_community("My Community")
except ServerVersionMismatchError as e:
print(f"Cannot connect: {e}")
print(f"This script only works with DSpace versions: {', '.join(TARGET_VERSIONS)}")
"""
import logging
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import List, Optional, Tuple, Union
import httpx
from .attribution import show_script_attribution
from .auth import DSpaceAuthClient
from .batch import BatchItemCreator
from .concurrency import ConcurrencyConfig, ConcurrencyController
from .core import DSpaceClient
from .oai import OAIClient
from .promo import (
is_atmire_promo_disabled,
show_atmire_promo_end,
show_atmire_promo_start,
)
from .rest_pdf_cache import RestPDFCountCache
logging.getLogger("dspace_client").addHandler(logging.NullHandler())
async def create_validated_client(
base_url: str,
username: str,
password: str,
target_versions: str | list[str] = "bleeding-edge",
*,
show_atmire_promo: bool = False,
fetch_docs: bool = False,
**client_kwargs
) -> tuple[DSpaceAuthClient, DSpaceClient]:
"""
Authenticate and create DSpaceClient with automatic version validation.
This helper function performs the complete flow:
1. Authenticate with DSpace server
2. Create DSpaceClient with specified target_versions
3. Verify server version compatibility
4. Raise ServerVersionMismatchError if major version mismatch
5. Print warnings for minor version differences
6. Optional Atmire thank-you panel when the session ends (disable with DSPACE_CLIENT_DISABLE_ATMIRE_PROMO=1)
Args:
base_url: DSpace server base URL
username: Username for authentication
password: Password for authentication
target_versions: DSpace version(s) that the client should be compatible with.
Can be a single string (e.g., "9.0") or list (e.g., ["8.0", "9.0"]).
Defaults to "bleeding-edge".
**client_kwargs: Additional keyword arguments passed to DSpaceClient constructor
(timeout, max_retries, courtesy_delay, etc.)
show_atmire_promo: When True, show optional Atmire thank-you panels at session start/end.
fetch_docs: When True, fetch RestContract documentation for each target version.
Returns:
Tuple of (auth_client, dspace_client)
Raises:
AuthenticationError: If authentication fails
ServerVersionMismatchError: If server version major version doesn't match target_versions
Example:
from dspace_client import create_validated_client
auth, client = await create_validated_client(
base_url="https://demo.dspace.org",
username="admin@example.com",
password="password",
target_versions=["8.0", "9.0"]
)
# Server version will be validated automatically
# If major version mismatch, ServerVersionMismatchError is raised
# If minor version difference, warning is printed but connection proceeds
"""
timeout = client_kwargs.pop("timeout", 30.0)
auth = DSpaceAuthClient(base_url, timeout=timeout)
jwt, status = await auth.authenticate(username, password)
client = DSpaceClient(
base_url=base_url,
jwt_token=jwt,
csrf_token=auth.csrf_token,
http_client=auth.client,
target_versions=target_versions,
timeout=timeout,
**client_kwargs,
)
await client.verify_server_version(raise_on_mismatch=True)
if fetch_docs:
versions = [target_versions] if isinstance(target_versions, str) else list(target_versions)
for version in versions:
await client.docs_fetcher.fetch_version(version)
if show_atmire_promo:
auth.show_atmire_promo = True
show_atmire_promo_start()
return auth, client
@asynccontextmanager
async def managed_client(
base_url: str,
username: str,
password: str,
target_versions: str | list[str] = "bleeding-edge",
**client_kwargs,
) -> AsyncIterator[tuple[DSpaceAuthClient, DSpaceClient]]:
"""Authenticate, validate, and guarantee ``auth.close()`` in a ``finally`` block."""
auth: DSpaceAuthClient | None = None
try:
auth, client = await create_validated_client(
base_url=base_url,
username=username,
password=password,
target_versions=target_versions,
**client_kwargs,
)
yield auth, client
finally:
if auth is not None:
await auth.close()
async def create_anonymous_client(
base_url: str,
target_versions: str | list[str] = "bleeding-edge",
**client_kwargs,
) -> tuple[httpx.AsyncClient, DSpaceClient]:
"""Build a DSpaceClient for anonymous, read-only access.
This is the read-only sibling of :func:`create_validated_client`. It
creates a fresh ``httpx.AsyncClient`` (no authentication, no cookies),
instantiates :class:`DSpaceClient` with ``jwt_token=None`` and
``csrf_token=None``, and verifies the server version against
``target_versions``.
Mutating operations (POST/PUT/PATCH/DELETE) raise
:class:`AuthenticationError` before any request is dispatched, so this
client is safe to hand to scripts that only read public data.
The caller owns the returned ``httpx.AsyncClient`` and must close it,
typically via ``await http.aclose()`` in a ``finally`` block (mirrors
``await auth.close()`` in the authenticated path).
Args:
base_url: DSpace server base URL.
target_versions: DSpace version(s) the client should be compatible
with. See :func:`create_validated_client` for value semantics.
**client_kwargs: Additional keyword arguments forwarded to
:class:`DSpaceClient` (e.g. ``timeout``, ``courtesy_delay``,
``slow_request_threshold_seconds``, ``slow_request_callback``).
Returns:
Tuple of (httpx.AsyncClient, DSpaceClient).
Raises:
ServerVersionMismatchError: If the server's major version does not
match ``target_versions``.
Example:
from dspace_client import create_anonymous_client
http, client = await create_anonymous_client(
base_url="https://demo.dspace.org",
target_versions=["7.6", "8.0", "9.0"],
)
try:
results = await client.search_items(query="dc.type:\"Journal article\"")
finally:
await http.aclose()
"""
timeout = client_kwargs.pop("timeout", 30.0)
http = httpx.AsyncClient(timeout=timeout, follow_redirects=True)
client = DSpaceClient(
base_url=base_url,
jwt_token=None,
csrf_token=None,
http_client=http,
target_versions=target_versions,
**client_kwargs,
)
await client.verify_server_version(raise_on_mismatch=True)
return http, client
from .credentials import prompt_and_authenticate
from .exceptions import (
AuthenticationError,
DSpaceAPIError,
DSpaceClientError,
OAIError,
ServerVersionMismatchError,
VersionIncompatibilityError,
)
__version__ = "0.1.0"
__author__ = "Bram Luyten"
__email__ = "bram@atmire.com"
__all__ = [
# Main client classes
"DSpaceAuthClient",
"DSpaceClient",
"BatchItemCreator",
"OAIClient",
"RestPDFCountCache",
# Concurrency control
"ConcurrencyController",
"ConcurrencyConfig",
# Exceptions
"DSpaceClientError",
"AuthenticationError",
"DSpaceAPIError",
"VersionIncompatibilityError",
"ServerVersionMismatchError",
"OAIError",
# Helper functions
"create_validated_client",
"create_anonymous_client",
"managed_client",
"prompt_and_authenticate",
# Script attribution
"show_script_attribution",
# Optional Atmire promo (also controlled by DSPACE_CLIENT_DISABLE_ATMIRE_PROMO)
"show_atmire_promo_start",
"show_atmire_promo_end",
"is_atmire_promo_disabled",
]