Skip to content

Commit 3306198

Browse files
anu3990SameeraPriyathamTadikonda
authored andcommitted
DEVEXP-415 : Call any REST endpoint with Node Client
1 parent 6ca367d commit 3306198

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

lib/internal.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2023 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
const Operation = require("./operation");
19+
const requester = require("./requester.js");
20+
const mlutil = require("./mlutil");
21+
let requestOptions;
22+
let clientObject;
23+
24+
function intialize(client){
25+
clientObject = client;
26+
requestOptions = mlutil.copyProperties(clientObject.getConnectionParams());
27+
return this;
28+
}
29+
30+
function sendRequest(userOptions, optionalName, requestType='single', responseType='single', validStatusCodes = [200, 201, 204, 404]){
31+
if(!userOptions.path){
32+
throw new Error('Path is needed to send request.');
33+
}
34+
mlutil.copyProperties(userOptions,requestOptions);
35+
let operation = new Operation(
36+
optionalName?optionalName:'', clientObject, requestOptions, requestType, responseType
37+
);
38+
operation.validStatusCodes = validStatusCodes;
39+
return requester.startRequest(operation);
40+
}
41+
42+
function newRequestOptions(path){
43+
let userOptions = {};
44+
userOptions.path = mlutil.databaseParam(clientObject.getConnectionParams(), path, '?');
45+
return userOptions;
46+
}
47+
48+
module.exports = {
49+
intialize: intialize,
50+
sendRequest: sendRequest,
51+
newRequestOptions: newRequestOptions
52+
};

lib/marklogic.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var planBuilder = require('./plan-builder.js');
3939
var ctsQueryBuilder = require('./ctsquery-builder');
4040
var Operation = require('./operation.js');
4141
var requester = require('./requester.js');
42+
let internal = require('./internal.js');
4243

4344
const proxy = require("./endpoint-proxy.js");
4445
const dns = require('dns');
@@ -806,6 +807,7 @@ function initClient(client, inputParams) {
806807
connectionParams.agent = inputParams.agent;
807808
}
808809
}
810+
client.internal = internal.intialize(client);
809811
}
810812
function releaseClient(client) {
811813
var agent = client.connectionParams.agent;

test-basic/test-internal.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2023 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const testconfig = require("../etc/test-config");
18+
const marklogic = require("../lib/marklogic");
19+
const should = require('should');
20+
let dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
21+
22+
describe('internal tests', function() {
23+
24+
after((function(done){
25+
marklogic.releaseClient(dbWriter);
26+
done();
27+
}));
28+
29+
it('test internal object exists', function(done){
30+
31+
try {
32+
should.exist(dbWriter.internal);
33+
done();
34+
} catch(error){
35+
done(error);
36+
}
37+
});
38+
39+
it('test newRequestOptions creation', function(done){
40+
const requestOptions = dbWriter.internal.newRequestOptions('/v1/ping');
41+
requestOptions.method = 'POST';
42+
requestOptions.headers = {
43+
'Accept': 'application/json',
44+
'Content-Type': 'application/json'
45+
};
46+
try {
47+
should.deepEqual(requestOptions.path, '/v1/ping');
48+
should.deepEqual(requestOptions.headers.Accept, 'application/json');
49+
should.deepEqual(requestOptions.headers['Content-Type'], 'application/json');
50+
done();
51+
} catch(error){
52+
done(error);
53+
}
54+
});
55+
56+
it('test internal sendRequest', function(done){
57+
const requestOptions = dbWriter.internal.newRequestOptions('/v1/internal/forestinfo');
58+
requestOptions.method = 'GET';
59+
requestOptions.headers = {
60+
'Accept': 'application/json'
61+
};
62+
try {
63+
should.deepEqual(requestOptions.path, '/v1/internal/forestinfo');
64+
should.deepEqual(requestOptions.method, 'GET');
65+
should.deepEqual(requestOptions.headers.Accept, 'application/json');
66+
dbWriter.internal.sendRequest(requestOptions, 'read forestInfo','single', 'empty')
67+
.result(function(response){
68+
for(let i=0; i<response.length; i++){
69+
should.exist(response[i].host);
70+
should.exist(response[i].name);
71+
should.exist(response[i].id);
72+
}
73+
}).then(() => done())
74+
.catch(e=>done(e));
75+
} catch(error){
76+
done(error);
77+
}
78+
});
79+
80+
it('test newRequestOptions creation with optional database and basePath', function(done){
81+
testconfig.restWriterConnection.basePath = '/test-basePath';
82+
testconfig.restWriterConnection.database = 'test-database';
83+
dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
84+
const requestOptions = dbWriter.internal.newRequestOptions('/v1/ping');
85+
requestOptions.method = 'POST';
86+
requestOptions.headers = {
87+
'Accept': 'application/json',
88+
'Content-Type': 'application/json'
89+
};
90+
try {
91+
should.deepEqual(requestOptions.path, '/test-basePath/v1/ping?database=test-database');
92+
done();
93+
} catch(error){
94+
done(error);
95+
}
96+
});
97+
});

0 commit comments

Comments
 (0)