glpi-python-client 0.6.9__py3-none-any.whl → 0.6.12__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.
- glpi_python_client/__init__.py +15 -1
- glpi_python_client/_async/_retry.py +90 -0
- glpi_python_client/_async/auth/_v1_session.py +70 -28
- glpi_python_client/_async/auth/auth.py +41 -18
- glpi_python_client/_async/clients/_base_client.py +24 -1
- glpi_python_client/_async/clients/api/__init__.py +4 -0
- glpi_python_client/_async/clients/api/assistance/__init__.py +4 -1
- glpi_python_client/_async/clients/api/assistance/_pending_reason.py +336 -0
- glpi_python_client/_async/clients/api/dropdowns/__init__.py +4 -1
- glpi_python_client/_async/clients/api/dropdowns/_itil_category.py +233 -0
- glpi_python_client/_async/clients/client.py +4 -0
- glpi_python_client/_async/clients/commons/_config.py +42 -0
- glpi_python_client/_async/clients/commons/_constants.py +4 -0
- glpi_python_client/_async/clients/commons/_http.py +63 -2
- glpi_python_client/_async/clients/commons/_transport.py +13 -10
- glpi_python_client/_errors.py +87 -2
- glpi_python_client/_sync/_retry.py +90 -0
- glpi_python_client/_sync/auth/_v1_session.py +70 -28
- glpi_python_client/_sync/auth/auth.py +41 -18
- glpi_python_client/_sync/clients/_base_client.py +24 -1
- glpi_python_client/_sync/clients/api/__init__.py +4 -0
- glpi_python_client/_sync/clients/api/assistance/__init__.py +4 -1
- glpi_python_client/_sync/clients/api/assistance/_pending_reason.py +336 -0
- glpi_python_client/_sync/clients/api/dropdowns/__init__.py +4 -1
- glpi_python_client/_sync/clients/api/dropdowns/_itil_category.py +233 -0
- glpi_python_client/_sync/clients/client.py +4 -0
- glpi_python_client/_sync/clients/commons/_config.py +42 -0
- glpi_python_client/_sync/clients/commons/_constants.py +4 -0
- glpi_python_client/_sync/clients/commons/_http.py +63 -2
- glpi_python_client/_sync/clients/commons/_transport.py +13 -10
- glpi_python_client/models/__init__.py +12 -0
- glpi_python_client/models/api_schema/assistance/__init__.py +6 -0
- glpi_python_client/models/api_schema/assistance/_pending_reason.py +102 -0
- glpi_python_client/models/api_schema/dropdowns/__init__.py +10 -0
- glpi_python_client/models/api_schema/dropdowns/_itil_category.py +199 -0
- {glpi_python_client-0.6.9.dist-info → glpi_python_client-0.6.12.dist-info}/METADATA +1 -1
- {glpi_python_client-0.6.9.dist-info → glpi_python_client-0.6.12.dist-info}/RECORD +39 -31
- {glpi_python_client-0.6.9.dist-info → glpi_python_client-0.6.12.dist-info}/WHEEL +0 -0
- {glpi_python_client-0.6.9.dist-info → glpi_python_client-0.6.12.dist-info}/licenses/LICENSE +0 -0
glpi_python_client/__init__.py
CHANGED
|
@@ -24,6 +24,7 @@ from glpi_python_client._errors import (
|
|
|
24
24
|
GlpiError,
|
|
25
25
|
GlpiNotFoundError,
|
|
26
26
|
GlpiProtocolError,
|
|
27
|
+
GlpiResponseBodyError,
|
|
27
28
|
GlpiServerError,
|
|
28
29
|
GlpiStatusError,
|
|
29
30
|
GlpiTimeoutError,
|
|
@@ -41,6 +42,7 @@ from glpi_python_client.models import (
|
|
|
41
42
|
DeleteEntity,
|
|
42
43
|
DeleteFollowup,
|
|
43
44
|
DeleteGroup,
|
|
45
|
+
DeleteITILCategory,
|
|
44
46
|
DeleteKBArticle,
|
|
45
47
|
DeleteKBArticleComment,
|
|
46
48
|
DeleteKBCategory,
|
|
@@ -62,6 +64,7 @@ from glpi_python_client.models import (
|
|
|
62
64
|
GetEntity,
|
|
63
65
|
GetFollowup,
|
|
64
66
|
GetGroup,
|
|
67
|
+
GetITILCategory,
|
|
65
68
|
GetKBArticle,
|
|
66
69
|
GetKBArticleComment,
|
|
67
70
|
GetKBArticleRevision,
|
|
@@ -99,6 +102,7 @@ from glpi_python_client.models import (
|
|
|
99
102
|
PatchEntity,
|
|
100
103
|
PatchFollowup,
|
|
101
104
|
PatchGroup,
|
|
105
|
+
PatchITILCategory,
|
|
102
106
|
PatchKBArticle,
|
|
103
107
|
PatchKBArticleComment,
|
|
104
108
|
PatchKBCategory,
|
|
@@ -109,6 +113,8 @@ from glpi_python_client.models import (
|
|
|
109
113
|
PatchTicketTask,
|
|
110
114
|
PatchTimelineDocument,
|
|
111
115
|
PatchUser,
|
|
116
|
+
PendingReason,
|
|
117
|
+
PendingReasonItem,
|
|
112
118
|
PostComputer,
|
|
113
119
|
PostContract,
|
|
114
120
|
PostContractCost,
|
|
@@ -118,6 +124,7 @@ from glpi_python_client.models import (
|
|
|
118
124
|
PostEntity,
|
|
119
125
|
PostFollowup,
|
|
120
126
|
PostGroup,
|
|
127
|
+
PostITILCategory,
|
|
121
128
|
PostKBArticle,
|
|
122
129
|
PostKBArticleComment,
|
|
123
130
|
PostKBCategory,
|
|
@@ -138,7 +145,7 @@ from glpi_python_client.rsql import (
|
|
|
138
145
|
date_window,
|
|
139
146
|
)
|
|
140
147
|
|
|
141
|
-
__version__ = "0.6.
|
|
148
|
+
__version__ = "0.6.12"
|
|
142
149
|
|
|
143
150
|
__all__ = [
|
|
144
151
|
"AsyncGlpiClient",
|
|
@@ -151,6 +158,7 @@ __all__ = [
|
|
|
151
158
|
"DeleteEntity",
|
|
152
159
|
"DeleteFollowup",
|
|
153
160
|
"DeleteGroup",
|
|
161
|
+
"DeleteITILCategory",
|
|
154
162
|
"DeleteKBArticle",
|
|
155
163
|
"DeleteKBArticleComment",
|
|
156
164
|
"DeleteKBCategory",
|
|
@@ -172,6 +180,7 @@ __all__ = [
|
|
|
172
180
|
"GetEntity",
|
|
173
181
|
"GetFollowup",
|
|
174
182
|
"GetGroup",
|
|
183
|
+
"GetITILCategory",
|
|
175
184
|
"GetKBArticle",
|
|
176
185
|
"GetKBArticleComment",
|
|
177
186
|
"GetKBArticleRevision",
|
|
@@ -196,6 +205,7 @@ __all__ = [
|
|
|
196
205
|
"GlpiNotFoundError",
|
|
197
206
|
"GlpiPriority",
|
|
198
207
|
"GlpiProtocolError",
|
|
208
|
+
"GlpiResponseBodyError",
|
|
199
209
|
"GlpiServerError",
|
|
200
210
|
"GlpiSolutionStatus",
|
|
201
211
|
"GlpiStatusError",
|
|
@@ -220,6 +230,7 @@ __all__ = [
|
|
|
220
230
|
"PatchEntity",
|
|
221
231
|
"PatchFollowup",
|
|
222
232
|
"PatchGroup",
|
|
233
|
+
"PatchITILCategory",
|
|
223
234
|
"PatchKBArticle",
|
|
224
235
|
"PatchKBArticleComment",
|
|
225
236
|
"PatchKBCategory",
|
|
@@ -230,6 +241,8 @@ __all__ = [
|
|
|
230
241
|
"PatchTicketTask",
|
|
231
242
|
"PatchTimelineDocument",
|
|
232
243
|
"PatchUser",
|
|
244
|
+
"PendingReason",
|
|
245
|
+
"PendingReasonItem",
|
|
233
246
|
"PostComputer",
|
|
234
247
|
"PostContract",
|
|
235
248
|
"PostContractCost",
|
|
@@ -239,6 +252,7 @@ __all__ = [
|
|
|
239
252
|
"PostEntity",
|
|
240
253
|
"PostFollowup",
|
|
241
254
|
"PostGroup",
|
|
255
|
+
"PostITILCategory",
|
|
242
256
|
"PostKBArticle",
|
|
243
257
|
"PostKBArticleComment",
|
|
244
258
|
"PostKBCategory",
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""Configurable retry policy shared by the v2 transport, v1 session and OAuth.
|
|
2
|
+
|
|
3
|
+
Every network-facing helper of the client retries a transient failure. The
|
|
4
|
+
policy is a per-client setting, not a decorator fixed at import time, so the
|
|
5
|
+
decorator built here reads two attributes from the object it decorates:
|
|
6
|
+
|
|
7
|
+
``_max_retries``
|
|
8
|
+
Retries after the first attempt. ``0`` disables retrying.
|
|
9
|
+
``_retry_wait_seconds``
|
|
10
|
+
Fixed pause between attempts; ``None`` selects
|
|
11
|
+
:data:`DEFAULT_RETRY_WAIT_SECONDS`.
|
|
12
|
+
|
|
13
|
+
Which errors are retried is decided by each call site (``retry_on``), not by
|
|
14
|
+
the setting.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import functools
|
|
20
|
+
from collections.abc import Callable
|
|
21
|
+
from typing import Any, TypeVar, cast
|
|
22
|
+
|
|
23
|
+
from tenacity import (
|
|
24
|
+
AsyncRetrying,
|
|
25
|
+
retry_if_exception_type,
|
|
26
|
+
stop_after_attempt,
|
|
27
|
+
wait_fixed,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
from glpi_python_client._errors import GlpiValidationError
|
|
31
|
+
|
|
32
|
+
#: Retries after the first attempt (3 attempts in total).
|
|
33
|
+
DEFAULT_MAX_RETRIES = 2
|
|
34
|
+
|
|
35
|
+
#: Seconds between two attempts when no wait is configured.
|
|
36
|
+
DEFAULT_RETRY_WAIT_SECONDS = 3.0
|
|
37
|
+
|
|
38
|
+
F = TypeVar("F", bound=Callable[..., Any])
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def validate_retry_settings(
|
|
42
|
+
max_retries: object, retry_wait_seconds: object
|
|
43
|
+
) -> tuple[int, float | None]:
|
|
44
|
+
"""Return the validated ``(max_retries, retry_wait_seconds)`` pair.
|
|
45
|
+
|
|
46
|
+
Raises
|
|
47
|
+
------
|
|
48
|
+
GlpiValidationError
|
|
49
|
+
If ``max_retries`` is not an integer >= 0, or ``retry_wait_seconds``
|
|
50
|
+
is neither ``None`` nor a number >= 0.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
if (
|
|
54
|
+
isinstance(max_retries, bool)
|
|
55
|
+
or not isinstance(max_retries, int)
|
|
56
|
+
or max_retries < 0
|
|
57
|
+
):
|
|
58
|
+
raise GlpiValidationError("max_retries must be an integer >= 0")
|
|
59
|
+
if retry_wait_seconds is not None and (
|
|
60
|
+
isinstance(retry_wait_seconds, bool)
|
|
61
|
+
or not isinstance(retry_wait_seconds, int | float)
|
|
62
|
+
or retry_wait_seconds < 0
|
|
63
|
+
):
|
|
64
|
+
raise GlpiValidationError("retry_wait_seconds must be a number >= 0 or None")
|
|
65
|
+
return max_retries, retry_wait_seconds
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def retried(
|
|
69
|
+
retry_on: type[BaseException] | tuple[type[BaseException], ...],
|
|
70
|
+
) -> Callable[[F], F]:
|
|
71
|
+
"""Retry the decorated method on ``retry_on`` per its instance's policy.
|
|
72
|
+
|
|
73
|
+
The final error is re-raised as-is, never as ``tenacity.RetryError``.
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
def decorator(func: F) -> F:
|
|
77
|
+
@functools.wraps(func)
|
|
78
|
+
async def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
|
|
79
|
+
wait = self._retry_wait_seconds
|
|
80
|
+
retrying = AsyncRetrying(
|
|
81
|
+
retry=retry_if_exception_type(retry_on),
|
|
82
|
+
stop=stop_after_attempt(self._max_retries + 1),
|
|
83
|
+
wait=wait_fixed(DEFAULT_RETRY_WAIT_SECONDS if wait is None else wait),
|
|
84
|
+
reraise=True,
|
|
85
|
+
)
|
|
86
|
+
return await retrying(func, self, *args, **kwargs)
|
|
87
|
+
|
|
88
|
+
return cast(F, wrapper)
|
|
89
|
+
|
|
90
|
+
return decorator
|
|
@@ -13,11 +13,16 @@ The session wrapper owns the authenticated v1 lifecycle (init, refresh,
|
|
|
13
13
|
kill) and exposes the typed ``upload_document`` helper plus the generic
|
|
14
14
|
``request_json`` JSON-only HTTP helper that newer mixins build on.
|
|
15
15
|
|
|
16
|
+
Session renewal is serialised by a lock and the need is re-checked under it,
|
|
17
|
+
so concurrent requests share one new session instead of each renewing.
|
|
18
|
+
|
|
16
19
|
Retry policy
|
|
17
20
|
------------
|
|
18
21
|
Every public dispatch helper (``_init_session``, ``request_json``,
|
|
19
22
|
``upload_document``) carries the same :mod:`tenacity` retry decorator
|
|
20
|
-
used by the v2 transport: three attempts spaced by three
|
|
23
|
+
used by the v2 transport: by default three attempts spaced by three
|
|
24
|
+
seconds (the ``max_retries`` and ``retry_wait_seconds`` client settings,
|
|
25
|
+
``max_retries=0`` disables retrying),
|
|
21
26
|
triggered by :class:`~glpi_python_client.GlpiTransportError` (network
|
|
22
27
|
faults) and
|
|
23
28
|
:class:`~glpi_python_client.GlpiServerError` (which
|
|
@@ -41,10 +46,16 @@ from datetime import UTC, datetime, timedelta
|
|
|
41
46
|
from typing import Any, cast
|
|
42
47
|
|
|
43
48
|
import httpx
|
|
44
|
-
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_fixed
|
|
45
49
|
|
|
50
|
+
from glpi_python_client._async._concurrency import Lock
|
|
51
|
+
from glpi_python_client._async._retry import (
|
|
52
|
+
DEFAULT_MAX_RETRIES,
|
|
53
|
+
retried,
|
|
54
|
+
validate_retry_settings,
|
|
55
|
+
)
|
|
46
56
|
from glpi_python_client._async.clients.commons._config import build_http_session
|
|
47
57
|
from glpi_python_client._async.clients.commons._http import (
|
|
58
|
+
decode_json_body,
|
|
48
59
|
ensure_response_status,
|
|
49
60
|
finalize_request_response,
|
|
50
61
|
response_json_or_empty,
|
|
@@ -66,12 +77,7 @@ _AUTH_FAILURE_STATUS_CODES = frozenset({401, 403})
|
|
|
66
77
|
#: Mirrors the v2 transport policy deliberately: naming the HTTP library's
|
|
67
78
|
#: own exception base here would make the retries stop matching — silently —
|
|
68
79
|
#: the next time the transport changes.
|
|
69
|
-
_RETRY_ON_NETWORK_ERRORS =
|
|
70
|
-
retry=retry_if_exception_type((GlpiTransportError, GlpiServerError)),
|
|
71
|
-
stop=stop_after_attempt(3),
|
|
72
|
-
wait=wait_fixed(3),
|
|
73
|
-
reraise=True,
|
|
74
|
-
)
|
|
80
|
+
_RETRY_ON_NETWORK_ERRORS = retried((GlpiTransportError, GlpiServerError))
|
|
75
81
|
|
|
76
82
|
|
|
77
83
|
class GLPIV1Session:
|
|
@@ -92,10 +98,15 @@ class GLPIV1Session:
|
|
|
92
98
|
session_refresh_interval_seconds: int = (
|
|
93
99
|
_DEFAULT_SESSION_REFRESH_INTERVAL_SECONDS
|
|
94
100
|
),
|
|
101
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
102
|
+
retry_wait_seconds: float | None = None,
|
|
95
103
|
) -> None:
|
|
96
104
|
self._base_url = base_url.rstrip("/")
|
|
97
105
|
self._user_token = user_token
|
|
98
106
|
self._app_token = app_token
|
|
107
|
+
self._max_retries, self._retry_wait_seconds = validate_retry_settings(
|
|
108
|
+
max_retries, retry_wait_seconds
|
|
109
|
+
)
|
|
99
110
|
if session_refresh_interval_seconds < 1:
|
|
100
111
|
raise GlpiValidationError(
|
|
101
112
|
"session_refresh_interval_seconds must be a positive integer"
|
|
@@ -111,6 +122,11 @@ class GLPIV1Session:
|
|
|
111
122
|
|
|
112
123
|
self._session_token: str | None = None
|
|
113
124
|
self._session_started_at: datetime | None = None
|
|
125
|
+
# Serialises every acquisition or replacement of the session token.
|
|
126
|
+
# It is not re-entrant: only ``_ensure_session``,
|
|
127
|
+
# ``_renew_rejected_session`` and ``close`` take it, and none of them
|
|
128
|
+
# calls another of them (or ``_headers``) while holding it.
|
|
129
|
+
self._session_lock = Lock()
|
|
114
130
|
|
|
115
131
|
async def _dispatch(self, method: str, url: str, **kwargs: Any) -> httpx.Response:
|
|
116
132
|
"""Send one raw v1 HTTP call, translating transport faults.
|
|
@@ -165,7 +181,8 @@ class GLPIV1Session:
|
|
|
165
181
|
failure_message="GLPI v1 initSession failed",
|
|
166
182
|
)
|
|
167
183
|
|
|
168
|
-
|
|
184
|
+
body = decode_json_body(response)
|
|
185
|
+
token = body.get("session_token") if isinstance(body, dict) else None
|
|
169
186
|
if not token:
|
|
170
187
|
raise GlpiProtocolError("GLPI v1 initSession returned no session_token")
|
|
171
188
|
|
|
@@ -178,13 +195,32 @@ class GLPIV1Session:
|
|
|
178
195
|
|
|
179
196
|
The helper is called by every authenticated request before any header
|
|
180
197
|
construction so callers never have to manage the lifecycle directly.
|
|
198
|
+
Concurrent callers are serialised and the need is re-tested under the
|
|
199
|
+
lock, so a caller that waited reuses the token the first one obtained.
|
|
181
200
|
"""
|
|
182
201
|
|
|
183
|
-
if self._session_token is None:
|
|
184
|
-
await self._init_session()
|
|
202
|
+
if self._session_token is not None and not self._is_session_stale():
|
|
185
203
|
return
|
|
186
|
-
|
|
187
|
-
|
|
204
|
+
async with self._session_lock:
|
|
205
|
+
if self._session_token is None:
|
|
206
|
+
await self._init_session()
|
|
207
|
+
elif self._is_session_stale():
|
|
208
|
+
logger.info(
|
|
209
|
+
"GLPI v1 session reached refresh interval; renewing session."
|
|
210
|
+
)
|
|
211
|
+
await self._renew_session()
|
|
212
|
+
|
|
213
|
+
async def _renew_rejected_session(self, rejected_token: str | None) -> None:
|
|
214
|
+
"""Renew the session after the server rejected ``rejected_token``.
|
|
215
|
+
|
|
216
|
+
Under the lock the current token is compared with the rejected one:
|
|
217
|
+
when another request already replaced it, the new token is reused
|
|
218
|
+
and nothing is killed or opened.
|
|
219
|
+
"""
|
|
220
|
+
|
|
221
|
+
async with self._session_lock:
|
|
222
|
+
if self._session_token != rejected_token:
|
|
223
|
+
return
|
|
188
224
|
await self._renew_session()
|
|
189
225
|
|
|
190
226
|
def _is_session_stale(self) -> bool:
|
|
@@ -218,6 +254,8 @@ class GLPIV1Session:
|
|
|
218
254
|
async def _renew_session(self) -> None:
|
|
219
255
|
"""Drop the current GLPI v1 session token and acquire a new one.
|
|
220
256
|
|
|
257
|
+
The caller must hold ``_session_lock``; the helper never takes it.
|
|
258
|
+
|
|
221
259
|
The previous token is best-effort killed so the GLPI server can release
|
|
222
260
|
the associated session state immediately. ``_init_session`` will set
|
|
223
261
|
the new token on success or raise, leaving the existing state
|
|
@@ -267,7 +305,8 @@ class GLPIV1Session:
|
|
|
267
305
|
the caller to validate with :func:`ensure_response_status`.
|
|
268
306
|
"""
|
|
269
307
|
|
|
270
|
-
|
|
308
|
+
session_headers = await self._headers()
|
|
309
|
+
request_headers = {**session_headers, **(headers or {})}
|
|
271
310
|
# Dispatch through ``request(method, ...)`` rather than looking up a
|
|
272
311
|
# per-verb attribute: it is the one call shape both transports share,
|
|
273
312
|
# and it keeps the verb a value instead of an attribute name.
|
|
@@ -278,7 +317,7 @@ class GLPIV1Session:
|
|
|
278
317
|
"GLPI v1 session token was rejected; refreshing session and "
|
|
279
318
|
"retrying request once."
|
|
280
319
|
)
|
|
281
|
-
await self.
|
|
320
|
+
await self._renew_rejected_session(session_headers["Session-Token"])
|
|
282
321
|
request_headers = {**await self._headers(), **(headers or {})}
|
|
283
322
|
response = await self._dispatch(
|
|
284
323
|
verb, url, headers=request_headers, **kwargs
|
|
@@ -299,19 +338,22 @@ class GLPIV1Session:
|
|
|
299
338
|
"""
|
|
300
339
|
|
|
301
340
|
try:
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
341
|
+
async with self._session_lock:
|
|
342
|
+
try:
|
|
343
|
+
if self._session_token is not None:
|
|
344
|
+
await self._dispatch(
|
|
345
|
+
"GET",
|
|
346
|
+
f"{self._base_url}/killSession",
|
|
347
|
+
headers=self._session_headers(),
|
|
348
|
+
timeout=10,
|
|
349
|
+
)
|
|
350
|
+
logger.info("GLPI v1 session killed.")
|
|
351
|
+
except Exception:
|
|
352
|
+
logger.warning("Failed to kill GLPI v1 session.", exc_info=True)
|
|
353
|
+
finally:
|
|
354
|
+
self._session_token = None
|
|
355
|
+
self._session_started_at = None
|
|
312
356
|
finally:
|
|
313
|
-
self._session_token = None
|
|
314
|
-
self._session_started_at = None
|
|
315
357
|
await self._http.aclose()
|
|
316
358
|
|
|
317
359
|
@_RETRY_ON_NETWORK_ERRORS
|
|
@@ -441,7 +483,7 @@ class GLPIV1Session:
|
|
|
441
483
|
success_statuses=(200, 201),
|
|
442
484
|
failure_message="GLPI v1 document upload failed",
|
|
443
485
|
)
|
|
444
|
-
payload = response
|
|
486
|
+
payload = decode_json_body(response)
|
|
445
487
|
if not isinstance(payload, dict):
|
|
446
488
|
raise GlpiProtocolError(
|
|
447
489
|
"GLPI v1 document upload returned unexpected payload: "
|
|
@@ -9,12 +9,20 @@ from __future__ import annotations
|
|
|
9
9
|
|
|
10
10
|
import logging
|
|
11
11
|
from datetime import UTC, datetime, timedelta
|
|
12
|
+
from typing import cast
|
|
12
13
|
|
|
13
14
|
import httpx
|
|
14
|
-
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_fixed
|
|
15
15
|
|
|
16
|
+
from glpi_python_client._async._retry import (
|
|
17
|
+
DEFAULT_MAX_RETRIES,
|
|
18
|
+
retried,
|
|
19
|
+
validate_retry_settings,
|
|
20
|
+
)
|
|
16
21
|
from glpi_python_client._async.clients.commons._config import build_http_session
|
|
17
|
-
from glpi_python_client._async.clients.commons._http import
|
|
22
|
+
from glpi_python_client._async.clients.commons._http import (
|
|
23
|
+
decode_json_body,
|
|
24
|
+
transport_error_from,
|
|
25
|
+
)
|
|
18
26
|
from glpi_python_client._errors import (
|
|
19
27
|
GlpiServerError,
|
|
20
28
|
GlpiTransportError,
|
|
@@ -114,6 +122,12 @@ class GLPITokenManager:
|
|
|
114
122
|
auth_token_refresh : int | None, optional
|
|
115
123
|
Maximum token age in seconds before a refresh is attempted. ``None``
|
|
116
124
|
disables interval-based refreshes.
|
|
125
|
+
max_retries : int, optional
|
|
126
|
+
Retries after the first attempt of a failed token request
|
|
127
|
+
(network fault or 5xx); ``0`` disables retrying. Defaults to 2,
|
|
128
|
+
that is 3 attempts in total.
|
|
129
|
+
retry_wait_seconds : float | None, optional
|
|
130
|
+
Pause between two attempts; ``None`` keeps the 3-second default.
|
|
117
131
|
"""
|
|
118
132
|
|
|
119
133
|
def __init__(
|
|
@@ -125,7 +139,12 @@ class GLPITokenManager:
|
|
|
125
139
|
password: str | None = None,
|
|
126
140
|
session: httpx.AsyncClient | None = None,
|
|
127
141
|
auth_token_refresh: int | None = None,
|
|
142
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
143
|
+
retry_wait_seconds: float | None = None,
|
|
128
144
|
) -> None:
|
|
145
|
+
self._max_retries, self._retry_wait_seconds = validate_retry_settings(
|
|
146
|
+
max_retries, retry_wait_seconds
|
|
147
|
+
)
|
|
129
148
|
self._token_url = token_url
|
|
130
149
|
self._client_id = client_id
|
|
131
150
|
self._client_secret = client_secret
|
|
@@ -218,6 +237,18 @@ class GLPITokenManager:
|
|
|
218
237
|
|
|
219
238
|
return data
|
|
220
239
|
|
|
240
|
+
def _decode_token_body(self, response: httpx.Response) -> dict[str, object]:
|
|
241
|
+
"""Decode a successful token response, raising a library error if not JSON.
|
|
242
|
+
|
|
243
|
+
Raises
|
|
244
|
+
------
|
|
245
|
+
GlpiResponseBodyError
|
|
246
|
+
When the 2xx body is not JSON. Not retried: the endpoint answered.
|
|
247
|
+
"""
|
|
248
|
+
|
|
249
|
+
payload = decode_json_body(response, url=self._token_url)
|
|
250
|
+
return cast(dict[str, object], payload)
|
|
251
|
+
|
|
221
252
|
def _store_token_data(
|
|
222
253
|
self, token_data: dict[str, object], label: str = "acquired"
|
|
223
254
|
) -> None:
|
|
@@ -284,12 +315,7 @@ class GLPITokenManager:
|
|
|
284
315
|
return False
|
|
285
316
|
return now >= self.token_updated_at + self._auth_token_refresh_interval
|
|
286
317
|
|
|
287
|
-
@
|
|
288
|
-
retry=retry_if_exception_type((GlpiTransportError, GlpiServerError)),
|
|
289
|
-
stop=stop_after_attempt(3),
|
|
290
|
-
wait=wait_fixed(3),
|
|
291
|
-
reraise=True,
|
|
292
|
-
)
|
|
318
|
+
@retried((GlpiTransportError, GlpiServerError))
|
|
293
319
|
async def _acquire_token(self) -> None:
|
|
294
320
|
"""Acquire an OAuth2 access token using the configured auth flow.
|
|
295
321
|
|
|
@@ -303,7 +329,8 @@ class GLPITokenManager:
|
|
|
303
329
|
GlpiAuthError
|
|
304
330
|
If GLPI rejects the credentials (401/403). Not retried.
|
|
305
331
|
GlpiServerError
|
|
306
|
-
If the token endpoint fails (5xx). Retried up to
|
|
332
|
+
If the token endpoint fails (5xx). Retried up to ``max_retries`` times
|
|
333
|
+
(3 attempts by default).
|
|
307
334
|
GlpiStatusError
|
|
308
335
|
If the token endpoint returns any other unexpected status. Not
|
|
309
336
|
retried.
|
|
@@ -312,7 +339,7 @@ class GLPITokenManager:
|
|
|
312
339
|
data = self._build_token_request_data()
|
|
313
340
|
response = await self._post_token_request(data)
|
|
314
341
|
if 200 <= response.status_code < 300:
|
|
315
|
-
self._store_token_data(
|
|
342
|
+
self._store_token_data(self._decode_token_body(response))
|
|
316
343
|
return
|
|
317
344
|
try:
|
|
318
345
|
error_detail = response.json()
|
|
@@ -326,12 +353,7 @@ class GLPITokenManager:
|
|
|
326
353
|
response_text=str(error_detail),
|
|
327
354
|
)
|
|
328
355
|
|
|
329
|
-
@
|
|
330
|
-
retry=retry_if_exception_type(GlpiTransportError),
|
|
331
|
-
stop=stop_after_attempt(3),
|
|
332
|
-
wait=wait_fixed(3),
|
|
333
|
-
reraise=True,
|
|
334
|
-
)
|
|
356
|
+
@retried(GlpiTransportError)
|
|
335
357
|
async def _refresh_access_token(self) -> None:
|
|
336
358
|
"""Refresh the OAuth2 access token using the stored refresh token.
|
|
337
359
|
|
|
@@ -356,7 +378,8 @@ class GLPITokenManager:
|
|
|
356
378
|
``GlpiServerError``, so it does not retry the fall-through to
|
|
357
379
|
:meth:`_acquire_token`. The nested call carries its own
|
|
358
380
|
independent decorator, which does retry ``GlpiServerError`` up
|
|
359
|
-
to 3
|
|
381
|
+
to ``max_retries`` + 1 attempts (3 by default). A persistent 5xx
|
|
382
|
+
therefore costs (with the default setting) exactly 1
|
|
360
383
|
refresh POST + 3 nested acquire POSTs = 4 POST requests, not the
|
|
361
384
|
12 an earlier, less precise predicate produced by retrying the
|
|
362
385
|
already-retried nested failure a second time.
|
|
@@ -381,7 +404,7 @@ class GLPITokenManager:
|
|
|
381
404
|
data["client_secret"] = self._client_secret
|
|
382
405
|
response = await self._post_token_request(data)
|
|
383
406
|
if 200 <= response.status_code < 300:
|
|
384
|
-
self._store_token_data(
|
|
407
|
+
self._store_token_data(self._decode_token_body(response), label="refreshed")
|
|
385
408
|
return
|
|
386
409
|
logger.warning("Token refresh failed, acquiring new token...")
|
|
387
410
|
await self._acquire_token()
|
|
@@ -16,6 +16,10 @@ import os
|
|
|
16
16
|
from typing import TYPE_CHECKING, Self
|
|
17
17
|
|
|
18
18
|
from glpi_python_client._async._concurrency import Lock
|
|
19
|
+
from glpi_python_client._async._retry import (
|
|
20
|
+
DEFAULT_MAX_RETRIES,
|
|
21
|
+
validate_retry_settings,
|
|
22
|
+
)
|
|
19
23
|
from glpi_python_client._async.clients.commons._config import (
|
|
20
24
|
build_client_env_config,
|
|
21
25
|
build_client_resources,
|
|
@@ -55,6 +59,8 @@ class _BaseGlpiClient:
|
|
|
55
59
|
v1_base_url: str | None = None,
|
|
56
60
|
v1_user_token: str | None = None,
|
|
57
61
|
v1_app_token: str | None = None,
|
|
62
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
63
|
+
retry_wait_seconds: float | None = None,
|
|
58
64
|
) -> None:
|
|
59
65
|
"""Build the shared resources for a GLPI client.
|
|
60
66
|
|
|
@@ -111,6 +117,17 @@ class _BaseGlpiClient:
|
|
|
111
117
|
``user_token`` for the v1 fallback session.
|
|
112
118
|
v1_app_token : str | None, optional
|
|
113
119
|
``app_token`` for the v1 fallback session.
|
|
120
|
+
max_retries : int, optional
|
|
121
|
+
Automatic retries after the first attempt of a call that failed on
|
|
122
|
+
a network fault (``GlpiTransportError``) or a 5xx answer
|
|
123
|
+
(``GlpiServerError``): ``0`` disables retrying, the default ``2``
|
|
124
|
+
makes 3 attempts in total. It applies to every v2 verb, the v1
|
|
125
|
+
session helpers and OAuth token acquisition. A retried write may
|
|
126
|
+
already have been applied; pass ``0`` when the caller owns its
|
|
127
|
+
own retry policy.
|
|
128
|
+
retry_wait_seconds : float | None, optional
|
|
129
|
+
Pause between two attempts; ``None`` keeps the default of 3
|
|
130
|
+
seconds.
|
|
114
131
|
|
|
115
132
|
Raises
|
|
116
133
|
------
|
|
@@ -120,6 +137,9 @@ class _BaseGlpiClient:
|
|
|
120
137
|
"""
|
|
121
138
|
|
|
122
139
|
self.server_timezone = resolve_server_timezone(server_timezone)
|
|
140
|
+
self._max_retries, self._retry_wait_seconds = validate_retry_settings(
|
|
141
|
+
max_retries, retry_wait_seconds
|
|
142
|
+
)
|
|
123
143
|
resources = build_client_resources(
|
|
124
144
|
glpi_api_url=glpi_api_url,
|
|
125
145
|
client_name=type(self).__name__,
|
|
@@ -132,6 +152,8 @@ class _BaseGlpiClient:
|
|
|
132
152
|
v1_base_url=v1_base_url,
|
|
133
153
|
v1_user_token=v1_user_token,
|
|
134
154
|
v1_app_token=v1_app_token,
|
|
155
|
+
max_retries=self._max_retries,
|
|
156
|
+
retry_wait_seconds=self._retry_wait_seconds,
|
|
135
157
|
)
|
|
136
158
|
self.glpi_api_url = resources.glpi_api_url
|
|
137
159
|
self._session = resources.session
|
|
@@ -160,7 +182,8 @@ class _BaseGlpiClient:
|
|
|
160
182
|
``GLPI_V1_BASE_URL``, ``GLPI_V1_USER_TOKEN``, ``GLPI_V1_APP_TOKEN``,
|
|
161
183
|
``GLPI_ENTITY``, ``GLPI_PROFILE``, ``GLPI_ENTITY_RECURSIVE``,
|
|
162
184
|
``GLPI_LANGUAGE``, ``GLPI_AUTH_TOKEN_REFRESH``,
|
|
163
|
-
``GLPI_SERVER_TIMEZONE``
|
|
185
|
+
``GLPI_SERVER_TIMEZONE``, ``GLPI_MAX_RETRIES``,
|
|
186
|
+
``GLPI_RETRY_WAIT_SECONDS``).
|
|
164
187
|
|
|
165
188
|
Parameters
|
|
166
189
|
----------
|
|
@@ -16,6 +16,7 @@ from glpi_python_client._async.clients.api.administration import (
|
|
|
16
16
|
)
|
|
17
17
|
from glpi_python_client._async.clients.api.assets import ComputerMixin
|
|
18
18
|
from glpi_python_client._async.clients.api.assistance import (
|
|
19
|
+
PendingReasonMixin,
|
|
19
20
|
TeamMemberMixin,
|
|
20
21
|
TicketMixin,
|
|
21
22
|
)
|
|
@@ -27,6 +28,7 @@ from glpi_python_client._async.clients.api.assistance.timeline import (
|
|
|
27
28
|
)
|
|
28
29
|
from glpi_python_client._async.clients.api.dropdowns import (
|
|
29
30
|
ContractTypeMixin,
|
|
31
|
+
ITILCategoryMixin,
|
|
30
32
|
LocationMixin,
|
|
31
33
|
)
|
|
32
34
|
from glpi_python_client._async.clients.api.knowledgebase import (
|
|
@@ -51,11 +53,13 @@ __all__ = [
|
|
|
51
53
|
"EntityMixin",
|
|
52
54
|
"FollowupMixin",
|
|
53
55
|
"GroupMixin",
|
|
56
|
+
"ITILCategoryMixin",
|
|
54
57
|
"KBArticleCommentMixin",
|
|
55
58
|
"KBArticleMixin",
|
|
56
59
|
"KBArticleRevisionMixin",
|
|
57
60
|
"KBCategoryMixin",
|
|
58
61
|
"LocationMixin",
|
|
62
|
+
"PendingReasonMixin",
|
|
59
63
|
"PluginFieldsMixin",
|
|
60
64
|
"SolutionMixin",
|
|
61
65
|
"TeamMemberMixin",
|
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
from glpi_python_client._async.clients.api.assistance._pending_reason import (
|
|
6
|
+
PendingReasonMixin,
|
|
7
|
+
)
|
|
5
8
|
from glpi_python_client._async.clients.api.assistance._team import TeamMemberMixin
|
|
6
9
|
from glpi_python_client._async.clients.api.assistance._ticket import TicketMixin
|
|
7
10
|
|
|
8
|
-
__all__ = ["TeamMemberMixin", "TicketMixin"]
|
|
11
|
+
__all__ = ["PendingReasonMixin", "TeamMemberMixin", "TicketMixin"]
|