Skip to content

Commit 2b39521

Browse files
authored
Merge pull request #44 from multinet-app/s3ff
Reconfigure to enable file uploading using S3 File Field
2 parents 4e09788 + a956e4a commit 2b39521

4 files changed

Lines changed: 65 additions & 46 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"license": "Apache-2.0",
2626
"dependencies": {
2727
"axios": "^0.21.1",
28+
"django-s3-file-field": "^0.1.2",
2829
"ts-jest": "^26.5.4"
2930
},
3031
"devDependencies": {

src/axios.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import axios, { AxiosInstance, AxiosPromise, AxiosRequestConfig, AxiosResponse } from 'axios';
2+
import S3FileFieldClient, { S3FileFieldProgress, S3FileFieldProgressState } from 'django-s3-file-field';
23

34
import {
45
CreateGraphOptionsSpec,
56
TableMetadata,
6-
FileUploadOptionsSpec,
7+
TableUploadOptionsSpec,
8+
NetworkUploadOptionsSpec,
79
EdgesSpec,
810
EdgesOptionsSpec,
911
Graph,
@@ -51,10 +53,11 @@ export interface MultinetAxiosInstance extends AxiosInstance {
5153
createWorkspace(workspace: string): AxiosPromise<string>;
5254
deleteWorkspace(workspace: string): AxiosPromise<string>;
5355
renameWorkspace(workspace: string, name: string): AxiosPromise<any>;
54-
uploadTable(workspace: string, table: string, options: FileUploadOptionsSpec, config?: AxiosRequestConfig): AxiosPromise<Array<{}>>;
56+
uploadTable(workspace: string, table: string, options: TableUploadOptionsSpec, config?: AxiosRequestConfig): AxiosPromise<Array<{}>>;
5557
downloadTable(workspace: string, table: string): AxiosPromise<any>;
5658
deleteTable(workspace: string, table: string): AxiosPromise<string>;
5759
tableMetadata(workspace: string, table: string): AxiosPromise<TableMetadata>;
60+
uploadNetwork(workspace: string, network: string, options: NetworkUploadOptionsSpec, config?: AxiosRequestConfig): AxiosPromise<Array<{}>>;
5861
createGraph(workspace: string, graph: string, options: CreateGraphOptionsSpec): AxiosPromise<CreateGraphOptionsSpec>;
5962
deleteGraph(workspace: string, graph: string): AxiosPromise<string>;
6063
aql(workspace: string, query: string): AxiosPromise<any[]>;
@@ -144,38 +147,20 @@ export function multinetAxiosInstance(config: AxiosRequestConfig): MultinetAxios
144147
});
145148
};
146149

147-
Proto.uploadTable = async function(workspace: string, table: string, options: FileUploadOptionsSpec, cfg?: AxiosRequestConfig): Promise<AxiosResponse<Array<{}>>> {
148-
const headers = cfg ? cfg.headers : undefined;
149-
const params = cfg ? cfg.params : undefined;
150-
const { type, data, key, overwrite, columnTypes } = options;
151-
152-
let text;
153-
154-
if (typeof data === 'string') {
155-
text = data;
156-
} else {
157-
text = await fileToText(data);
158-
}
159-
160-
let metadata;
161-
if (columnTypes) {
162-
const columns = Object.keys(columnTypes).map((column) => ({
163-
key: column,
164-
type: columnTypes[column],
165-
}));
166-
167-
metadata = { columns };
168-
}
169-
170-
return this.post(`/${type}/${workspace}/${table}`, text, {
171-
...cfg,
172-
headers: { ...headers, 'Content-Type': 'text/plain' },
173-
params: {
174-
...params,
175-
key: key || undefined,
176-
overwrite: overwrite || undefined,
177-
metadata: metadata || undefined,
178-
},
150+
Proto.uploadTable = async function(workspace: string, table: string, options: TableUploadOptionsSpec): Promise<AxiosResponse<Array<{}>>> {
151+
const { data, edgeTable, columnTypes } = options;
152+
const s3ffClient = new S3FileFieldClient({
153+
baseUrl: `${this.defaults.baseURL}/s3-upload/`,
154+
apiConfig: this.defaults,
155+
});
156+
157+
const fieldValue = await s3ffClient.uploadFile(data, 'api.Upload.blob');
158+
159+
return this.post(`/workspaces/${workspace}/uploads/csv/`, {
160+
field_value: fieldValue.value,
161+
edge: edgeTable,
162+
table_name: table,
163+
columns: columnTypes
179164
});
180165
};
181166

@@ -191,6 +176,21 @@ export function multinetAxiosInstance(config: AxiosRequestConfig): MultinetAxios
191176
return this.get(`/workspaces/${workspace}/tables/${table}/metadata`);
192177
};
193178

179+
Proto.uploadNetwork = async function(workspace: string, network: string, options: NetworkUploadOptionsSpec): Promise<AxiosResponse<Array<{}>>> {
180+
const { type, data } = options;
181+
const s3ffClient = new S3FileFieldClient({
182+
baseUrl: `${this.defaults.baseURL}/s3-upload/`,
183+
apiConfig: this.defaults,
184+
});
185+
186+
const fieldValue = await s3ffClient.uploadFile(data, 'api.Upload.blob');
187+
188+
return this.post(`/workspaces/${workspace}/uploads/${type}/`, {
189+
field_value: fieldValue.value,
190+
network_name: network
191+
});
192+
};
193+
194194
Proto.createGraph = function(workspace: string, graph: string, options: CreateGraphOptionsSpec): AxiosPromise<CreateGraphOptionsSpec> {
195195
return this.post(`/workspaces/${workspace}/networks/`, {
196196
name: graph,

src/index.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { multinetAxiosInstance, MultinetAxiosInstance } from './axios';
22

3-
import axios, { AxiosRequestConfig } from 'axios';
4-
53
export interface Paginated<T> {
64
count: number,
75
next: string | null,
@@ -82,8 +80,16 @@ export type TableUploadType = 'csv';
8280
export type GraphUploadType = 'nested_json' | 'newick' | 'd3_json';
8381
export type UploadType = TableUploadType | GraphUploadType;
8482

83+
export function validTableUploadType(type: string): type is TableUploadType {
84+
return type === 'csv';
85+
}
86+
87+
export function validGraphUploadType(type: string): type is GraphUploadType {
88+
return ['nested_json', 'newick', 'd3_json'].includes(type);
89+
}
90+
8591
export function validUploadType(type: string): type is UploadType {
86-
return ['csv', 'nested_json', 'newick', 'd3_json'].includes(type);
92+
return validTableUploadType(type) || validGraphUploadType(type);
8793
}
8894

8995
export type Direction = 'all' | 'incoming' | 'outgoing';
@@ -119,16 +125,19 @@ export interface TableMetadata {
119125
};
120126
}
121127

122-
export interface FileUploadOptionsSpec {
123-
type: UploadType;
124-
data: string | File;
125-
key?: string;
126-
overwrite?: boolean;
128+
export interface TableUploadOptionsSpec {
129+
data: File;
130+
edgeTable: boolean;
127131
columnTypes?: {
128132
[key: string]: ColumnType;
129133
};
130134
}
131135

136+
export interface NetworkUploadOptionsSpec {
137+
data: File;
138+
type: GraphUploadType;
139+
}
140+
132141
export interface CreateGraphOptionsSpec {
133142
edgeTable: string;
134143
}
@@ -211,10 +220,8 @@ class MultinetAPI {
211220
return (await this.axios.renameWorkspace(workspace, name)).data;
212221
}
213222

214-
public async uploadTable(
215-
workspace: string, table: string, options: FileUploadOptionsSpec, config?: AxiosRequestConfig
216-
): Promise<Array<{}>> {
217-
return (await this.axios.uploadTable(workspace, table, options, config)).data;
223+
public async uploadTable(workspace: string, table: string, options: TableUploadOptionsSpec): Promise<Array<{}>> {
224+
return (await this.axios.uploadTable(workspace, table, options)).data;
218225
}
219226

220227
public async downloadTable(workspace: string, table: string): Promise<any> {
@@ -239,6 +246,10 @@ class MultinetAPI {
239246
return types;
240247
}
241248

249+
public async uploadNetwork(workspace: string, network: string, options: NetworkUploadOptionsSpec): Promise<Array<{}>> {
250+
return (await this.axios.uploadNetwork(workspace, network, options)).data;
251+
}
252+
242253
public async createGraph(workspace: string, graph: string, options: CreateGraphOptionsSpec): Promise<CreateGraphOptionsSpec> {
243254
return (await this.axios.createGraph(workspace, graph, options)).data;
244255
}

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,13 @@ diff@^4.0.1:
11851185
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
11861186
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
11871187

1188+
django-s3-file-field@^0.1.2:
1189+
version "0.1.2"
1190+
resolved "https://registry.yarnpkg.com/django-s3-file-field/-/django-s3-file-field-0.1.2.tgz#eeee3c4fcc9debca1c07f800d175fd8e559c076f"
1191+
integrity sha512-zo0YYIZCvH7efYFBKM44VtQLofLCe286dez7HnxB/gUTdbFfV0Zp9lBc+tQ9YVbpyJMLrCaVWvRqR7dkJUAVwg==
1192+
dependencies:
1193+
axios "^0.21.1"
1194+
11881195
domexception@^2.0.1:
11891196
version "2.0.1"
11901197
resolved "https://registry.yarnpkg.com/domexception/-/domexception-2.0.1.tgz#fb44aefba793e1574b0af6aed2801d057529f304"

0 commit comments

Comments
 (0)