caterva2.c2context#

caterva2.c2context(*, urlbase: str | None = None, username: str | None = None, password: str | None = None, auth_cookie: str | None = None) None#

Context manager for setting parameters in Caterva2 subscriber requests.

Parameters not specified or set to None will inherit values from the previous context manager, defaulting to caterva2.sub_urlbase_default for the URL and None for the authentication cookie. Parameters set to an empty string will not be used in requests.

To authorize requests, provide either an existing auth_cookie or both username and password to log in and obtain the cookie. The cookie will persist until explicitly reset or reobtained in a subsequent context manager invocation.

Note that this manager is reentrant but not safe for concurrent use.

Parameters:
  • urlbase (str | None) – The base URL to use for all operations within the context.

  • username (str | None) – The username for logging in to obtain an authorization token.

  • password (str | None) – The password for logging in to obtain an authorization cookie.

  • auth_cookie (str | None) – The cookie used as the default for operations within the context.

Yields:

out (None)

Examples

>>> import caterva2 as cat2
>>> with cat2.c2context(urlbase='https://demo.caterva2.net'):
...     print(cat2.get_roots()['h5lung_j2k'])
...
{'name': 'h5lung_j2k', 'http': 'localhost:8012', 'subscribed': None}
>>> urlbase = 'https://cat2.cloud/demo'
>>> with cat2.c2context(urlbase=urlbase, username='user@example.com', password='foo'):
...     print(cat2.upload('root-example/ds-2d-fields.b2nd', '@personal/fields.b2nd'))
...
@personal/fields.b2nd