MCPcopy
hub / github.com/acode/cli / request

Method request

cli/registry.js:21–133  ·  view source on GitHub ↗
(action, params, body, completeCallback, progressCallback)

Source from the content-addressed store, hash-verified

19 }
20
21 request (action, params, body, completeCallback, progressCallback) {
22
23 let pusher = this.pusher;
24
25 let completed = false;
26 let callback = function () {
27 completed = true;
28 completeCallback.apply(this, arguments);
29 };
30
31 let performAction = function () {
32
33 let uriString = Object.keys(params)
34 .filter(function (key) {
35 return params[key] !== undefined && params[key] !== null;
36 })
37 .map(function (key) {
38 let value = params[key];
39 value = typeof value === 'object'
40 ? JSON.stringify(value)
41 : value;
42 return [encodeURIComponent(key), encodeURIComponent(value)].join('=');
43 })
44 .join('&');
45
46 let req = this.http.request(
47 {
48 hostname: this.host,
49 port: this.port,
50 path: '/' + action + '?' + uriString,
51 method: 'POST',
52 headers: {
53 'Authorization': `Bearer ${this.accessToken}`,
54 'Content-Length': body ? body.byteLength : 0
55 }
56 },
57 res => {
58 let buffers = [];
59 res.on('data', chunk => buffers.push(chunk));
60 res.on('end', () => {
61 let buffer = Buffer.concat(buffers);
62 let text = buffer.toString();
63 let json;
64 let error;
65 try {
66 json = JSON.parse(text);
67 } catch (e) {
68 error = new Error('Could not ' + action + ', invalid response: "' + text +'"');
69 }
70 if (res.statusCode !== 200) {
71 return callback(
72 error
73 ? new Error(text)
74 : new Error(
75 json && json.error
76 ? json.error.message
77 : text
78 )

Callers 15

runMethod · 0.95
runMethod · 0.95
runMethod · 0.95
runMethod · 0.95
parser.jsFile · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
setAccessTokenMethod · 0.80
loopCbMethod · 0.80

Calls 1

endMethod · 0.80

Tested by

no test coverage detected