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

Commit

Permalink
Merge pull request #184 from multinet-app/multinetjs
Browse files Browse the repository at this point in the history
Introduce Multinet client library
  • Loading branch information
waxlamp committed Oct 21, 2019
2 parents 33b2686 + 2c9dd13 commit a15be45
Show file tree
Hide file tree
Showing 15 changed files with 1,191 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@ jobs:
with:
python-version: '3.7'
architecture: 'x64'

# Build and test Multinet server.
- run: pip install pipenv
- run: pipenv install --dev --deploy
- run: pipenv run lint
- run: pipenv run format
- run: pipenv run typecheck
- run: pipenv run test

# Build and test Multinet client app.
- uses: actions/setup-node@v1
with:
node-version: '10.x'
- run: cd client && yarn install
- run: cd client && yarn lint

# Build and test Multinet client library.
- run: cd multinetjs && yarn install
- run: cd multinetjs && yarn build
- run: cd multinetjs && yarn lint
- run: cd multinetjs && yarn test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ ENV/
# Development / platform
.vscode/
.DS_Store

# Arango test materials.
arango/
arango-apps/
server.out
server.pid
3 changes: 3 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ allow_prereleases = true
serve = "flask run -p ${FLASK_SERVE_PORT}"
build-docs = "make html -C docs"
serve-docs = "python -m http.server ${DOC_SERVE_PORT} -d docs/_build/html"
test-server-up = "sh ./scripts/server/start.sh"
test-server-down = "sh ./scripts/server/stop.sh"
test-server-clean = "sh ./scripts/server/clean.sh"
lint = "flake8"
test = "pytest -v -W ignore::DeprecationWarning test"
typecheck = "mypy -p multinet --disallow-untyped-defs"
Expand Down
2 changes: 1 addition & 1 deletion multinet/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def count_text(filt: str) -> str:
"""

if direction == "all":
filter_clause = f'e._from == "{table}/{node}" || e._to == "{node}"'
filter_clause = f'e._from == "{table}/{node}" || e._to == "{table}/{node}"'
elif direction == "incoming":
filter_clause = f'e._to == "{table}/{node}"'
elif direction == "outgoing":
Expand Down
1 change: 1 addition & 0 deletions multinetjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
32 changes: 32 additions & 0 deletions multinetjs/package.json
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"
}
}
35 changes: 35 additions & 0 deletions multinetjs/src/client.ts
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);
});
});
}
}
105 changes: 105 additions & 0 deletions multinetjs/src/index.ts
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);
}

0 comments on commit a15be45

Please sign in to comment.