MCPcopy Index your code
hub / github.com/scality/cloudserver / request

Function request

lib/utilities/request.js:42–143  ·  view source on GitHub ↗

* @param {url.URL | string} endpoint - * @param {object} [options] - * @param {string} options.method - default: 'GET' * @param {object} options.headers- http headers * @param {boolean} options.json - if true, parse response body to json object * @param {function} callback - (error, response, b

(endpoint, options, callback)

Source from the content-addressed store, hash-verified

40 * @returns {object} request object
41 */
42function request(endpoint, options, callback) {
43 if (!endpoint || typeof endpoint === 'function') {
44 throw new Error('Missing target endpoint');
45 }
46
47 let cb;
48 let opts = {};
49 if (typeof options === 'function') {
50 cb = jsutil.once(options);
51 } else if (typeof options === 'object') {
52 opts = JSON.parse(JSON.stringify(options)); // deep-copy
53 if (typeof callback === 'function') {
54 cb = jsutil.once(callback);
55 }
56 }
57
58 if (typeof cb !== 'function') {
59 throw new Error('Missing request callback');
60 }
61
62 if (!(endpoint instanceof url.URL || typeof endpoint === 'string')) {
63 return cb(new Error(`Invalid URI ${endpoint}`));
64 }
65
66 if (!opts.method) {
67 opts.method = 'GET';
68 } else if (!validVerbs.has(opts.method)) {
69 return cb(new Error(`Invalid Method ${opts.method}`));
70 }
71
72 let reqParams;
73 if (typeof endpoint === 'string') {
74 try {
75 reqParams = url.parse(endpoint);
76 } catch (error) {
77 return cb(error);
78 }
79 } else {
80 reqParams = url.parse(endpoint.href);
81 }
82 reqParams.method = opts.method;
83 reqParams.headers = createHeaders(opts.headers || {});
84
85 let request;
86 if (reqParams.protocol === 'http:') {
87 request = http;
88 const httpProxy = process.env.HTTP_PROXY || process.env.http_proxy;
89 if (!proxyCompareUrl(reqParams.hostname) && httpProxy) {
90 reqParams.agent = new HttpProxyAgent(url.parse(httpProxy));
91 }
92 } else if (reqParams.protocol === 'https:') {
93 request = https;
94 const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy;
95 if (!proxyCompareUrl(reqParams.hostname) && httpsProxy) {
96 reqParams.agent = new HttpsProxyAgent(url.parse(httpsProxy));
97 }
98 } else {
99 return cb(new Error(`Invalid Protocol ${reqParams.protocol}`));

Callers 1

_requestWrapperFunction · 0.85

Calls 3

createHeadersFunction · 0.85
writeMethod · 0.80
endMethod · 0.80

Tested by

no test coverage detected