Skip to content

Commit 6720fbf

Browse files
committed
add woqlclientTools
1 parent 77c6953 commit 6720fbf

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

lib/connectionConfig.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,15 @@ ConnectionConfig.prototype.userURL = function(user) {
330330

331331
/**
332332
* Generate URL for the user's organization api endpoint
333-
* @param {string} [organization] - the organization id
333+
* @param {string} orgId - the organization id
334+
* @param {string} [action] - the organization id
334335
* @returns {string}
335336
*/
336337

337-
ConnectionConfig.prototype.organizationURL = function(organization) {
338+
ConnectionConfig.prototype.organizationURL = function(orgId, action) {
338339
let url = `${this.apiURL()}organization`
339-
if (organization) url += `/${encodeURIComponent(organization)}`
340+
if (orgId) url += `/${encodeURIComponent(orgId)}`
341+
if (action) url += `/${encodeURIComponent(action)}`
340342
return url
341343
}
342344

@@ -345,7 +347,7 @@ ConnectionConfig.prototype.organizationURL = function(organization) {
345347
* @returns {string}
346348
*/
347349
ConnectionConfig.prototype.rolesURL = function() {
348-
return `${this.apiURL()}role`
350+
return `${this.apiURL()}roles`
349351
}
350352

351353
/**

lib/dispatchRequest.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ function btoaImplementation(str) {
1919
* @file Dispatch Request
2020
* @license Apache Version 2
2121
* @description Functions for dispatching API requests via the axios library.
22-
* @param {String} url API endpoint URL
23-
* @param {String} action API action
24-
* @param {Object} payload data to be transmitted to endpoint
25-
* @param {String} key optional basic auth string to be passed
22+
* @param {string} url API endpoint URL
23+
* @param {string} action API action
24+
* @param {object} payload data to be transmitted to endpoint
25+
* @param {string} key optional basic auth string to be passed
26+
* @param {string} reqUID the unique reqID
2627
*/
27-
function DispatchRequest(url, action, payload, local_auth, remote_auth = null) {
28+
function DispatchRequest(url, action, payload, local_auth, remote_auth = null, reqUID = null) {
2829
/*
2930
*CORS is only required when trying to fetch data from a browser,
3031
*as browsers by default will block requests to different origins
@@ -68,6 +69,10 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null) {
6869
if (!options.headers) options.headers = {}
6970
options.headers['Authorization-Remote'] = `Basic ${rencoded}`
7071
}
72+
73+
if (reqUID) {
74+
options.headers['ReqUID'] = reqUID
75+
}
7176
switch (action) {
7277
case CONST.DELETE_DATABASE:
7378
case CONST.DELETE_CSV:
@@ -96,6 +101,7 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null) {
96101
case CONST.GET_CSV:
97102
case CONST.MESSAGE:
98103
case CONST.INFO:
104+
case CONST.GET_ROLES:
99105
if (payload) {
100106
const ext = UTILS.URIEncodePayload(payload)
101107
if (ext) url += `?${ext}`

lib/woqlClient.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const WOQLQuery = require('./query/woqlBuilder')
2424
* client.db("test")
2525
* client.checkout("dev")
2626
* const turtle = await client.getTriples("schema", "main")
27-
* //The client has an internal state which defines what
27+
* //The client has an internal state which defines what
2828
* //organization / database / repository / branch / ref it is currently attached to
2929
*/
3030

@@ -35,6 +35,10 @@ function WOQLClient(serverUrl, params) {
3535
this.connection = new ConnectionCapabilities()
3636
}
3737

38+
WOQLClient.prototype.reqUID = function(reqUID) {
39+
if (reqUID) this._reqUID = reqUID
40+
else return this._reqUID
41+
}
3842

3943
WOQLClient.CONST = CONST
4044

@@ -870,7 +874,14 @@ WOQLClient.prototype.dispatch = function(action, apiUrl, payload) {
870874
),
871875
)
872876
}
873-
return DispatchRequest(apiUrl, action, payload, this.localAuth(), this.remoteAuth())
877+
return DispatchRequest(
878+
apiUrl,
879+
action,
880+
payload,
881+
this.localAuth(),
882+
this.remoteAuth(),
883+
this.reqUID(),
884+
)
874885
}
875886

876887
/**
@@ -985,7 +996,6 @@ WOQLClient.prototype.getClassFrame = function(docType) {
985996
*/
986997

987998
WOQLClient.prototype.getDatabase = function() {
988-
//console.log('___DB__URL__', this.connectionConfig.dbURL())
989999
return this.dispatch(CONST.READ_DATABASE, this.connectionConfig.dbURL())
9901000
}
9911001

@@ -1122,13 +1132,17 @@ WOQLClient.prototype.createOrganization = function(orgId, orgDoc) {
11221132
}
11231133

11241134
/**
1125-
* Gets all the information about the given organization
1135+
* Gets all the information about the given organization
11261136
* @param {string} orgId - the organization id
1137+
* @param {string} [action] - set an action like recommendations | invitations | collaborators
11271138
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
11281139
*/
11291140

1130-
WOQLClient.prototype.getOrganization = function(orgId) {
1131-
return this.dispatch(CONST.READ_ORGANIZATION, this.connectionConfig.organizationURL(orgId))
1141+
WOQLClient.prototype.getOrganization = function(orgId, action) {
1142+
return this.dispatch(
1143+
CONST.READ_ORGANIZATION,
1144+
this.connectionConfig.organizationURL(orgId, action),
1145+
)
11321146
}
11331147

11341148
/**
@@ -1176,23 +1190,20 @@ WOQLClient.prototype.deleteOrganization = function(orgId) {
11761190
}
11771191

11781192
/**
1179-
* @param {string} userId - the user id
1193+
* get all the user roles (for the current logged user)
1194+
* or the user roles for a specific database and user
1195+
* (the logged used need to have the permission to see the roles info for another user)
1196+
* @param {string} [userId] - the user id
11801197
* @param {string} [orgId] - the organization id
11811198
* @param {string} [dbId] - the dbId
11821199
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
11831200
*/
11841201
WOQLClient.prototype.getRoles = function(userId, orgId, dbId) {
1185-
let payload = {}
1186-
if (userId) {
1187-
payload['agent_name'] = userId
1188-
}
1189-
if (dbId) {
1190-
payload['database_name'] = dbId
1191-
}
1192-
if (orgId) {
1193-
payload['organization_name'] = orgId
1202+
let roleURL = this.connectionConfig.rolesURL()
1203+
if (userId && orgId && dbId) {
1204+
roleURL = `${roleURL}/${userId}/${orgId}/${dbId}`
11941205
}
1195-
return this.dispatch(CONST.GET_ROLES, this.connectionConfig.rolesURL(), payload)
1206+
return this.dispatch(CONST.GET_ROLES, roleURL)
11961207
}
11971208

11981209
/**

lib/woqlClientTools.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const WOQLLibrary = require('./query/woqlLibrary')
22

33
function WOQLClientTools(client) {
4-
this.client = client
4+
this.client = client.copy()
55
this.woqlLibrary = new WOQLLibrary()
66
}
77

0 commit comments

Comments
 (0)