Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #202 from blink1073/use-url
Browse files Browse the repository at this point in the history
Clean up utils and use external url functions
  • Loading branch information
afshin authored Jul 26, 2016
2 parents 0faeb5b + c2b7340 commit fe678ed
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 177 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"path-posix": "^1.0.0",
"phosphor-disposable": "^1.0.5",
"phosphor-signaling": "^1.2.0",
"requirejs": "^2.2.0"
"requirejs": "^2.2.0",
"url": "^0.11.0",
"url-join": "^1.1.0"
},
"devDependencies": {
"expect.js": "^0.3.1",
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class ConfigSection implements IConfigSection {
let baseUrl = options.baseUrl || utils.getBaseUrl();
this.ajaxSettings = options.ajaxSettings || {};
this._url = utils.urlPathJoin(baseUrl, SERVICE_CONFIG_URL,
utils.urlJoinEncode(options.name));
encodeURIComponent(options.name));
}

/**
Expand Down
18 changes: 11 additions & 7 deletions src/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
import * as posix
from 'path-posix';

import {
JSONObject
} from './json';

import * as utils
from './utils';

Expand Down Expand Up @@ -110,7 +114,7 @@ namespace IContents {
* The options used to fetch a file.
*/
export
interface IFetchOptions {
interface IFetchOptions extends JSONObject {
/**
* The override file type for the request.
*/
Expand All @@ -133,7 +137,7 @@ namespace IContents {
* The options used to create a file.
*/
export
interface ICreateOptions {
interface ICreateOptions extends JSONObject {
/**
* The directory in which to create the file.
*/
Expand Down Expand Up @@ -352,7 +356,7 @@ class ContentsManager implements IContents.IManager {
if (options.type === 'notebook') {
delete options['format'];
}
let params = utils.copy(options);
let params: any = utils.copy(options);
params.content = options.content ? '1' : '0';
url += utils.jsonToQueryString(params);
}
Expand All @@ -377,7 +381,8 @@ class ContentsManager implements IContents.IManager {
* path if necessary.
*/
getDownloadUrl(path: string): string {
return utils.urlPathJoin(this._baseUrl, FILES_URL, utils.encodeURIComponents(path));
return utils.urlPathJoin(this._baseUrl, FILES_URL,
utils.urlEncodeParts(path));
}

/**
Expand Down Expand Up @@ -653,10 +658,9 @@ class ContentsManager implements IContents.IManager {
* Get a REST url for a file given a path.
*/
private _getUrl(...args: string[]): string {
let urlParts = [].concat(
Array.prototype.slice.apply(args));
let parts = args.map(path => utils.urlEncodeParts(path));
return utils.urlPathJoin(this._baseUrl, SERVICE_CONTENTS_URL,
utils.urlJoinEncode.apply(null, urlParts));
...parts);
}

private _baseUrl = '';
Expand Down
2 changes: 1 addition & 1 deletion src/ikernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ namespace IKernel {
* The options object used to initialize a kernel.
*/
export
interface IOptions {
interface IOptions extends JSONObject {
/**
* The kernel type (e.g. python3).
*/
Expand Down
2 changes: 1 addition & 1 deletion src/isession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ namespace ISession {
* The session initialization options.
*/
export
interface IOptions {
interface IOptions extends JSONObject {
/**
* The path (not including name) to the session.
*/
Expand Down
39 changes: 20 additions & 19 deletions src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,10 @@ function findKernelById(id: string, options?: IKernel.IOptions): Promise<IKernel
* Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernelspecs).
*/
export
function getKernelSpecs(options?: IKernel.IOptions): Promise<IKernel.ISpecModels> {
options = options || {};
function getKernelSpecs(options: IKernel.IOptions = {}): Promise<IKernel.ISpecModels> {
let baseUrl = options.baseUrl || utils.getBaseUrl();
let url = utils.urlPathJoin(baseUrl, KERNELSPEC_SERVICE_URL);
let ajaxSettings = utils.copy(options.ajaxSettings) || {};
let ajaxSettings: IAjaxSettings = utils.copy(options.ajaxSettings || {});
ajaxSettings.method = 'GET';
ajaxSettings.dataType = 'json';

Expand Down Expand Up @@ -281,11 +280,10 @@ function getKernelSpecs(options?: IKernel.IOptions): Promise<IKernel.ISpecModels
* The promise is fulfilled on a valid response and rejected otherwise.
*/
export
function listRunningKernels(options?: IKernel.IOptions): Promise<IKernel.IModel[]> {
options = options || {};
function listRunningKernels(options: IKernel.IOptions = {}): Promise<IKernel.IModel[]> {
let baseUrl = options.baseUrl || utils.getBaseUrl();
let url = utils.urlPathJoin(baseUrl, KERNEL_SERVICE_URL);
let ajaxSettings = utils.copy(options.ajaxSettings) || {};
let ajaxSettings: IAjaxSettings = utils.copy(options.ajaxSettings || {});
ajaxSettings.method = 'GET';
ajaxSettings.dataType = 'json';
ajaxSettings.cache = false;
Expand Down Expand Up @@ -322,7 +320,7 @@ function startNewKernel(options?: IKernel.IOptions): Promise<IKernel> {
options = options || {};
let baseUrl = options.baseUrl || utils.getBaseUrl();
let url = utils.urlPathJoin(baseUrl, KERNEL_SERVICE_URL);
let ajaxSettings = utils.copy(options.ajaxSettings) || {};
let ajaxSettings: IAjaxSettings = utils.copy(options.ajaxSettings || {});
ajaxSettings.method = 'POST';
ajaxSettings.data = JSON.stringify({ name: options.name });
ajaxSettings.dataType = 'json';
Expand Down Expand Up @@ -362,7 +360,7 @@ function connectToKernel(id: string, options?: IKernel.IOptions): Promise<IKerne
return Promise.resolve(kernel.clone());
}
}
return Private.getKernelModel(utils.urlJoinEncode(id), options).then(model => {
return Private.getKernelModel(id, options).then(model => {
return new Kernel(options, id);
}).catch(() => {
return Private.typedThrow<IKernel>(`No running kernel with id: ${id}`);
Expand Down Expand Up @@ -543,7 +541,7 @@ class Kernel implements IKernel {
* Clone the current kernel with a new clientId.
*/
clone(): IKernel {
let options = {
let options: IKernel.IOptions = {
baseUrl: this._baseUrl,
wsUrl: this._wsUrl,
name: this._name,
Expand Down Expand Up @@ -799,7 +797,7 @@ class Kernel implements IKernel {
username: this._username,
session: this._clientId
};
let defaults = {
let defaults: JSONObject = {
silent : false,
store_history : true,
user_expressions : {},
Expand Down Expand Up @@ -962,7 +960,9 @@ class Kernel implements IKernel {
return Promise.resolve(this._spec);
}
let name = this.name;
let options = { baseUrl: this._baseUrl, ajaxSettings: this._ajaxSettings };
let options: IKernel.IOptions = {
baseUrl: this._baseUrl, ajaxSettings: this.ajaxSettings
};
return getKernelSpecs(options).then(ids => {
let id = ids.kernelspecs[name];
if (!id) {
Expand All @@ -978,14 +978,14 @@ class Kernel implements IKernel {
*/
private _createSocket(): void {
let partialUrl = utils.urlPathJoin(this._wsUrl, KERNEL_SERVICE_URL,
utils.urlJoinEncode(this._id));
encodeURIComponent(this._id));
// Strip any authentication from the display string.
let display = partialUrl.replace(/^((?:\w+:)?\/\/)(?:[^@\/]+@)/, '$1');
console.log('Starting WebSocket:', display);

let url = (
utils.urlPathJoin(partialUrl, 'channels') +
'?session_id=' + this._clientId
let url = utils.urlPathJoin(
partialUrl,
'channels?session_id=' + encodeURIComponent(this._clientId)
);

this._connectionPromise = new utils.PromiseDelegate<void>();
Expand Down Expand Up @@ -1513,7 +1513,7 @@ namespace Private {
}
let url = utils.urlPathJoin(
baseUrl, KERNEL_SERVICE_URL,
utils.urlJoinEncode(kernel.id, 'restart')
encodeURIComponent(kernel.id), 'restart'
);
ajaxSettings = ajaxSettings || { };
ajaxSettings.method = 'POST';
Expand All @@ -1539,7 +1539,7 @@ namespace Private {
}
let url = utils.urlPathJoin(
baseUrl, KERNEL_SERVICE_URL,
utils.urlJoinEncode(kernel.id, 'interrupt')
encodeURIComponent(kernel.id), 'interrupt'
);
ajaxSettings = ajaxSettings || { };
ajaxSettings.method = 'POST';
Expand All @@ -1560,7 +1560,7 @@ namespace Private {
export
function shutdownKernel(id: string, baseUrl: string, ajaxSettings?: IAjaxSettings): Promise<void> {
let url = utils.urlPathJoin(baseUrl, KERNEL_SERVICE_URL,
utils.urlJoinEncode(id));
encodeURIComponent(id));
ajaxSettings = ajaxSettings || { };
ajaxSettings.method = 'DELETE';
ajaxSettings.dataType = 'json';
Expand All @@ -1580,7 +1580,8 @@ namespace Private {
function getKernelModel(id: string, options?: IKernel.IOptions): Promise<IKernel.IModel> {
options = options || {};
let baseUrl = options.baseUrl || utils.getBaseUrl();
let url = utils.urlPathJoin(baseUrl, KERNEL_SERVICE_URL, id);
let url = utils.urlPathJoin(baseUrl, KERNEL_SERVICE_URL,
encodeURIComponent(id));
let ajaxSettings = options.ajaxSettings || {};
ajaxSettings.method = 'GET';
ajaxSettings.dataType = 'json';
Expand Down
12 changes: 10 additions & 2 deletions src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import {
ISession
} from './isession';

import {
JSONObject
} from './json';

import {
KernelManager, getKernelSpecs
} from './kernel';
Expand Down Expand Up @@ -131,7 +135,11 @@ function createServiceManager(options: IServiceManager.IOptions = {}): Promise<I
if (options.kernelspecs) {
return Promise.resolve(new ServiceManager(options));
}
return getKernelSpecs(options.baseUrl).then(specs => {
let kernelOptions: IKernel.IOptions = {
baseUrl: options.baseUrl,
ajaxSettings: options.ajaxSettings
};
return getKernelSpecs(kernelOptions).then(specs => {
options.kernelspecs = specs;
return new ServiceManager(options);
});
Expand All @@ -147,7 +155,7 @@ class ServiceManager implements IServiceManager {
* Construct a new services provider.
*/
constructor(options: IServiceManager.IOptions) {
let subOptions = {
let subOptions: JSONObject = {
baseUrl: options.baseUrl,
ajaxSettings: options.ajaxSettings
};
Expand Down
6 changes: 3 additions & 3 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function listRunningSessions(options?: ISession.IOptions): Promise<ISession.IMod
options = options || {};
let baseUrl = options.baseUrl || utils.getBaseUrl();
let url = utils.urlPathJoin(baseUrl, SESSION_SERVICE_URL);
let ajaxSettings = utils.copy(options.ajaxSettings) || {};
let ajaxSettings: IAjaxSettings = utils.copy(options.ajaxSettings || {});
ajaxSettings.method = 'GET';
ajaxSettings.dataType = 'json';
ajaxSettings.cache = false;
Expand Down Expand Up @@ -770,7 +770,7 @@ namespace Private {
kernel: { name: options.kernelName, id: options.kernelId },
notebook: { path: options.path }
};
let ajaxSettings = utils.copy(options.ajaxSettings) || {};
let ajaxSettings: IAjaxSettings = utils.copy(options.ajaxSettings || {});
ajaxSettings.method = 'POST';
ajaxSettings.dataType = 'json';
ajaxSettings.data = JSON.stringify(model);
Expand All @@ -792,7 +792,7 @@ namespace Private {
*/
export
function createKernel(model: ISession.IModel, options: ISession.IOptions): Promise<IKernel> {
let kernelOptions = {
let kernelOptions: IKernel.IOptions = {
name: model.kernel.name,
baseUrl: options.baseUrl || utils.getBaseUrl(),
wsUrl: options.wsUrl,
Expand Down
16 changes: 10 additions & 6 deletions src/terminals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
JSONPrimitive, JSONObject, deepEqual
} from './json';

import {
IAjaxSettings
} from './utils';

import * as utils
from './utils';

Expand Down Expand Up @@ -70,7 +74,7 @@ namespace ITerminalSession {
* The options for intializing a terminal session object.
*/
export
interface IOptions {
interface IOptions extends JSONObject {
/**
* The name of the terminal.
*/
Expand Down Expand Up @@ -188,7 +192,7 @@ class TerminalManager implements ITerminalSession.IManager {
constructor(options: TerminalManager.IOptions = {}) {
this._baseUrl = options.baseUrl || utils.getBaseUrl();
this._wsUrl = options.wsUrl || utils.getWsUrl(this._baseUrl);
this._ajaxSettings = utils.copy(options.ajaxSettings) || {};
this._ajaxSettings = utils.copy(options.ajaxSettings || {});
}

/**
Expand Down Expand Up @@ -237,7 +241,7 @@ class TerminalManager implements ITerminalSession.IManager {
*/
shutdown(name: string): Promise<void> {
let url = utils.urlPathJoin(this._baseUrl, TERMINAL_SERVICE_URL, name);
let ajaxSettings = utils.copy(this._ajaxSettings) || {};
let ajaxSettings: IAjaxSettings = utils.copy(this._ajaxSettings || {});
ajaxSettings.method = 'DELETE';

return utils.ajaxRequest(url, ajaxSettings).then(success => {
Expand All @@ -252,7 +256,7 @@ class TerminalManager implements ITerminalSession.IManager {
*/
listRunning(): Promise<ITerminalSession.IModel[]> {
let url = utils.urlPathJoin(this._baseUrl, TERMINAL_SERVICE_URL);
let ajaxSettings = utils.copy(this._ajaxSettings) || {};
let ajaxSettings: IAjaxSettings = utils.copy(this._ajaxSettings || {});
ajaxSettings.method = 'GET';
ajaxSettings.dataType = 'json';

Expand Down Expand Up @@ -392,7 +396,7 @@ class TerminalSession implements ITerminalSession {
*/
shutdown(): Promise<void> {
let url = utils.urlPathJoin(this._baseUrl, TERMINAL_SERVICE_URL, this._name);
let ajaxSettings = utils.copy(this._ajaxSettings);
let ajaxSettings: IAjaxSettings = utils.copy(this._ajaxSettings);
ajaxSettings.method = 'DELETE';

return utils.ajaxRequest(url, ajaxSettings).then(success => {
Expand Down Expand Up @@ -421,7 +425,7 @@ class TerminalSession implements ITerminalSession {
*/
private _getName(): Promise<string> {
let url = utils.urlPathJoin(this._baseUrl, TERMINAL_SERVICE_URL);
let ajaxSettings = utils.copy(this._ajaxSettings);
let ajaxSettings: IAjaxSettings = utils.copy(this._ajaxSettings);
ajaxSettings.method = 'POST';
ajaxSettings.dataType = 'json';

Expand Down
2 changes: 2 additions & 0 deletions src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/// <reference path="../typings/requirejs/r.d.ts"/>
/// <reference path="../typings/path-posix/path-posix.d.ts"/>
/// <reference path="../typings/text-encoding/text-encoding.d.ts"/>
/// <reference path="../typings/url/url.d.ts"/>
/// <reference path="../typings/url-join/url-join.d.ts"/>
Loading

0 comments on commit fe678ed

Please sign in to comment.