Skip to content

Commit

Permalink
Split API client into hub and service client
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Mar 8, 2024
1 parent 8a9de41 commit 5516a6e
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 18 deletions.
5 changes: 4 additions & 1 deletion frontend/src/common/AxiosContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { createContext, useContext } from 'react';
import { AxiosClient } from './axiosclient';

export const AxiosContext = createContext<AxiosClient>(new AxiosClient({}));
export const AxiosContext = createContext<{
hubClient: AxiosClient;
serviceClient: AxiosClient;
}>({ hubClient: new AxiosClient({}), serviceClient: new AxiosClient({}) });

export const useAxios = () => {
return useContext(AxiosContext);
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/common/JupyterhubContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { createContext, useContext } from 'react';

export interface IJupyterhubData {
baseUrl: string;
prefix: string;
servicePrefix: string;
hubPrefix: string;
user: string;
adminAccess: boolean;
xsrfToken: string;
}
export const JupyterhubContext = createContext<IJupyterhubData>({
baseUrl: '',
prefix: '',
servicePrefix: '',
hubPrefix: '',
user: '',
adminAccess: false,
xsrfToken: ''
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/environments/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ export interface IAppProps {
}
export default function App(props: IAppProps) {
const jhData = useJupyterhub();
const axios = useMemo(() => {
const baseUrl = jhData.baseUrl;
const hubClient = useMemo(() => {
const baseUrl = jhData.hubPrefix;
const xsrfToken = jhData.xsrfToken;
return new AxiosClient({ baseUrl, xsrfToken });
}, [jhData]);
const serviceClient = useMemo(() => {
const baseUrl = jhData.servicePrefix;
const xsrfToken = jhData.xsrfToken;
return new AxiosClient({ baseUrl, xsrfToken });
}, [jhData]);
return (
<ThemeProvider theme={customTheme}>
<AxiosContext.Provider value={axios}>
<AxiosContext.Provider value={{ hubClient, serviceClient }}>
<ScopedCssBaseline>
<Stack sx={{ padding: 1 }} spacing={1}>
<NewEnvironmentDialog
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/environments/NewEnvironmentDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function _NewEnvironmentDialog(props: INewEnvironmentDialogProps) {
data.memory = data.memory ?? '2';
data.username = data.username ?? '';
data.password = data.password ?? '';
const response = await axios.request({
const response = await axios.serviceClient.request({
method: 'post',
prefix: API_PREFIX,
path: ENV_PREFIX,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/environments/RemoveEnvironmentButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function _RemoveEnvironmentButton(props: IRemoveEnvironmentButton) {
const axios = useAxios();

const removeEnv = useCallback(async () => {
const response = await axios.request({
const response = await axios.serviceClient.request({
method: 'delete',
prefix: API_PREFIX,
path: ENV_PREFIX,
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/environments/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ if (rootElement) {
configData = JSON.parse(dataElement.textContent || '') as IAppProps;
}
const jhData = (window as any).jhdata;
const { base_url, xsrf_token, user, prefix, admin_access } = jhData;
const {
base_url,
xsrf_token,
user,
hub_prefix,
service_prefix,
admin_access
} = jhData;

root.render(
<JupyterhubContext.Provider
value={{
baseUrl: base_url,
xsrfToken: xsrf_token,
user,
prefix,
hubPrefix: hub_prefix ?? base_url,
servicePrefix: service_prefix ?? base_url,
adminAccess: admin_access
}}
>
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/servers/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ export interface IAppProps {
}
export default function App(props: IAppProps) {
const jhData = useJupyterhub();
const axios = useMemo(() => {
const baseUrl = jhData.baseUrl;
const hubClient = useMemo(() => {
const baseUrl = jhData.hubPrefix;
const xsrfToken = jhData.xsrfToken;
return new AxiosClient({ baseUrl, xsrfToken });
}, [jhData]);
const serviceClient = useMemo(() => {
const baseUrl = jhData.servicePrefix;
const xsrfToken = jhData.xsrfToken;
return new AxiosClient({ baseUrl, xsrfToken });
}, [jhData]);

return (
<ThemeProvider theme={customTheme}>
<AxiosContext.Provider value={axios}>
<AxiosContext.Provider value={{ hubClient, serviceClient }}>
<ScopedCssBaseline>
<Stack sx={{ padding: 1 }} spacing={1}>
<NewServerDialog
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/servers/NewServerDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function _NewServerDialog(props: INewServerDialogProps) {
path = jhData.user;
}
try {
await axios.request({
await axios.hubClient.request({
method: 'post',
prefix: SPAWN_PREFIX,
path,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/servers/OpenServerButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function _OpenServerButton(props: IOpenServerButton) {
const data = new FormData();
data.append('image', imageName);
try {
await axios.request({
await axios.hubClient.request({
method: 'post',
prefix: SPAWN_PREFIX,
path: `${jhData.user}/${props.serverName}`,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/servers/RemoveServerButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function _RemoveServerButton(props: IRemoveServerButton) {
path = `users/${jhData.user}/server`;
}
try {
await axios.request({
await axios.hubClient.request({
method: 'delete',
prefix: API_PREFIX,
path,
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/servers/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@ if (rootElement) {
configData = JSON.parse(dataElement.textContent || '') as IAppProps;
}
const jhData = (window as any).jhdata;
const { base_url, xsrf_token, user, prefix, admin_access } = jhData;
const {
base_url,
xsrf_token,
user,
hub_prefix,
service_prefix,
admin_access
} = jhData;
root.render(
<JupyterhubContext.Provider
value={{
baseUrl: base_url,
xsrfToken: xsrf_token,
user,
prefix,
hubPrefix: hub_prefix ?? base_url,
servicePrefix: service_prefix ?? base_url,
adminAccess: admin_access
}}
>
Expand Down

0 comments on commit 5516a6e

Please sign in to comment.