8000 Expand api and odata apis · DHLRPAadmin/orchestrator-nodejs@9aec602 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9aec602

Browse files
committed
Expand api and odata apis
1 parent b82864b commit 9aec602

File tree

9 files changed

+1891
-93
lines changed

9 files changed

+1891
-93
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ https://demo.uipath.com/
6060
This is just the beginning and there is a lot left to do.
6161
If you have suggestions and ideas, please do not hesitate to let me know.
6262
- [ ] Proper unit testing
63-
- [ ] Extend each API version
63+
- [X] Extend each API version
6464
- [ ] Write wiki
6565
- [ ] Write TS definitions
66-
- [ ] Add DELETE method
66+
- [X] Add DELETE method
67+
- [X] Add PATCH method
68+
- [X] Add OrganizationUnitId handling
69+
- [ ] Add browser support (minified)
6770

6871
### License
6972

lib/Orchestrator.js

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var restHelper = require('./restHelper');
88
var v2 = require('./v2');
99

1010
var WELL_KNOWN_METHODS = restHelper.WELL_KNOWN_METHODS;
11+
var ORGANIZATION_UNIT_ID_HEADER = 'x-uipath-organizationunitid';
1112

1213
// TODO we may want to handle relative path for servers configured to host Orchestrator other than at root
1314

@@ -49,7 +50,7 @@ function Orchestrator(options) {
4950
* @private
5051
*/
5152
this._credentials = undefined;
52-
/** @type {RestGroup} */
53+
/** @type {V2RestGroup} */
5354
this.v2 = v2.v2Factory(this);
5455
/**
5556
* @type {Agent|undefined}
@@ -69,10 +70,24 @@ function Orchestrator(options) {
6970
* @private
7071
*/
7172
this._pendingRequestCount = 0;
73+
/**
74+
* @type {number}
75+
* @private
76+
*/
77+
this._activeOrganizationUnitId = -1;
7278
}
7379

7480
module.exports = Orchestrator;
7581

82+
/** @param {number} [organizationUnitId] set the active OU (empty to clear) */
83+
Orchestrator.prototype.switchOrganizationUnitId = function (organizationUnitId) {
84+
if (organizationUnitId === undefined) {
85+
this._activeOrganizationUnitId = -1;
86+
} else {
87+
this._activeOrganizationUnitId = organizationUnitId;
88+
}
89+
};
90+
7691
/**
7792
* @private
7893
* @param {function(Error=)} callback
@@ -180,29 +195,45 @@ Orchestrator.prototype._processElement = function (element, isRetry) {
180195

181196
options.headers = options.headers || {};
182197
options.headers.authorization = 'bearer ' + this._credentials; // TODO check if systematically needed
198+
if (this._activeOrganizationUnitId !== -1) {
199+
options.headers[ORGANIZATION_UNIT_ID_HEADER] = this._activeOrganizationUnitId;
200+
}
183201
switch (element.method) {
184-
case WELL_KNOWN_METHODS.GET:
185-
restHelper.getJSON(
186-
element.options,
187-
this._processingResultHandlerFactory(element, isRetry)
188-
);
189-
break;
190-
case WELL_KNOWN_METHODS.POST:
191-
restHelper.postJSON(
192-
element.options,
193-
element.data,
194-
this._processingResultHandlerFactory(element, isRetry)
195-
);
196-
break;
197-
case WELL_KNOWN_METHODS.PUT:
198-
restHelper.putJSON(
199-
element.options,
200-
element.data,
201-
this._processingResultHandlerFactory(element, isRetry)
202-
);
203-
break;
204-
default:
205-
element.cb(new Error('Method not supported: ' + element.method));
202+
case WELL_KNOWN_METHODS.GET:
203+
restHelper.getJSON(
204+
element.options,
205+
this._processingResultHandlerFactory(element, isRetry)
206+
);
207+
break;
208+
case WELL_KNOWN_METHODS.POST:
209+
restHelper.postJSON(
210+
element.options,
211+
element.data,
212+
this._processingResultHandlerFactory(element, isRetry)
213+
);
214+
break;
215+
case WELL_KNOWN_METHODS.PUT:
216+
restHelper.putJSON(
217+
element.options,
218+
element.data,
219+
this._processingResultHandlerFactory(element, isRetry)
220+
);
221+
break;
222+
case WELL_KNOWN_METHODS.PATCH:
223+
restHelper.patchJSON(
224+
element.options,
225+
element.data,
226+
this._processingResultHandlerFactory(element, isRetry)
227+
);
228+
break;
229+
case WELL_KNOWN_METHODS.DELETE:
230+
restHelper.delete(
231+
element.options,
232+
this._processingResultHandlerFactory(element, isRetry)
233+
);
234+
break;
235+
default:
236+
element.cb(new Error('Method not supported: ' + element.method));
206237
}
207238
};
208239

@@ -313,6 +344,20 @@ Orchestrator.prototype.put = function (path, data, callback) {
313344
);
314345
};
315346

347+
/**
348+
* @param {string} path
349+
* @param {Object} data
350+
* @param {function(err: Error|undefined, response: Object=)} callback
351+
*/
352+
Orchestrator.prototype.patch = function (path, data, callback) {
353+
this._methodWrapper(
354+
WELL_KNOWN_METHODS.PATCH,
355+
path,
356+
data,
357+
callback
358+
);
359+
};
360+
316361
/**
317362
* @param {string} path
318363
* @param {function(err: Error|undefined, response: Object=)} callback

lib/RestGroup.js

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,39 +44,57 @@ RestGroup.prototype.graftLeaf = function (name, method, path) {
4444
var absolutePath = this._path + path;
4545

4646
checkNameExistence(this, name);
47-
switch(method) {
48-
case WELL_KNOWN_METHODS.GET:
49-
this[name] = function (query, callback) {
50-
orchestrator.get(
51-
absolutePath,
52-
query,
53-
callback
54-
);
55-
};
56-
break;
57-
case WELL_KNOWN_METHODS.POST:
58-
this[name] = function (data, callback) {
59-
orchestrator.post(
60-
absolutePath,
61-
data,
62-
callback
63-
);
64-
};
65-
break;
66-
case WELL_KNOWN_METHODS.PUT:
67-
this[name] = function (data, callback) {
68-
orchestrator.put(
69-
absolutePath,
70-
data,
71-
callback
72-
);
73-
};
74-
break;
75-
default:
76-
throw new Error('Unsupported method: ' + method);
47+
switch (method) {
48+
case WELL_KNOWN_METHODS.GET:
49+
this[name] = function (query, callback) {
50+
orchestrator.get(
51+
absolutePath,
52+
query,
53+
callback
54+
);
55+
};
56+
break;
57+
case WELL_KNOWN_METHODS.POST:
58+
this[name] = function (data, callback) {
59+
orchestrator.post(
60+
absolutePath,
61+
data,
62+
callback
63+
);
64+
};
65+
break;
66+
case WELL_KNOWN_METHODS.PUT:
67+
this[name] = function (data, callback) {
68+
orchestrator.put(
69+
absolutePath,
70+
data,
71+
callback
72+
);
73+
};
74+
break;
75+
case WELL_KNOWN_METHODS.DELETE:
76+
this[name] = function (callback) {
77+
orchestrator.delete(
78+
absolutePath,
79+
callback
80+
);
81+
};
82+
break;
83+
default:
84+
throw new Error('Unsupported method: ' + method);
7785
}
7886
};
7987

88+
/**
89+
* @param {string} name
90+
* @param {function} wrapperFunc
91+
* @protected
92+
*/
93+
RestGroup.prototype.graftLeafCustom = function (name, wrapperFunc) {
94+
checkNameExistence(this, name);
95+
this[name] = wrapperFunc(this._orchestrator);
96+
};
97+
8098
/**
8199
* @param {string} name
82100
* @param {string} path
@@ -95,3 +113,13 @@ RestGroup.prototype.graftNode = function (name, path) {
95113
this[name] = newRestGroup;
96114
return newRestGroup;
97115
};
116+
117+
118+
/** @typedef {function(query:Object, OrchestratorRestHelperCallback)} OrchestratorGetHelper */
119+
120+
/** @typedef {function(data:Object, OrchestratorRestHelperCallback)} OrchestratorPostHelper */
121+
122+
/** @typedef {function(data:Object, OrchestratorRestHelperCallback)} OrchestratorPutHelper */
123+
124+
/** @typedef {function(OrchestratorRestHelperCallback)} OrchestratorDeleteHelper */
125+

lib/restHelper.js

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var WELL_KNOWN_METHODS = {
99
GET: 'GET',
1010
PUT: 'PUT',
1111
POST: 'POST',
12+
PATCH: 'PATCH',
1213
DELETE: 'DELETE'
1314
};
1415
module.exports.WELL_KNOWN_METHODS = WELL_KNOWN_METHODS;
@@ -90,7 +91,7 @@ function reqEndHandler(response, buffers, cb) {
9091
* @returns {function(IncomingMessage)}
9192
*/
9293
function responseHandlerFactory(callback) {
93-
return function(res) {
94+
return function (res) {
9495
/** @type {IncomingMessage|EventEmitter} */
9596
var response = res;
9697
/** @type {Array.<Buffer>} */
@@ -104,14 +105,16 @@ function responseHandlerFactory(callback) {
104105
}
105106

106107
/**
108+
* @param {WELL_KNOWN_METHODS} method
107109
* @param {OrchestratorRestHelperOptions} options
110+
* @param {Object|undefined} data
108111
* @param {OrchestratorRestHelperCallback} callback
109112
*/
110-
module.exports.getJSON = function (options, callback) {
113+
function genericRequestHandler(method, options, data, callback) {
111114
var httpModule = options.isSecure ? https : http;
112115
/** @type {ClientRequest|EventEmitter} */
113116
var request = httpModule.request(
114-
optionMapper(WELL_KNOWN_METHODS.GET, options),
117+
optionMapper(method, options),
115118
responseHandlerFactory(callback)
116119
);
117120

@@ -120,7 +123,23 @@ module.exports.getJSON = function (options, callback) {
120123
callback(err);
121124
}
122125
});
126+
if (data !== undefined) {
127+
request.write(JSON.stringify(data));
128+
}
123129
request.end();
130+
}
131+
132+
/**
133+
* @param {OrchestratorRestHelperOptions} options
134+
* @param {OrchestratorRestHelperCallback} callback
135+
*/
136+
module.exports.getJSON = function (options, callback) {
137+
genericRequestHandler(
138+
WELL_KNOWN_METHODS.GET,
139+
options,
140+
undefined,
141+
callback
142+
);
124143
};
125144

126145
/**
@@ -129,20 +148,12 @@ module.exports.getJSON = function (options, callback) {
129148
* @param {OrchestratorRestHelperCallback} callback
130149
*/
131150
module.exports.postJSON = function (options, data, callback) {
132-
var httpModule = options.isSecure ? https : http;
133-
/** @type {ClientRequest|EventEmitter} */
134-
var request = httpModule.request(
135-
optionMapper(WELL_KNOWN_METHODS.POST, options),
136-
responseHandlerFactory(callback)
151+
genericRequestHandler(
152+
WELL_KNOWN_METHODS.POST,
153+
options,
154+
data,
155+
callback
137156
);
138-
139-
request.on('error', function (err) {
140-
if (callback) {
141-
callback(err);
142-
}
143-
});
144-
request.write(JSON.stringify(data));
145-
request.end();
146157
};
147158

148159
/**
@@ -151,20 +162,39 @@ module.exports.postJSON = function (options, data, callback) {
151162
* @param {OrchestratorRestHelperCallback} callback
152163
*/
153164
module.exports.putJSON = function (options, data, callback) {
154-
var httpModule = options.isSecure ? https : http;
155-
/** @type {ClientRequest|EventEmitter} */
156-
var request = httpModule.request(
157-
optionMapper(WELL_KNOWN_METHODS.PUT, options),
158-
responseHandlerFactory(callback)
165+
genericRequestHandler(
166+
WELL_KNOWN_METHODS.PUT,
167+
options,
168+
data,
169+
callback
159170
);
171+
};
160172

161-
request.on('error', function (err) {
162-
if (callback) {
163-
callback(err);
164-
}
165-
});
166-
request.write(JSON.stringify(data));
167-
request.end();
173+
/**
174+
* @param {OrchestratorRestHelperOptions} options
175+
* @param {Object} data
176+
* @param {OrchestratorRestHelperCallback} callback
177+
*/
178+
module.exports.patchJSON = function (options, data, callback) {
179+
genericRequestHandler(
180+
WELL_KNOWN_METHODS.PATCH,
181+
options,
182+
data,
183+
callback
184+
);
185+
};
186+
187+
/**
188+
* @param {OrchestratorRestHelperOptions} options
189+
* @param {OrchestratorRestHelperCallback} callback
190+
*/
191+
module.exports.delete = function (options, callback) {
192+
genericRequestHandler(
193+
WELL_KNOWN_METHODS.DELETE,
194+
options,
195+
undefined,
196+
callback
197+
);
168198
};
169199

170200
/**

0 commit comments

Comments
 (0)
0