|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import typing as t |
| 4 | +import uuid |
| 5 | + |
| 6 | +import globus_sdk |
| 7 | +from globus_sdk.authorizers import GlobusAuthorizer |
| 8 | +from globus_sdk.scopes import GCSCollectionScopes |
| 9 | + |
| 10 | + |
| 11 | +# NOTE: this stub class idea is inspired by the SpecificFlowScopes class stub |
| 12 | +# it implements the same interface as the base class, so it's type-compatible |
| 13 | +# but it raises errors at runtime -- because it can't *actually* be populated with data |
| 14 | +class _GCSCollectionScopesClassStub(GCSCollectionScopes): |
| 15 | + """ |
| 16 | + This internal stub object ensures that the type deductions for type checkers (e.g. |
| 17 | + mypy) on GCSCollectionClient.scopes are correct. |
| 18 | +
|
| 19 | + Primarily, it should be possible to access the `scopes` attribute, the `user` |
| 20 | + scope, and the `resource_server`, but these usages should raise specific and |
| 21 | + informative runtime errors. |
| 22 | +
|
| 23 | + Our types are therefore less accurate for class-var access, but more accurate for |
| 24 | + instance-var access. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self) -> None: |
| 28 | + super().__init__("<stub>") |
| 29 | + |
| 30 | + def __getattribute__(self, name: str) -> t.Any: |
| 31 | + if name == "https": |
| 32 | + _raise_attr_error("https") |
| 33 | + if name == "data_access": |
| 34 | + _raise_attr_error("data_access") |
| 35 | + if name == "resource_server": |
| 36 | + _raise_attr_error("resource_server") |
| 37 | + return object.__getattribute__(self, name) |
| 38 | + |
| 39 | + |
| 40 | +class GCSCollectionClient(globus_sdk.BaseClient): |
| 41 | + """ |
| 42 | + A client for interacting directly with a GCS Collection. |
| 43 | + Typically for HTTPS upload/download via HTTPS-enabled collections. |
| 44 | +
|
| 45 | + .. sdk-sphinx-copy-params:: BaseClient |
| 46 | +
|
| 47 | + :param collection_id: The ID of the collection. |
| 48 | + :param collection_address: The URL of the collection, as might be retrieved from |
| 49 | + the ``https_server`` field in Globus Transfer. |
| 50 | + """ |
| 51 | + |
| 52 | + scopes: GCSCollectionScopes = _GCSCollectionScopesClassStub() |
| 53 | + |
| 54 | + def __init__( |
| 55 | + self, |
| 56 | + collection_id: str | uuid.UUID, |
| 57 | + collection_address: str, |
| 58 | + *, |
| 59 | + environment: str | None = None, |
| 60 | + app: globus_sdk.GlobusApp | None = None, |
| 61 | + app_scopes: list[globus_sdk.scopes.Scope] | None = None, |
| 62 | + authorizer: GlobusAuthorizer | None = None, |
| 63 | + app_name: str | None = None, |
| 64 | + transport: globus_sdk.transport.RequestsTransport | None = None, |
| 65 | + retry_config: globus_sdk.transport.RetryConfig | None = None, |
| 66 | + ) -> None: |
| 67 | + self.collection_id = str(collection_id) |
| 68 | + self.scopes = GCSCollectionScopes(self.collection_id) |
| 69 | + |
| 70 | + if not collection_address.startswith("https://"): |
| 71 | + collection_address = f"https://{collection_address}" |
| 72 | + |
| 73 | + super().__init__( |
| 74 | + environment=environment, |
| 75 | + base_url=collection_address, |
| 76 | + app=app, |
| 77 | + app_scopes=app_scopes, |
| 78 | + authorizer=authorizer, |
| 79 | + app_name=app_name, |
| 80 | + transport=transport, |
| 81 | + retry_config=retry_config, |
| 82 | + ) |
| 83 | + |
| 84 | + @property |
| 85 | + def default_scope_requirements(self) -> list[globus_sdk.Scope]: |
| 86 | + return [self.scopes.https] |
| 87 | + |
| 88 | + |
| 89 | +def _raise_attr_error(name: str) -> t.NoReturn: |
| 90 | + raise AttributeError( |
| 91 | + f"It is not valid to attempt to access the '{name}' attribute of the " |
| 92 | + "GCSCollectionClient class. " |
| 93 | + f"Instead, instantiate a GCSCollectionClient and access the '{name}' attribute " |
| 94 | + "from that instance." |
| 95 | + ) |
0 commit comments