| 1 |
""" |
| 2 |
This module models a read-write object representation of the |
| 3 |
X-Globus-Client-Info header. |
| 4 |
|
| 5 |
The spec for X-Globus-Client-Info is documented, in brief, in the GlobusClientInfo class |
| 6 |
docstring. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
import typing as t |
| 12 |
|
| 13 |
from globus_sdk import __version__, exc |
| 14 |
|
| 15 |
_RESERVED_CHARS = ";,=" |
| 16 |
|
| 17 |
|
| 18 |
class GlobusClientInfo: |
| 19 |
""" |
| 20 |
An implementation of X-Globus-Client-Info as an object. This header encodes a |
| 21 |
mapping of multiple products to versions and potentially other information. |
| 22 |
|
| 23 |
Values can be added to a clientinfo object via the ``add()`` method. |
| 24 |
|
| 25 |
The object always initializes itself to start with |
| 26 |
|
| 27 |
product=python-sdk,version=... |
| 28 |
|
| 29 |
using the current package version information. |
| 30 |
|
| 31 |
.. rubric:: ``X-Globus-Client-Info`` Specification |
| 32 |
|
| 33 |
Header Name: ``X-Globus-Client-Info`` |
| 34 |
|
| 35 |
Header Value: |
| 36 |
|
| 37 |
- A semicolon (``;``) separated list of client information. |
| 38 |
|
| 39 |
- Client information is a comma-separated list of ``=`` delimited key-value pairs. |
| 40 |
Well-known values for client-information are: |
| 41 |
|
| 42 |
- ``product``: A unique identifier of the product. |
| 43 |
- ``version``: Relevant version information for the product. |
| 44 |
|
| 45 |
- Based on the above, the characters ``;,=`` should be considered reserved and |
| 46 |
should NOT be included in client information values to ensure proper parsing. |
| 47 |
|
| 48 |
.. rubric:: Example Headers |
| 49 |
|
| 50 |
.. code-block:: none |
| 51 |
|
| 52 |
X-Globus-Client-Info: product=python-sdk,version=3.32.1 |
| 53 |
X-Globus-Client-Info: product=python-sdk,version=3.32.1;product=cli,version=4.0.0a1 |
| 54 |
|
| 55 |
.. note:: |
| 56 |
|
| 57 |
The ``GlobusClientInfo`` object is not guaranteed to reject all invalid usages. |
| 58 |
For example, ``product`` is required to be unique per header, and users are |
| 59 |
expected to enforce this in their usage. |
| 60 |
|
| 61 |
:param update_callback: A callback function to be invoked each time the content of |
| 62 |
the GlobusClientInfo changes via ``add()`` or ``clear()``. |
| 63 |
""" # noqa: E501 |
| 64 |
|
| 65 |
def __init__( |
| 66 |
self, *, update_callback: t.Callable[[GlobusClientInfo], None] | None = None |
| 67 |
) -> None: |
| 68 |
self.infos: list[str] = [] |
| 69 |
|
| 70 |
# set `update_callback` to `None` at first so that `add()` can run without |
| 71 |
# triggering it during `__init__` |
| 72 |
self.update_callback = None |
| 73 |
self.add({"product": "python-sdk", "version": __version__}) |
| 74 |
self.update_callback = update_callback |
| 75 |
|
| 76 |
def __bool__(self) -> bool: |
| 77 |
"""Check if there are any values present.""" |
| 78 |
return bool(self.infos) |
| 79 |
|
| 80 |
def format(self) -> str: |
| 81 |
"""Format as a header value.""" |
| 82 |
return ";".join(self.infos) |
| 83 |
|
| 84 |
def add(self, value: str | dict[str, str]) -> None: |
| 85 |
""" |
| 86 |
Add an item to the clientinfo. The item is either already formatted |
| 87 |
as a string, or is a dict containing values to format. |
| 88 |
|
| 89 |
:param value: The element to add to the client-info. If it is a dict, |
| 90 |
it may not contain reserved characters in any keys or values. If it is a |
| 91 |
string, it cannot contain the ``;`` separator. |
| 92 |
""" |
| 93 |
if not isinstance(value, str): |
| 94 |
value = ",".join(_format_items(value)) |
| 95 |
elif ";" in value: |
| 96 |
raise exc.GlobusSDKUsageError( |
| 97 |
"GlobusClientInfo.add() cannot be used to add multiple items in " |
| 98 |
"an already-joined string. Add items separately instead. " |
| 99 |
f"Bad usage: '{value}'" |
| 100 |
) |
| 101 |
self.infos.append(value) |
| 102 |
|
| 103 |
if self.update_callback is not None: |
| 104 |
self.update_callback(self) |
| 105 |
|
| 106 |
def clear(self) -> None: |
| 107 |
"""Empty the list of info strings and trigger the update callback.""" |
| 108 |
self.infos = [] |
| 109 |
if self.update_callback is not None: |
| 110 |
self.update_callback(self) |
| 111 |
|
| 112 |
|
| 113 |
def _format_items(info: dict[str, str]) -> t.Iterable[str]: |
| 114 |
"""Format the items in a dict, yielding the contents as an iterable.""" |
| 115 |
for key, value in info.items(): |
| 116 |
_check_reserved_chars(key, value) |
| 117 |
yield f"{key}={value}" |
| 118 |
|
| 119 |
|
| 120 |
def _check_reserved_chars(key: str, value: str) -> None: |
| 121 |
"""Check a key-value pair to see if it uses reserved chars.""" |
| 122 |
if any(c in x for c in _RESERVED_CHARS for x in (key, value)): |
| 123 |
raise exc.GlobusSDKUsageError( |
| 124 |
"X-Globus-Client-Info reserved characters cannot be used in keys or " |
| 125 |
f"values. Bad usage: '{key}: {value}'" |
| 126 |
) |