| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
import typing as t |
| 5 |
import uuid |
| 6 |
|
| 7 |
from globus_sdk import client, exc, response |
| 8 |
from globus_sdk._internal import guards |
| 9 |
from globus_sdk.scopes import ( |
| 10 |
GCSCollectionScopes, |
| 11 |
Scope, |
| 12 |
SpecificFlowScopes, |
| 13 |
TimersScopes, |
| 14 |
TransferScopes, |
| 15 |
) |
| 16 |
|
| 17 |
from .data import FlowTimer, TimerJob, TransferTimer |
| 18 |
from .errors import TimersAPIError |
| 19 |
|
| 20 |
log = logging.getLogger(__name__) |
| 21 |
|
| 22 |
|
| 23 |
class TimersClient(client.BaseClient): |
| 24 |
r""" |
| 25 |
Client for the Globus Timers API. |
| 26 |
|
| 27 |
.. sdk-sphinx-copy-params:: BaseClient |
| 28 |
|
| 29 |
.. automethodlist:: globus_sdk.TimersClient |
| 30 |
""" |
| 31 |
|
| 32 |
error_class = TimersAPIError |
| 33 |
service_name = "timer" |
| 34 |
scopes = TimersScopes |
| 35 |
default_scope_requirements = [TimersScopes.timer] |
| 36 |
|
| 37 |
def add_app_transfer_data_access_scope( |
| 38 |
self, collection_ids: uuid.UUID | str | t.Iterable[uuid.UUID | str] |
| 39 |
) -> TimersClient: |
| 40 |
""" |
| 41 |
Add a dependent ``data_access`` scope for one or more given ``collection_ids`` |
| 42 |
to this client's ``GlobusApp``, under the Transfer ``all`` scope. |
| 43 |
Useful for preventing ``ConsentRequired`` errors when creating timers |
| 44 |
that use Globus Connect Server mapped collection(s) as the source or |
| 45 |
destination. |
| 46 |
|
| 47 |
.. warning:: |
| 48 |
|
| 49 |
This method must only be used on ``collection_ids`` for non-High-Assurance |
| 50 |
GCS Mapped Collections. |
| 51 |
|
| 52 |
Use on other collection types, e.g., on GCP Mapped Collections or any form |
| 53 |
of Guest Collection, will result in "Unknown Scope" errors during the login |
| 54 |
flow. |
| 55 |
|
| 56 |
Returns ``self`` for chaining. |
| 57 |
|
| 58 |
Raises ``GlobusSDKUsageError`` if this client was not initialized with an app. |
| 59 |
|
| 60 |
:param collection_ids: a collection ID or an iterable of IDs. |
| 61 |
|
| 62 |
.. tab-set:: |
| 63 |
|
| 64 |
.. tab-item:: Example Usage |
| 65 |
|
| 66 |
.. code-block:: python |
| 67 |
|
| 68 |
app = UserApp("myapp", client_id=NATIVE_APP_CLIENT_ID) |
| 69 |
client = TimersClient(app=app).add_app_transfer_data_access_scope(COLLECTION_ID) |
| 70 |
|
| 71 |
transfer_data = TransferData(COLLECTION_ID, COLLECTION_ID) |
| 72 |
transfer_data.add_item("/staging/", "/active/") |
| 73 |
|
| 74 |
daily_timer = TransferTimer( |
| 75 |
name="test_timer", schedule=RecurringTimerSchedule(24 * 60 * 60), body=transfer_data |
| 76 |
) |
| 77 |
|
| 78 |
client.create_timer(daily_timer) |
| 79 |
""" # noqa: E501 |
| 80 |
if isinstance(collection_ids, (str, uuid.UUID)): |
| 81 |
guards.validators.uuidlike("collection_ids", collection_ids) |
| 82 |
# wrap the collection_ids input in a list for consistent iteration below |
| 83 |
collection_ids_ = [collection_ids] |
| 84 |
else: |
| 85 |
# copy to a list so that ephemeral iterables can be iterated multiple times |
| 86 |
collection_ids_ = list(collection_ids) |
| 87 |
for i, c in enumerate(collection_ids_): |
| 88 |
guards.validators.uuidlike(f"collection_ids[{i}]", c) |
| 89 |
|
| 90 |
dependencies: list[Scope] = [] |
| 91 |
for coll_id in collection_ids_: |
| 92 |
data_access_scope = GCSCollectionScopes( |
| 93 |
str(coll_id) |
| 94 |
).data_access.with_optional(True) |
| 95 |
dependencies.append(data_access_scope) |
| 96 |
transfer_scope = TransferScopes.all.with_dependencies(dependencies) |
| 97 |
|
| 98 |
timers_scope = TimersScopes.timer.with_dependency(transfer_scope) |
| 99 |
self.add_app_scope(timers_scope) |
| 100 |
return self |
| 101 |
|
| 102 |
def add_app_flow_user_scope( |
| 103 |
self, flow_ids: uuid.UUID | str | t.Iterable[uuid.UUID | str] |
| 104 |
) -> TimersClient: |
| 105 |
""" |
| 106 |
Add a dependent flow ``user`` scope for one or more given ``flow_ids`` |
| 107 |
to this client's ``GlobusApp``, under the Timers ``timer`` scope. |
| 108 |
Needed to prevent ``ConsentRequired`` errors when creating flow timers. |
| 109 |
|
| 110 |
Returns ``self`` for chaining. |
| 111 |
|
| 112 |
Raises ``GlobusSDKUsageError`` if this client was not initialized with an app. |
| 113 |
|
| 114 |
:param flow_ids: a flow ID or an iterable of IDs. |
| 115 |
|
| 116 |
.. tab-set:: |
| 117 |
|
| 118 |
.. tab-item:: Example Usage |
| 119 |
|
| 120 |
.. code-block:: python |
| 121 |
|
| 122 |
app = UserApp("myapp", client_id=NATIVE_APP_CLIENT_ID) |
| 123 |
client = TimersClient(app=app) |
| 124 |
client.add_app_flow_user_scope(FLOW_ID) |
| 125 |
|
| 126 |
flow_timer = FlowTimer( |
| 127 |
name="my flow timer", |
| 128 |
flow_id=FLOW_ID, |
| 129 |
schedule=RecurringTimerSchedule(24 * 60 * 60), |
| 130 |
body={ |
| 131 |
"body": { |
| 132 |
"input_key": "input_value", |
| 133 |
}, |
| 134 |
}, |
| 135 |
) |
| 136 |
|
| 137 |
client.create_timer(flow_timer) |
| 138 |
""" # noqa: E501 |
| 139 |
if isinstance(flow_ids, (str, uuid.UUID)): |
| 140 |
guards.validators.uuidlike("flow_ids", flow_ids) |
| 141 |
# wrap the flow_ids input in a list for consistent iteration below |
| 142 |
flow_ids_ = [flow_ids] |
| 143 |
else: |
| 144 |
# copy to a list so that ephemeral iterables can be iterated multiple times |
| 145 |
flow_ids_ = list(flow_ids) |
| 146 |
for i, c in enumerate(flow_ids_): |
| 147 |
guards.validators.uuidlike(f"flow_ids[{i}]", c) |
| 148 |
|
| 149 |
dependencies: list[Scope] = [ |
| 150 |
SpecificFlowScopes(flow_id).user for flow_id in flow_ids_ |
| 151 |
] |
| 152 |
timers_scope = TimersScopes.timer.with_dependencies(dependencies) |
| 153 |
self.add_app_scope(timers_scope) |
| 154 |
return self |
| 155 |
|
| 156 |
def list_jobs( |
| 157 |
self, *, query_params: dict[str, t.Any] | None = None |
| 158 |
) -> response.GlobusHTTPResponse: |
| 159 |
""" |
| 160 |
``GET /jobs/`` |
| 161 |
|
| 162 |
:param query_params: additional parameters to pass as query params |
| 163 |
|
| 164 |
**Examples** |
| 165 |
|
| 166 |
>>> timer_client = globus_sdk.TimersClient(...) |
| 167 |
>>> jobs = timer_client.list_jobs() |
| 168 |
""" |
| 169 |
log.debug(f"TimersClient.list_jobs({query_params})") |
| 170 |
return self.get("/jobs/", query_params=query_params) |
| 171 |
|
| 172 |
def get_job( |
| 173 |
self, |
| 174 |
job_id: uuid.UUID | str, |
| 175 |
*, |
| 176 |
query_params: dict[str, t.Any] | None = None, |
| 177 |
) -> response.GlobusHTTPResponse: |
| 178 |
""" |
| 179 |
``GET /jobs/<job_id>`` |
| 180 |
|
| 181 |
:param job_id: the ID of the timer ("job") |
| 182 |
:param query_params: additional parameters to pass as query params |
| 183 |
|
| 184 |
**Examples** |
| 185 |
|
| 186 |
>>> timer_client = globus_sdk.TimersClient(...) |
| 187 |
>>> job = timer_client.get_job(job_id) |
| 188 |
>>> assert job["job_id"] == job_id |
| 189 |
""" |
| 190 |
log.debug(f"TimersClient.get_job({job_id})") |
| 191 |
return self.get(f"/jobs/{job_id}", query_params=query_params) |
| 192 |
|
| 193 |
def create_timer( |
| 194 |
self, timer: dict[str, t.Any] | TransferTimer | FlowTimer |
| 195 |
) -> response.GlobusHTTPResponse: |
| 196 |
""" |
| 197 |
:param timer: a document defining the new timer |
| 198 |
|
| 199 |
A ``TransferTimer`` object can be constructed from a ``TransferData`` object, |
| 200 |
which is the recommended way to create a timer for data transfers. |
| 201 |
|
| 202 |
**Examples** |
| 203 |
|
| 204 |
.. tab-set:: |
| 205 |
|
| 206 |
.. tab-item:: Example Usage |
| 207 |
|
| 208 |
.. code-block:: pycon |
| 209 |
|
| 210 |
>>> transfer_data = TransferData(...) |
| 211 |
>>> timers_client = globus_sdk.TimersClient(...) |
| 212 |
>>> create_doc = globus_sdk.TransferTimer( |
| 213 |
... name="my-timer", |
| 214 |
... schedule={"type": "recurring", "interval": 1800}, |
| 215 |
... body=transfer_data, |
| 216 |
... ) |
| 217 |
>>> response = timers_client.create_timer(timer=create_doc) |
| 218 |
|
| 219 |
.. tab-item:: Example Response Data |
| 220 |
|
| 221 |
.. expandtestfixture:: timer.create_timer |
| 222 |
|
| 223 |
.. tab-item:: API Info |
| 224 |
|
| 225 |
``POST /v2/timer`` |
| 226 |
""" |
| 227 |
if isinstance(timer, TimerJob): |
| 228 |
raise exc.GlobusSDKUsageError( |
| 229 |
"Cannot pass a TimerJob to create_timer(). " |
| 230 |
"Create a TransferTimer instead." |
| 231 |
) |
| 232 |
log.debug("TimersClient.create_timer(...)") |
| 233 |
return self.post("/v2/timer", data={"timer": timer}) |
| 234 |
|
| 235 |
def create_job( |
| 236 |
self, data: dict[str, t.Any] | TimerJob |
| 237 |
) -> response.GlobusHTTPResponse: |
| 238 |
""" |
| 239 |
``POST /jobs/`` |
| 240 |
|
| 241 |
:param data: a timer document used to create the new timer ("job") |
| 242 |
|
| 243 |
**Examples** |
| 244 |
|
| 245 |
>>> from datetime import datetime, timedelta, timezone |
| 246 |
>>> callback_url = ... |
| 247 |
>>> data = ... |
| 248 |
>>> timer_client = globus_sdk.TimersClient(...) |
| 249 |
>>> job = TimerJob( |
| 250 |
... callback_url, |
| 251 |
... data, |
| 252 |
... datetime.now(tz=timezone.utc).replace(tzinfo=None), |
| 253 |
... timedelta(days=14), |
| 254 |
... name="my-timer-job" |
| 255 |
... ) |
| 256 |
>>> timer_result = timers_client.create_job(job) |
| 257 |
""" |
| 258 |
if isinstance(data, TransferTimer): |
| 259 |
raise exc.GlobusSDKUsageError( |
| 260 |
"Cannot pass a TransferTimer to create_job(). Use create_timer() " |
| 261 |
"instead." |
| 262 |
) |
| 263 |
log.debug(f"TimersClient.create_job({data})") |
| 264 |
return self.post("/jobs/", data=data) |
| 265 |
|
| 266 |
def update_job( |
| 267 |
self, job_id: uuid.UUID | str, data: dict[str, t.Any] |
| 268 |
) -> response.GlobusHTTPResponse: |
| 269 |
""" |
| 270 |
``PATCH /jobs/<job_id>`` |
| 271 |
|
| 272 |
:param job_id: the ID of the timer ("job") |
| 273 |
:param data: a partial timer document used to update the job |
| 274 |
|
| 275 |
**Examples** |
| 276 |
|
| 277 |
>>> timer_client = globus_sdk.TimersClient(...) |
| 278 |
>>> timer_client.update_job(job_id, {"name": "new name}"}) |
| 279 |
""" |
| 280 |
log.debug(f"TimersClient.update_job({job_id}, {data})") |
| 281 |
return self.patch(f"/jobs/{job_id}", data=data) |
| 282 |
|
| 283 |
def delete_job( |
| 284 |
self, |
| 285 |
job_id: uuid.UUID | str, |
| 286 |
) -> response.GlobusHTTPResponse: |
| 287 |
""" |
| 288 |
``DELETE /jobs/<job_id>`` |
| 289 |
|
| 290 |
:param job_id: the ID of the timer ("job") |
| 291 |
|
| 292 |
**Examples** |
| 293 |
|
| 294 |
>>> timer_client = globus_sdk.TimersClient(...) |
| 295 |
>>> timer_client.delete_job(job_id) |
| 296 |
""" |
| 297 |
log.debug(f"TimersClient.delete_job({job_id})") |
| 298 |
return self.delete(f"/jobs/{job_id}") |
| 299 |
|
| 300 |
def pause_job( |
| 301 |
self, |
| 302 |
job_id: uuid.UUID | str, |
| 303 |
) -> response.GlobusHTTPResponse: |
| 304 |
""" |
| 305 |
Make a timer job inactive, preventing it from running until it is resumed. |
| 306 |
|
| 307 |
:param job_id: The ID of the timer to pause |
| 308 |
|
| 309 |
**Examples** |
| 310 |
|
| 311 |
>>> timer_client = globus_sdk.TimersClient(...) |
| 312 |
>>> timer_client.pause_job(job_id) |
| 313 |
""" |
| 314 |
log.debug(f"TimersClient.pause_job({job_id})") |
| 315 |
return self.post(f"/jobs/{job_id}/pause") |
| 316 |
|
| 317 |
def resume_job( |
| 318 |
self, |
| 319 |
job_id: uuid.UUID | str, |
| 320 |
*, |
| 321 |
update_credentials: bool | None = None, |
| 322 |
) -> response.GlobusHTTPResponse: |
| 323 |
""" |
| 324 |
Resume an inactive timer job, optionally replacing credentials to resolve |
| 325 |
issues with insufficient authorization. |
| 326 |
|
| 327 |
:param job_id: The ID of the timer to resume |
| 328 |
:param update_credentials: When true, replace the credentials for the timer |
| 329 |
using the credentials for this resume call. This can be used to resolve |
| 330 |
authorization errors (such as session and consent errors), but it also |
| 331 |
could introduce session and consent errors, if the credentials being used |
| 332 |
to resume lack some necessary properties of the credentials they're |
| 333 |
replacing. If not supplied, the Timers service will determine whether to |
| 334 |
replace credentials according to the reason why the timer job |
| 335 |
became inactive. |
| 336 |
|
| 337 |
**Examples** |
| 338 |
|
| 339 |
>>> timer_client = globus_sdk.TimersClient(...) |
| 340 |
>>> timer_client.resume_job(job_id) |
| 341 |
""" |
| 342 |
log.debug(f"TimersClient.resume_job({job_id})") |
| 343 |
data = {} |
| 344 |
if update_credentials is not None: |
| 345 |
data["update_credentials"] = update_credentials |
| 346 |
return self.post(f"/jobs/{job_id}/resume", data=data) |