Skip to content

Commit

Permalink
Add option to override endpoint (#37)
Browse files Browse the repository at this point in the history
* Add option to override endpoint

* Bump version

* Fix name, add icon for settings
  • Loading branch information
krassowski authored Jul 19, 2024
1 parent e9bb3d6 commit 96c8b60
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jupyterlab-gallery",
"version": "0.4.0",
"version": "0.5.0",
"description": "A JupyterLab gallery extension for presenting and downloading examples from remote repositories",
"keywords": [
"jupyter",
Expand Down
12 changes: 10 additions & 2 deletions schema/plugin.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
{
"jupyter.lab.shortcuts": [],
"title": "jupyterlab-gallery",
"jupyter.lab.setting-icon": "jupyterlab-gallery:gallery",
"jupyter.lab.setting-icon-label": "Gallery",
"title": "Gallery",
"description": "jupyterlab-gallery settings.",
"type": "object",
"properties": {},
"properties": {
"endpoint": {
"title": "Server endpoint",
"type": "string",
"default": "jupyterlab-gallery"
}
},
"additionalProperties": false
}
11 changes: 8 additions & 3 deletions src/gallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class GalleryWidget extends ReactWidget {
openPath: (path: string) => void;
fileChanged: Contents.IManager['fileChanged'];
refreshFileBrowser: () => Promise<void>;
serverAPI: string;
}
) {
super();
Expand Down Expand Up @@ -61,7 +62,7 @@ export class GalleryWidget extends ReactWidget {
if (xsrfTokenMatch) {
args['_xsrf'] = xsrfTokenMatch[1];
}
await requestAPI('pull', {
await requestAPI('pull', this.options.serverAPI, {
method: 'POST',
body: JSON.stringify(args)
});
Expand All @@ -82,7 +83,8 @@ export class GalleryWidget extends ReactWidget {
error => {
// TODO
console.error(error);
}
},
this.options.serverAPI
);
this._stream.connect(this._reloadOnFinish);
void this._load();
Expand All @@ -105,7 +107,10 @@ export class GalleryWidget extends ReactWidget {

private async _load() {
try {
const data = await requestAPI<IExhibitReply>('exhibits');
const data = await requestAPI<IExhibitReply>(
'exhibits',
this.options.serverAPI
);
this.exhibits = data.exhibits;
const allStatusesKnown = this.exhibits.every(
exhibit =>
Expand Down
18 changes: 6 additions & 12 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ import { ServerConnection } from '@jupyterlab/services';
* @returns The response body interpreted as JSON
*/
export async function requestAPI<T>(
endPoint = '',
endPoint: string,
namespace: string,
init: RequestInit = {}
): Promise<T> {
// Make request to Jupyter API
const settings = ServerConnection.makeSettings();
const requestUrl = URLExt.join(
settings.baseUrl,
'jupyterlab-gallery', // API Namespace
endPoint
);
const requestUrl = URLExt.join(settings.baseUrl, namespace, endPoint);

let response: Response;
try {
Expand Down Expand Up @@ -67,14 +64,11 @@ export type IStreamMessage = IProgressStreamMessage | ITextStreamMessage;
export function eventStream(
endPoint = '',
onStream: (message: IStreamMessage) => void,
onError: (error: Event) => void
onError: (error: Event) => void,
namespace: string
): EventSource {
const settings = ServerConnection.makeSettings();
let requestUrl = URLExt.join(
settings.baseUrl,
'jupyterlab-gallery', // API Namespace
endPoint
);
let requestUrl = URLExt.join(settings.baseUrl, namespace, endPoint);
const xsrfTokenMatch = document.cookie.match('\\b_xsrf=([^;]*)\\b');
if (xsrfTokenMatch) {
const fullUrl = new URL(requestUrl);
Expand Down
23 changes: 13 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ const plugin: JupyterFrontEndPlugin<void> = {
) => {
console.log('JupyterLab extension jupyterlab-gallery is activated!');

let settings: ISettingRegistry.ISettings;
try {
settings = await settingRegistry.load(plugin.id);
console.log('jupyterlab-gallery settings loaded:', settings.composite);
} catch (reason) {
console.error('Failed to load settings for jupyterlab-gallery.', reason);
return;
}
const serverAPI = settings.composite.endpoint as string;

translator = translator ?? nullTranslator;
const trans = translator.load('jupyterlab-gallery');
const widget = new GalleryWidget({
Expand All @@ -51,10 +61,11 @@ const plugin: JupyterFrontEndPlugin<void> = {
fileChanged: app.serviceManager.contents.fileChanged,
refreshFileBrowser: () => {
return app.commands.execute('filebrowser:refresh');
}
},
serverAPI
});

const data = await requestAPI<IGalleryReply>('gallery');
const data = await requestAPI<IGalleryReply>('gallery', serverAPI);
const expectedVersion = '1.0';
if (data.apiVersion !== expectedVersion) {
console.warn(
Expand Down Expand Up @@ -90,14 +101,6 @@ const plugin: JupyterFrontEndPlugin<void> = {
widget.show();
app.shell.add(widget, 'left', { rank: 850 });
}

try {
const settings = await settingRegistry.load(plugin.id);
console.log('jupyterlab-gallery settings loaded:', settings.composite);
} catch (reason) {
console.error('Failed to load settings for jupyterlab-gallery.', reason);
return;
}
}
};

Expand Down

0 comments on commit 96c8b60

Please sign in to comment.