From 73833b2da1e71009ddc0be49e0077469a9a6d98c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ola=20Flisb=C3=A4ck?= Date: Tue, 27 Jun 2023 13:55:22 +0200 Subject: [PATCH] fix: support fallback to env variable for JH token --- src/api.ts | 27 +++++++++++++++++---------- src/client.ts | 4 ++-- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/api.ts b/src/api.ts index e69a958..7fba8e4 100644 --- a/src/api.ts +++ b/src/api.ts @@ -91,7 +91,7 @@ class Api { }: { impactApiKey?: string impactToken?: string - jupyterHubToken: string + jupyterHubToken?: string serverAddress: string jupyterHubUserPath?: string }) { @@ -99,12 +99,20 @@ class Api { this.impactApiKey = impactApiKey this.impactToken = impactToken - if (!jupyterHubToken) { - throw new ApiError({ - errorCode: MissingJupyterHubToken, - message: - 'Impact client instantiation failed: The jupyterHubToken parameter is mandatory', - }) + if (jupyterHubToken) { + this.jhToken = jupyterHubToken + } else { + // No provided JupyterHub token, to mimick impact-python-client we try to + // fallback on the environment variable available inside JupyterHub. + if (process.env.JUPYTERHUB_API_TOKEN) { + this.jhToken = process.env.JUPYTERHUB_API_TOKEN + } else { + throw new ApiError({ + errorCode: MissingJupyterHubToken, + message: + 'Impact client instantiation failed: Missing JupyterHub token.', + }) + } } if (jupyterHubUserPath) { @@ -113,7 +121,6 @@ class Api { (jupyterHubUserPath.endsWith('/') ? '' : '/') } - this.jhToken = jupyterHubToken this.configureAxios() } @@ -124,7 +131,7 @@ class Api { serverAddress, }: { impactApiKey: string - jupyterHubToken: string + jupyterHubToken?: string jupyterHubUserPath?: string serverAddress: string }) { @@ -143,7 +150,7 @@ class Api { serverAddress, }: { impactToken: string - jupyterHubToken: string + jupyterHubToken?: string jupyterHubUserPath?: string serverAddress: string }) { diff --git a/src/client.ts b/src/client.ts index 73d984f..32f1d0b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -16,7 +16,7 @@ class Client { serverAddress, }: { impactApiKey: string - jupyterHubToken: string + jupyterHubToken?: string jupyterHubUserPath?: string serverAddress: string }) { @@ -36,7 +36,7 @@ class Client { serverAddress, }: { impactToken: string - jupyterHubToken: string + jupyterHubToken?: string jupyterHubUserPath?: string serverAddress: string }) {