JupyterAsyncClient#
- class rubin.nublado.client.JupyterAsyncClient(*, discovery_client, logger, timeout, token, username)#
Bases:
objectWrapper around an HTTP client with JupyterHub and JupyterLab support.
The Nublado client, rather than using Jupyter access tokens, simulates a web browser when interacting with JupyterHub and JupyterLab. This requires some special handling:
An
Authorizationheader must be set on every request to get past Gafaelfawr authentication.The client needs a persistent cookie jar to handle XSRF cookies.
An additional
X-XSRFTokenheader must be added to requests containing the XSRF token. (This is easier than trying to inject it into the query string or POST body.)The XSRF token for the
X-XSRFTokenheader must be extracted from the cookie jar, but it may only be present in some intermediate responses in redirect chains.Sec-Fetch-Modemust be set to appropriate values on different requests to avoid annoying logs messages or XSRF validation failures.
This wrapper around an HTTPX
AsyncClienthandles that complexity and exposes a simple API to the rest of the Nublado client.- Parameters:
discovery_client (
DiscoveryClient) – Repertoire discovery client, used to find the base URL of JupyterHub.logger (
BoundLogger) – Logger to use.timeout (
timedelta) – Timeout to use when talking to JupyterHub and JupyterLab. This is used as a connection, read, and write timeout for all regular HTTP calls.token (
str) – Gafaelfawr token to use for authentication.username (
str) – Username on whose behalf the client will be acting.
Methods Summary
aclose()Close the underlying HTTP connection pool.
delete(route, *[, add_referer])Perform a DELETE request to JupyterHub or JupyterLab.
get(route, *[, add_referer, fetch_mode])Perform a GET request to JupyterHub or JupyterLab.
open_sse_stream(route)Open a server-sent events stream.
open_websocket(route, *, open_timeout, max_size)Open a WebSocket connection.
post(route, *[, content, data, json, ...])Perform a POST request to JupyterHub or JupyterLab.
Methods Documentation
- async aclose()#
Close the underlying HTTP connection pool.
This invalidates the client object. It can no longer be used after this method is called.
- Return type:
- async delete(route, *, add_referer=False)#
Perform a DELETE request to JupyterHub or JupyterLab.
- Parameters:
- Returns:
HTTP response at the end of any redirect chain.
- Return type:
httpx.Response
- Raises:
NubladoDiscoveryError – Raised if Nublado is missing from service discovery.
NubladoWebError – Raised if there were HTTP errors talking to Nublado.
rubin.repertoire.RepertoireError – Raised if there was an error talking to service discovery.
- async get(route, *, add_referer=False, fetch_mode='same-origin')#
Perform a GET request to JupyterHub or JupyterLab.
Handles authentication and redirect following, ensuring that the redirects are relative to either the JupyterHub or JupyterLab base URLs.
- Parameters:
route (
str) – Route relative to the base URL of Nublado. Routes starting withuserare considered JupyterLab routes.add_referer (
bool, default:False) – Whether to add aRefererheader pointing to the JupyterHub home page. This is required by JupyterHub in some cases.fetch_mode (
TypeAliasType, default:'same-origin') – Value ofSec-Fetch-Modeheader to send. This suppresses some log noise and improves XSRF handling in JupyterHub and JupyterLab.
- Returns:
HTTP response at the end of any redirect chain.
- Return type:
httpx.Response
- Raises:
NubladoDiscoveryError – Raised if Nublado is missing from service discovery.
NubladoRedirectError – Raised if the URL is outside of Nublado’s URL space or there is a redirect loop.
NubladoWebError – Raised if there were HTTP errors talking to Nublado.
rubin.repertoire.RepertoireError – Raised if there was an error talking to service discovery.
- async open_sse_stream(route)#
Open a server-sent events stream.
- Parameters:
route (
str) – Route relative to the base URL of Nublado. Routes starting withuserare considered JupyterLab routes.- Returns:
Context manager that provides an
httpx_sse.EventSource.- Return type:
- Raises:
NubladoDiscoveryError – Raised if Nublado is missing from service discovery.
NubladoWebError – Raised if there were HTTP errors talking to Nublado.
rubin.repertoire.RepertoireError – Raised if there was an error talking to service discovery.
- async open_websocket(route, *, open_timeout, max_size)#
Open a WebSocket connection.
- Parameters:
- Returns:
Context manager that provides a
ClientConnection.- Return type:
- Raises:
NubladoDiscoveryError – Raised if Nublado is missing from service discovery.
NubladoRedirectError – Raised if the URL is outside of Nublado’s URL space or there is a redirect loop.
rubin.repertoire.RepertoireError – Raised if there was an error talking to service discovery.
- async post(route, *, content=None, data=None, json=None, timeout=None, add_referer=False)#
Perform a POST request to JupyterHub or JupyterLab.
Follows redirects in the response using GET requests.
- Parameters:
route (
str) – Route relative to the base URL of Nublado. Routes starting withuserare considered JupyterLab routes.contents – Raw contents for the POST body. Only one of
contents,data, orjsonmay be specified.data (
dict[str,Any] |None, default:None) – Data for the POST body. Only one ofcontents,data, orjsonmay be specified.json (
dict[str,Any] |None, default:None) – Data for a POST body formatted as JSON. Only one ofcontents,data, orjsonmay be specified.timeout (
Timeout|None, default:None) – HTTPX timeout settings, overriding the defaults.add_referer (
bool, default:False) – Whether to add aRefererheader pointing to the JupyterHub home page. This is required by JupyterHub in some cases.
- Returns:
HTTP response at the end of any redirect chain.
- Return type:
httpx.Response
- Raises:
NubladoDiscoveryError – Raised if Nublado is missing from service discovery.
NubladoRedirectError – Raised if the URL is outside of Nublado’s URL space or there is a redirect loop.
NubladoWebError – Raised if there were HTTP errors talking to Nublado.
rubin.repertoire.RepertoireError – Raised if there was an error talking to service discovery.