caterva2.c2context#

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

Context manager that sets parameters in Caterva2 subscriber requests.

A parameter not specified or set to None inherits the value set by the previous context manager, defaulting the subscriber url to caterva2.sub_urlbase_default and the authentication cookie to None. Parameters set to the empty string are not to be used in requests (with no default either).

If the subscriber requires authorization for requests, you may either provide auth_cookie (which you should have obtained previously from the subscriber), or both username and password to get that cookie by first logging in to the subscriber. The cookie will be reused until explicitly reset or requested again in a latter context manager invocation.

Please note that this manager is reentrant but not concurrency-safe.

Parameters:
  • urlbase (str | None) – A URL base that will be used as default for all operations inside the context manager.

  • username (str | None) – A name to be used in credentials to login to the subscriber and get an authorization token from it.

  • password (str | None) – A secret to be used in credentials to login to the subscriber and get an authorization cookie from it.

  • auth_cookie (str | None) – A token that will be used as default for all operations inside the context manager.

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