Skip to content

Commit 7223dc0

Browse files
committed
adding csv api
1 parent f7c0f7e commit 7223dc0

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

lib/connectionConfig.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,17 @@ ConnectionConfig.prototype.triplesURL = function(type, gid) {
285285
return s
286286
}
287287

288+
/**
289+
* Generate URL for add / get csv api endpoint
290+
*/
291+
ConnectionConfig.prototype.csvURL = function(type, gid) {
292+
var s = this.branchBase('csv')
293+
const graphId = gid || 'main'
294+
const gType = type || 'instance'
295+
s += `/${gType}/${graphId}`
296+
return s
297+
}
298+
288299
/**
289300
* Generate URL for woql query api endpoint
290301
*/

lib/const.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ module.exports = Object.freeze({
3434
REBASE: 'rebase',
3535
RESET: 'reset',
3636
BRANCH: 'branch',
37+
ADD_CSV: 'add_csv',
38+
GET_CSV: 'get_csv'
3739
})

lib/dispatchRequest.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function btoaImplementation(str) {
2424
* @param {Object} payload data to be transmitted to endpoint
2525
* @param {String} key optional basic auth string to be passed
2626
*/
27-
function DispatchRequest(url, action, payload, local_auth, remote_auth = null) {
27+
function DispatchRequest(url, action, payload, local_auth, remote_auth = null, files) {
2828
/*
2929
*CORS is only required when trying to fetch data from a browser,
3030
*as browsers by default will block requests to different origins
@@ -88,6 +88,22 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null) {
8888
}
8989
throw e
9090
})
91+
case CONST.ADD_CSV:
92+
options.headers = options.headers ? options.headers : {}
93+
options.headers['Content-Type'] = 'multipart/form-data'
94+
const formData = new FormData();
95+
files.map((item)=>{
96+
formData.append(item.name, item.file)
97+
})
98+
formData.append('payload', payload)
99+
return axiosInstance
100+
.put(url, formData, options)
101+
.then(response => response.data)
102+
.catch(err => {
103+
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err))
104+
if (err.response && err.response.data) e.data = err.response.data
105+
throw e
106+
})
91107
case CONST.INSERT_TRIPLES:
92108
options.headers = options.headers ? options.headers : {}
93109
options.headers['Content-Type'] = 'application/json'
@@ -98,7 +114,7 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null) {
98114
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err))
99115
if (err.response && err.response.data) e.data = err.response.data
100116
throw e
101-
})
117+
})
102118
default:
103119
options.headers = options.headers ? options.headers : {}
104120
options.headers['Content-Type'] = 'application/json'

lib/woqlClient.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ WOQLClient.prototype.resource = function(type, val) {
226226
* @param {object} config - see constructor for structure
227227
* @return {Promise}
228228
* @public
229-
*
229+
*
230230
*/
231231
WOQLClient.prototype.connect = function(config) {
232232
if (config) this.connectionConfig.update(config)
@@ -369,7 +369,7 @@ WOQLClient.prototype.updateTriples = function(gtype, gid, turtle, commit_msg) {
369369

370370

371371
/**
372-
* Appends the passed turtle to the contents of a graph
372+
* Appends the passed turtle to the contents of a graph
373373
*
374374
* @param {string} gtype type of graph |instance|schema|inference|
375375
* @param {string} gid TerminusDB Graph ID to update, main is the default value
@@ -393,6 +393,33 @@ WOQLClient.prototype.insertTriples = function(gtype, gid, turtle, commit_msg) {
393393
)
394394
}
395395

396+
397+
/**
398+
* Appends the passed turtle to the contents of a graph
399+
*
400+
* @param {string} gtype type of graph |instance|schema|inference|
401+
* @param {string} gid TerminusDB Graph ID to update, main is the default value
402+
* @param {string} csv is an array of csv file names
403+
* @param {string} commit_msg Textual message describing the reason for the update
404+
* @return {Promise}
405+
*/
406+
407+
//DispatchRequest(url, action, payload, local_auth, remote_auth = null, files) {
408+
WOQLClient.prototype.addCSV = function(gtype, gid, csv, commit_msg) {
409+
if (commit_msg && csv) {
410+
let commit = this.generateCommitInfo(commit_msg)
411+
return this.dispatch(
412+
CONST.ADD_CSV,
413+
this.connectionConfig.dbURL(),
414+
commit,
415+
files=csv
416+
)
417+
}
418+
}
419+
420+
421+
422+
396423
/**
397424
* Executes a WOQL query on the specified database and returns the results
398425
*
@@ -586,7 +613,7 @@ WOQLClient.prototype._load_db_prefixes = function(dbs) {
586613
ndb.prefix_pairs = {}
587614
}
588615
if(row['Prefix_'+j] && row['Prefix_'+j]['@value'] && row['IRI_'+j]["@value"]){
589-
ndb.prefix_pairs[row['Prefix_'+j]["@value"]] = row['IRI_'+j]["@value"]
616+
ndb.prefix_pairs[row['Prefix_'+j]["@value"]] = row['IRI_'+j]["@value"]
590617
}
591618
}
592619
}
@@ -598,8 +625,8 @@ WOQLClient.prototype._load_db_prefixes = function(dbs) {
598625
/**
599626
* Not yet released - subject to change in parameters
600627
* The class frame API endpoint is not yet sufficiently stable for release
601-
* It should be considered unreliable and subject to change in parameters until
602-
* it has been released
628+
* It should be considered unreliable and subject to change in parameters until
629+
* it has been released
603630
*/
604631

605632
/**
@@ -621,12 +648,12 @@ WOQLClient.prototype.getClassFrame = function(cls) {
621648

622649

623650
/***
624-
* Server Version API
651+
* Server Version API
625652
* Note: the below endpoints are not part of the terminusdb desktop package
626-
* they belong to the server package version of the DB which is under construction.
653+
* they belong to the server package version of the DB which is under construction.
627654
* Until that package is released all of the below endpoints should be considered
628655
* as unreliable and subject to change - they are provided complete with the desktop
629-
* version to show users what is coming and to allow people to use them at their own risk
656+
* version to show users what is coming and to allow people to use them at their own risk
630657
* Any use of them should be considered unsupported and at your own risk
631658
*/
632659

0 commit comments

Comments
 (0)