Browse Source

Update services to include workspaces.

Afshin Darian 7 years ago
parent
commit
f4b8c7ca8e

+ 1 - 0
packages/services/src/index.ts

@@ -9,3 +9,4 @@ export * from './serverconnection';
 export * from './session';
 export * from './setting';
 export * from './terminal';
+export * from './workspace';

+ 10 - 0
packages/services/src/manager.ts

@@ -37,6 +37,10 @@ import {
   ServerConnection
 } from './serverconnection';
 
+import {
+  WorkspaceManager
+} from './workspace';
+
 
 /**
  * A Jupyter services manager.
@@ -56,6 +60,7 @@ class ServiceManager implements ServiceManager.IManager {
     this.settings = new SettingManager(options);
     this.terminals = new TerminalManager(options);
     this.builder = new BuildManager(options);
+    this.workspaces = new WorkspaceManager(options);
 
     this.sessions.specsChanged.connect((sender, specs) => {
       this._specsChanged.emit(specs);
@@ -135,6 +140,11 @@ class ServiceManager implements ServiceManager.IManager {
    */
   readonly terminals: TerminalManager;
 
+  /**
+   * Get the workspace manager instance.
+   */
+  readonly workspaces: WorkspaceManager;
+
   /**
    * Test whether the manager is ready.
    */

+ 3 - 3
packages/services/src/setting/index.ts

@@ -17,7 +17,7 @@ const SERVICE_SETTINGS_URL = 'api/settings';
 
 
 /**
- * The static namespace for `SettingManager`.
+ * The settings API service manager.
  */
 export
 class SettingManager {
@@ -47,7 +47,7 @@ class SettingManager {
     const { baseUrl, pageUrl } = serverSettings;
     const base = baseUrl + pageUrl;
     const url = Private.url(base, id);
-    const promise = ServerConnection.makeRequest(url, {}, serverSettings);
+    const promise = ServerConnection.makeRequest(url, { }, serverSettings);
 
     return promise.then(response => {
       if (response.status !== 200) {
@@ -84,7 +84,7 @@ class SettingManager {
         throw new ServerConnection.ResponseError(response);
       }
 
-      return void 0;
+      return undefined;
     });
   }
 }

+ 130 - 0
packages/services/src/workspace/index.ts

@@ -0,0 +1,130 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import {
+  URLExt
+} from '@jupyterlab/coreutils';
+
+import {
+  ReadonlyJSONObject
+} from '@phosphor/coreutils';
+
+import {
+  ServerConnection
+} from '../serverconnection';
+
+
+/**
+ * The url for the lab workspaces service.
+ */
+const SERVICE_WORKSPACES_URL = 'api/workspaces';
+
+
+/**
+ * The workspaces API service manager.
+ */
+export
+class WorkspaceManager {
+  /**
+   * Create a new workspace manager.
+   */
+  constructor(options: WorkspaceManager.IOptions = { }) {
+    this.serverSettings = options.serverSettings ||
+      ServerConnection.makeSettings();
+  }
+
+  /**
+   * The server settings used to make API requests.
+   */
+  readonly serverSettings: ServerConnection.ISettings;
+
+  /**
+   * Fetch a workspace.
+   *
+   * @param id - The workspaces's ID.
+   *
+   * @returns A promise that resolves with the workspace or rejects with a
+   * `ServerConnection.IError`.
+   */
+  fetch(id: string): Promise<Workspace.IWorkspace> {
+    const { serverSettings } = this;
+    const { baseUrl, pageUrl } = serverSettings;
+    const base = baseUrl + pageUrl;
+    const url = Private.url(base, id);
+    const promise = ServerConnection.makeRequest(url, { }, serverSettings);
+
+    return promise.then(response => {
+      if (response.status !== 200) {
+        throw new ServerConnection.ResponseError(response);
+      }
+
+      return response.json();
+    });
+  }
+}
+
+
+/**
+ * A namespace for `WorkspaceManager` statics.
+ */
+export
+namespace WorkspaceManager {
+  /**
+   * The instantiation options for a workspace manager.
+   */
+  export
+  interface IOptions {
+    /**
+     * The server settings used to make API requests.
+     */
+    serverSettings?: ServerConnection.ISettings;
+  }
+}
+
+
+/**
+ * A namespace for workspace API interfaces.
+ */
+export
+namespace Workspace {
+  /**
+   * The interface for the workspace API manager.
+   */
+  export
+  interface IManager extends WorkspaceManager { }
+
+  /**
+   * The interface describing a workspace API response.
+   */
+  export
+  interface IWorkspace {
+    /**
+     * The workspace data.
+     */
+    data: ReadonlyJSONObject;
+
+    /**
+     * The metadata for a workspace.
+     */
+    metadata: {
+      /**
+       * The workspace ID.
+       */
+      id: string;
+    };
+  }
+}
+
+
+/**
+ * A namespace for private data.
+ */
+namespace Private {
+  /**
+   * Get the url for a workspace.
+   */
+  export
+  function url(base: string, id: string): string {
+    return URLExt.join(base, SERVICE_WORKSPACES_URL, id);
+  }
+}