This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from multinet-app/multinetjs
Introduce Multinet client library
- Loading branch information
Showing
15 changed files
with
1,191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "multinet", | ||
"version": "0.1.0", | ||
"description": "Multinet client library", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"start": "tsc -w", | ||
"build": "tsc", | ||
"lint": "tslint -p tsconfig.json -c tslint.json", | ||
"lint:fix": "tslint --force -p tsconfig.json -c tslint.json", | ||
"test:server:up": "cd .. && PIPENV_DONT_LOAD_ENV=1 pipenv run test-server-up", | ||
"test:server:down": "cd .. && PIPENV_DONT_LOAD_ENV=1 pipenv run test-server-down", | ||
"test:server:clean": "cd .. && PIPENV_DONT_LOAD_ENV=1 pipenv run test-server-clean", | ||
"test:server:restart": "yarn test:server:down; yarn test:server:clean && yarn test:server:up", | ||
"test:cmd": "tape -r esm test/**/*.test.js | tap-spec", | ||
"test": "yarn test:server:restart && yarn test:cmd; code=$?; yarn test:server:down; if [ ${code} = 0 ]; then true; else false; fi" | ||
}, | ||
"author": "Kitware, Inc.", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"axios": "^0.19.0" | ||
}, | ||
"devDependencies": { | ||
"esm": "^3.2.25", | ||
"tap-spec": "^5.0.0", | ||
"tape": "^4.11.0", | ||
"tslib": "^1.10.0", | ||
"tslint": "^5.20.0", | ||
"typescript": "^3.6.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import axios, { AxiosInstance } from 'axios'; | ||
|
||
export class Client { | ||
private axios: AxiosInstance; | ||
|
||
constructor(baseURL: string) { | ||
this.axios = axios.create({ | ||
baseURL, | ||
}); | ||
} | ||
|
||
public get(path: string, params: {} = {}): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
this.axios.get(path, { params, }) | ||
.then((resp) => { | ||
resolve(resp.data); | ||
}) | ||
.catch((resp) => { | ||
reject(resp.response); | ||
}); | ||
}); | ||
} | ||
|
||
public post(path: string, params: {} = {}, headers: {} = {}): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
this.axios.post(path, params, { headers, }) | ||
.then((resp) => { | ||
resolve(resp.data); | ||
}) | ||
.catch((resp) => { | ||
reject(resp.response); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { Client } from './client'; | ||
|
||
export interface GraphSpec { | ||
edgeTable: string; | ||
nodeTables: string[]; | ||
} | ||
|
||
export interface NodesSpec { | ||
count: number; | ||
nodes: string[]; | ||
} | ||
|
||
export interface EdgesSpec { | ||
count: number; | ||
edges: string[]; | ||
} | ||
|
||
export type TableType = 'csv' | 'nested_json' | 'newick'; | ||
|
||
export type Direction = 'all' | 'incoming' | 'outgoing'; | ||
|
||
class MultinetAPI { | ||
private client: Client; | ||
|
||
constructor(baseURL: string) { | ||
this.client = new Client(baseURL); | ||
} | ||
|
||
public workspaces(): Promise<string[]> { | ||
return this.client.get('workspaces'); | ||
} | ||
|
||
public workspace(workspace: string): Promise<string> { | ||
if (!workspace) { | ||
throw new Error('argument "workspace" must not be empty'); | ||
} | ||
|
||
return this.client.get(`workspaces/${workspace}`); | ||
} | ||
|
||
public tables(workspace: string): Promise<string[]> { | ||
return this.client.get(`workspaces/${workspace}/tables`); | ||
} | ||
|
||
public table(workspace: string, table: string, offset: number = 0, limit: number = 30): Promise<Array<{}>> { | ||
return this.client.get(`workspaces/${workspace}/tables/${table}`, { | ||
offset, | ||
limit, | ||
}); | ||
} | ||
|
||
public graphs(workspace: string): Promise<string[]> { | ||
return this.client.get(`workspaces/${workspace}/graphs`); | ||
} | ||
|
||
public graph(workspace: string, graph: string): Promise<GraphSpec> { | ||
return this.client.get(`workspaces/${workspace}/graphs/${graph}`); | ||
} | ||
|
||
public nodes(workspace: string, graph: string, offset: number = 0, limit: number = 30): Promise<NodesSpec> { | ||
return this.client.get(`workspaces/${workspace}/graphs/${graph}/nodes`, { | ||
offset, | ||
limit, | ||
}); | ||
} | ||
|
||
public attributes(workspace: string, graph: string, nodeId: string): Promise<{}> { | ||
return this.client.get(`workspaces/${workspace}/graphs/${graph}/nodes/${nodeId}/attributes`); | ||
} | ||
|
||
public edges( | ||
workspace: string, | ||
graph: string, | ||
nodeId: string, | ||
direction: Direction = 'all', | ||
offset: number = 0, | ||
limit: number = 30): Promise<EdgesSpec> { | ||
return this.client.get(`workspaces/${workspace}/graphs/${graph}/nodes/${nodeId}/edges`, { | ||
direction, | ||
offset, | ||
limit, | ||
}); | ||
} | ||
|
||
public createWorkspace(workspace: string): Promise<string> { | ||
return this.client.post(`/workspaces/${workspace}`); | ||
} | ||
|
||
public uploadTable(type: TableType, workspace: string, table: string, data: string): Promise<Array<{}>> { | ||
return this.client.post(`/${type}/${workspace}/${table}`, data, { | ||
'Content-Type': 'text/plain', | ||
}); | ||
} | ||
|
||
public createGraph(workspace: string, graph: string, nodeTables: string[], edgeTable: string): Promise<string> { | ||
return this.client.post(`/workspaces/${workspace}/graph/${graph}`, { | ||
node_tables: nodeTables, | ||
edge_table: edgeTable, | ||
}); | ||
} | ||
} | ||
|
||
export function multinetApi(baseURL: string): MultinetAPI { | ||
return new MultinetAPI(baseURL); | ||
} |
Oops, something went wrong.