Skip to content

Commit 73bb0d0

Browse files
authored
Use serverSettings to make API requests (#1437)
* Use `IServerSettings` to make API requests * update mock * use serverSettings directly * fix * tests * cleanup
1 parent a22ec8b commit 73bb0d0

File tree

6 files changed

+123
-62
lines changed

6 files changed

+123
-62
lines changed

src/__tests__/plugin.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ISettingRegistry, SettingRegistry } from '@jupyterlab/settingregistry';
66
import { JupyterLab } from '@jupyterlab/application';
77
import { showErrorMessage } from '@jupyterlab/apputils';
88
import { URLExt } from '@jupyterlab/coreutils';
9+
import { ServerConnection } from '@jupyterlab/services';
910
import { Signal } from '@lumino/signaling';
1011
import {
1112
defaultMockedResponses,
@@ -30,6 +31,10 @@ describe('plugin', () => {
3031

3132
beforeAll(() => {
3233
app = new JupyterLab() as jest.Mocked<JupyterLab>;
34+
// Add the serviceManager.serverSettings mock
35+
(app as any).serviceManager = {
36+
serverSettings: ServerConnection.makeSettings()
37+
};
3338
browserFactory = {
3439
defaultBrowser: {
3540
model: { pathChanged: new Signal(null), restored: Promise.resolve() }

src/__tests__/utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ServerConnection } from '@jupyterlab/services';
12
import { ReadonlyJSONObject } from '@lumino/coreutils';
23
import { Git } from '../tokens';
34

@@ -44,8 +45,7 @@ export const defaultMockedResponses: {
4445
stash: {
4546
body: () => ({
4647
code: 0,
47-
message: '',
48-
command: ''
48+
stashes: []
4949
})
5050
},
5151
status: {
@@ -68,13 +68,15 @@ export function mockedRequestAPI(
6868
endPoint?: string,
6969
method?: string,
7070
body?: ReadonlyJSONObject | null,
71-
namespace?: string
71+
namespace?: string,
72+
serverSettings?: ServerConnection.ISettings
7273
) => Promise<any> {
7374
const mockedImplementation = (
7475
url?: string,
7576
method?: string,
7677
body?: ReadonlyJSONObject | null,
77-
namespace?: string
78+
namespace?: string,
79+
serverSettings?: ServerConnection.ISettings
7880
) => {
7981
mockedResponses = mockedResponses ?? {};
8082
const path = mockedResponses.path ?? DEFAULT_REPOSITORY_PATH;

src/git.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const AUTH_ERROR_MESSAGES = [
2020
* @param method HTML method; default 'GET'
2121
* @param body JSON object to be passed as body or null; default null
2222
* @param namespace API namespace; default 'git'
23+
* @param serverSettings Optional server connection settings; if not provided, uses ServerConnection.makeSettings()
2324
* @returns The response body interpreted as JSON
2425
*
2526
* @throws {Git.GitResponseError} If the server response is not ok
@@ -29,10 +30,11 @@ export async function requestAPI<T>(
2930
endPoint = '',
3031
method = 'GET',
3132
body: Partial<ReadonlyJSONObject> | null = null,
32-
namespace = 'git'
33+
namespace = 'git',
34+
serverSettings?: ServerConnection.ISettings
3335
): Promise<T> {
3436
// Make request to Jupyter API
35-
const settings = ServerConnection.makeSettings();
37+
const settings = serverSettings ?? ServerConnection.makeSettings();
3638
const requestUrl = URLExt.join(
3739
settings.baseUrl,
3840
namespace, // API Namespace

src/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async function activate(
8686
translator: ITranslator | null
8787
): Promise<IGitExtension> {
8888
let settings: ISettingRegistry.ISettings | undefined = undefined;
89-
let serverSettings: Git.IServerSettings;
89+
let gitServerSettings: Git.IServerSettings;
9090
translator = translator ?? nullTranslator;
9191
const trans = translator.load('jupyterlab_git');
9292

@@ -98,9 +98,10 @@ async function activate(
9898
trans.__('Failed to load settings for the Git Extension.\n%1', error)
9999
);
100100
}
101+
const serverSettings = app.serviceManager.serverSettings;
101102
try {
102-
serverSettings = await getServerSettings(trans);
103-
const { frontendVersion, gitVersion, serverVersion } = serverSettings;
103+
gitServerSettings = await getServerSettings(trans, serverSettings);
104+
const { frontendVersion, gitVersion, serverVersion } = gitServerSettings;
104105

105106
// Version validation
106107
if (!gitVersion) {
@@ -145,7 +146,12 @@ async function activate(
145146
return null;
146147
}
147148
// Create the Git model
148-
const gitExtension = new GitExtension(docmanager, app.docRegistry, settings);
149+
const gitExtension = new GitExtension(
150+
docmanager,
151+
app.docRegistry,
152+
settings,
153+
serverSettings
154+
);
149155

150156
const onPathChanged = (
151157
model: FileBrowserModel,

0 commit comments

Comments
 (0)