Wraps hyperquest to provide extra convenience
with handling responses

@param   {string} url - url to request
@param   {object} [options] - request options
@param   {function} callback - invoked on response
@returns {stream.Duplex} - request stream
489827
function request(url, options, callback)
{
  var connection;

  if (typeof options == 'function')
  {
    callback = options;
    options  = {};
  }

  connection = hyperquest(url, options, once(function(error, response)
  {
    if (error)
    {
      callback(error, response);
      return;
    }

    // set body
    response.body = '';

    // accumulate response body
    response.on('data', function(data)
    {
      response.body += data.toString();
    });

    response.on('end', function()
    {
      callback(null, response);
    });
  }));

  return connection;
}
920768
function geohubRequest (options, callback) {
  if (options.url.indexOf('https://') !== 0) {
    options.url = apiBase + options.url
  }

  options.headers = {
    'User-Agent': 'geohub/' + pkg.version
  }

  // delete null/undefined access token to avoid 401 from github
  if (options.qs && !options.qs.access_token) {
    delete options.qs.access_token
  }

  debug(options)

  request(options, function (err, res, body) {
    if (err) return callback(err)

    try {
      var json = JSON.parse(body)
    } catch (e) {
      var msg = 'Failed to parse JSON from ' + options.url
      msg += ' (status code: ' + res.statusCode + ', body: ' + body + ')'
      return callback(new Error(msg))
    }

    if (json.message) {
      return callback(new Error(res.statusCode + ' (github): ' + json.message))
    }

    callback(null, json)
  })
}
838545
function request (method, url) {
  // callback
      if ('function' === typeof url) {
        return new Request('GET', method).end(url)
      }

  // url first
      if (1 === arguments.length) {
        return new Request('GET', method)
      }

      return new Request(method, url)
    }
546901
def _options_request(self, url, **kwargs):
        
        '''  '''
        
    # construct request kwargs
        request_kwargs = {
            'method': 'OPTIONS',
            'url': url
        }
        for key, value in kwargs.items():
            request_kwargs[key] = value

    # send request and handle response
        return self._request(**request_kwargs)
798919
function crawl(options, callback) {

          // Case of retries
          if (options.currentRetries > 0) {
            log.error({"url" : options.url, "step" : "queue-resquester.crawl", "message" : "Retry the request with a delay : " + options.retryTimeout});
            setTimeout(function() {
              execHttp(options, callback);
            }, options.retryTimeout);
            return;
          }

          // Case of a ratelimit forcing
          if (options.rateLimits !== 0) {

              log.error({"url" : options.url, "step" : "queue-resquester.crawl", "message" : "Request with option on ratelimit = " + options.rateLimits});
              setTimeout(function() {
                execHttp(options, callback);
              }, options.rateLimits);

          }
          // The usual case without delay
          else {
            execHttp(options, callback);
          }


  }
324979
function makeRequest(url, handler) {
  const xhr = new XMLHttpRequest();

  xhr.open('GET', url, true);
  xhr.responseType = handler.type;

  xhr.onload = function onLoad(e) {
    if (this.status === 200 || this.status === 0) {
      handler.fn(null, xhr);
      return;
    }
    handler.fn(e, xhr);
  };
  xhr.onerror = function onError(e) {
    handler.fn(e, xhr);
  };
  xhr.send();
}
1282135
function request (url, options) {
  options = Object.assign({}, defaultOptions, options)
  let savedContent
  let savedResponse

  return new Promise((resolve, reject) => {
    fetch(url, options)
      .then(handleResponse)
      .then(handleBody)
      .catch(handleError)

    function handleResponse (response) {
      // Save the response for checking the status later
      savedResponse = response

      // Decode the response body
      switch (options.type) {
        case 'response':
          return response
        case 'json':
          return response.json()
        default:
          return response.text()
      }
    }

    function handleBody (content) {
      // Bubble an error if the response status is not okay
      if (savedResponse && savedResponse.status >= 400) {
        savedContent = content
        throw new Error(`Response status indicates error`)
      }

      // All is well!
      resolve(content)
    }

    function handleError (err) {
      // Overwrite potential decoding errors when the actual problem was the response
      if (savedResponse && savedResponse.status >= 400) {
        err = new Error(`Status ${savedResponse.status}`)
      }

      // Enrich the error message with the response and the content
      let error = new Error(err.message)
      error.response = savedResponse
      error.content = savedContent
      reject(error)
    }
  })
}
1027983
function single (url, options = {}) {
  let tries = 1

  // Execute the request and retry if there are errors (and the
  // retry decider decided that we should try our luck again)
  const callRequest = () => request(url, options).catch(err => {
    if (internalRetry(++tries, err)) {
      return wait(callRequest, internalRetryWait(tries))
    }

    throw err
  })

  return callRequest()
}
822127
function get(url, shouldParse, callback) {
    request(url, function (err, response, body) {
        if (err) {
            console.error('Error making GET request to endpoint ' + url);
      callback(shouldParse ? {} : "{}");
        }

        if (response.statusCode !== 200) {
            console.error('non 200 response for URL: ' + url);
            callback(shouldParse ? {} : "{}");
            return;
        }

        var toReturn = shouldParse ? JSON.parse(body) : body;
        callback(toReturn);
        return;
    });
}
1307762
function makeRequest(options, callback) {
    request(options, function (error, response, body){
        if (error) {
            callback(error, null);
        }
        else if (response.statusCode == 200) {
            callback( null, JSON.parse(body));
        }
        else {
            callback(new Error('HTTP Response Code: ' + response.statusCode), null);
        }
    });
}
563780
function (options, callbackFn) {
            var self = this;

            //compensate for optional options parm
            if (typeof options === 'function') {
                callbackFn = options;
                options = {};
            }

            //assign default port
            options.port = options.port || 1061;
            options.host = options.host || 'localhost';
            options.hostAndPort = 'http://' + options.host + ':' + options.port;
            options.eventStreamPath = require('path').normalize(__dirname + '/../temp/events.txt');
            options.phantomjsPath = options.phantomjsPath || 'phantomjs';

            options.debug && console.log('creating proxy to %s', options.hostAndPort);

            this.options = options;

            //try starting server
            createServer.call(self, options, function (proxy) {
                callbackFn.call(self, proxy);
            });

            return this;//return this to chain end
        }
77321
function networkRequest(url: string, opts: any): Promise<*> {
  if (url.startsWith(InlineBase64JSON)) {
    const content = atob(url.slice(url.indexOf("base64") + 7));
    return Promise.resolve({ content });
  }

  return Promise.race([
    fetch(`/get?url=${url}`).then(res => {
      if (res.status >= 200 && res.status < 300) {
        if (res.headers.get("Content-Type") === "application/wasm") {
          return res.arrayBuffer().then(buffer => ({
            content: buffer,
            isDwarf: true
          }));
        }
        return res.text().then(text => ({ content: text }));
      }
      return Promise.reject(new Error(`failed to request ${url}`));
    }),
    new Promise((resolve, reject) => {
      setTimeout(() => reject(new Error("Connect timeout error")), 6000);
    })
  ]);
}
1260979
function(base_url, callback, cors) {
    var recv = new XhrReceiver(base_url + '/chunking_test', {cors: cors});
    var result = 0;
    recv.onmessage = function(e) {
        // Now a cool hack: we can stop receiving after we got at least
        // one chunk, contains some data, but not everyting.
        var l = e.responsetext.split('h\n').length;
        if(e.readystate === 3 && l > 0 && l < 6 ) {
            result = l;
            recv.abort();
        }
    };
    recv.onclose = function(e) {
        recv = recv.onmessage = recv.onclose = null;
        utils.log('Chunking test: ' + (result ? 'passed' : 'failed')
                  + ' (' + result + ' chunk received)');
        callback(!!result);
    };
}
316905
function Inliner(source, options, callback) {
  var inliner = this;
  events.EventEmitter.call(this);

  // allow for source to be optional
  if (typeof source !== 'string') {
    callback = options;
    options = source;
  }

  if (typeof options === 'function') {
    callback = options;
    options = {};
  }

  if (!options) {
    options = {};
  }

  if (options.url) {
    this.url = options.url;
  }

  if (options.filename) {
    this.filename = options.filename;
  }

  if (options.source) {
    this.source = options.source;
  } else {
    this.source = source;
  }

  // this is an intentioal change. `.headers` is compatible with the request
  // module, but -H and --header is compatible (i.e. the same as) cURL
  if (options.header) {
    options.headers = options.header;
    delete options.header;
  }

  if (options.headers && !Array.isArray(options.headers)) {
    options.headers = [options.headers];
  }

  if (options.headers && Array.isArray(options.headers)) {
    options.headers = options.headers.reduce(function (acc, curr) {
      if (typeof curr === 'string') {
        var parts = curr.split(':').map(function (s) {
          return s.trim();
        });
        acc[parts[0]] = parts[1];
      } else {
        var key = Object.keys(curr);
        acc[key] = curr[key];
      }
      return acc;
    }, {});
  }

  if (options.headers && typeof options.headers[0] === 'string') {
    // convert to an object of key/value pairs
    options.headers = options.headers.reduce(function (acc, curr) {
      var pair = querystring.parse(curr);
      var key = Object.keys(pair).shift();
      acc[key] = pair[key];
      return acc;
    }, {});
  }

  this.headers = options.headers || {};

  this.callback = function wrapper(error, res) {
    // noop the callback once it's fired
    inliner.callback = function noop() {
      inliner.emit('error', 'callback fired again');
    };

    callback(error, res);
  };
  this.options = assign({}, Inliner.defaults(), options);
  this.jobs = {
    total: 0,
    todo: 0,
    tasks: tasks,
    add: this.addJob.bind(this),
    breakdown: {},
    done: {},
  };

  Object.keys(this.jobs.tasks).forEach(function (key) {
    this.jobs.breakdown[key] = 0;
    this.jobs.done[key] = this.completeJob.bind(this, key);
  }.bind(this));

  this.isFile = options.useStdin || false;

  this.on('error', function localErrorHandler(event) {
    inliner.callback(event);
  });

  // this allows the user code to get the object back before
  // it starts firing events
  if (this.source) {
    if (typeof setImmediate === 'undefined') {
      global.setImmediate = function setImmediatePolyfill(fn) {
        // :-/
        setTimeout(fn, 0);
      };
    }
    this.promise = new Promise(function (resolve) {
      global.setImmediate(function () {
        resolve(inliner.main());
      });
    });
  } else {
    this.promise = Promise.reject(new Error('No source to inline'));
  }

  return this;
}
832408
function APIRequest(method, url) {
	var data, callback, opts, req, headers = messageHeaders(this);
	callback = ( typeof(arguments[2]) === 'function' ? arguments[2] : (data = arguments[2], arguments[3]) );

	if (isNode) {
		opts = URL.parse(url);
		opts.method = method;
		opts.headers = headers;

		req = requesters[opts.protocol.slice(0, -1)].request(opts, function(res) {
			var chunks = [];
			res.on('data', function(c) { chunks[chunks.length] = c; });
			res.once('end', function() {
				chunks = Buffer.concat(chunks);
				Zlib.gunzip(chunks, function(err, uc) {
					if (!err) uc = uc.toString();
					try { res.body = JSON.parse(uc || chunks); } catch(e) {}
					return callback(null, res);
				});
			});
		});
		if (type(data) === 'object' || method.toLowerCase() === 'get') req.setHeader("Content-Type", "application/json; charset=utf-8");
		if (data instanceof Multipart) req.setHeader("Content-Type", "multipart/form-data; boundary=" + data.boundary);
		if (data) req.write( data.result || JSON.stringify(data), data.result ? 'binary' : 'utf-8' );
		req.end();

		return req.once('error', function(e) { return callback(e.message); });
	}

	req = new XMLHttpRequest();
	req.open(method.toUpperCase(), url, true);
	for (var key in headers) {
		req.setRequestHeader(key, headers[key]);
	}
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			req.statusCode = req.status;
			req.statusMessage = req.statusText;
			try {req.body = JSON.parse(req.responseText);} catch (e) { return handleErrCB(e, callback); }
			callback(null, req);
		}
	};
	if (type(data) === 'object' || method.toLowerCase() === 'get') req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
	if (data instanceof Multipart) req.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + data.boundary);
	if (data) return req.send( data.result ? data.result : JSON.stringify(data) );
	req.send(null);
}
1041838
function request( opts, clbk ) {
	var body;
	var req;
	var res;
	var err;
	var r;

	// debug( 'Query options: %s', JSON.stringify( opts ) );

	if ( RE.test( opts.protocol ) ) {
		req = https.request;
	} else {
		req = http.request;
	}
	body = '';
	r = req( opts, onResponse );
	r.on( 'error', onError );
	r.end();

	/**
	* FUNCTION: onError( error )
	*	Event listener invoked after encountering an error.
	*
	* @private
	* @param {Error} error - error object
	* @returns {Void}
	*/
	function onError( error ) {
		debug( 'Error encountered while querying endpoint: %s', error.message );
		if ( error instanceof Error ) {
			err = {
				'status': 500,
				'message': 'Request error: ' + error.message
			};
			return clbk( err );
		}
		return clbk( error, res, body );
	} // end FUNCTION onError()

	/**
	* FUNCTION: onResponse( response )
	*	Callback invoked after receiving an HTTP response.
	*
	* @private
	* @param {Object} response - HTTP response object
	* @returns {Void}
	*/
	function onResponse( response ) {
		res = response;
		if ( res.statusCode !== 200 ) {
			err = {
				'status': res.statusCode,
				'message': ''
			};
		}
		debug( 'Received a response from query endpoint.' );
		debug( 'Response status: %s.', res.statusCode );

		debug( 'Response headers: %s', JSON.stringify( res.headers ) );

		res.setEncoding( 'utf8' );
		res.on( 'data', onData );
		res.on( 'end', onEnd );
	} // end FUNCTION onResponse()

	/**
	* FUNCTION: onData( chunk )
	*	Event listener invoked upon receiving response data.
	*
	* @private
	* @param {String} chunk - data chunk
	* @returns {Void}
	*/
	function onData( chunk ) {
		body += chunk;
	} // end FUNCTION onData()

	/**
	* FUNCTION: onEnd()
	*	Event listener invoked upon a response end.
	*
	* @private
	* @returns {Void}
	*/
	function onEnd() {
		var data;
		if ( err ) {
			err.message = body;
			return onError( err );
		}
		// debug( 'Response body: %s', body );

		data = parse( body );
		if ( data instanceof Error ) {
			err = {
				'status': 502,
				'message': 'unable to parse endpoint response data as JSON: ' + data.message
			};
			return onError( err );
		}
		clbk( null, res, data );
	} // end FUNCTION onEnd()
}
862046
function parseUrl(url, callback) {
  var options = {
    uri:            url,
    method:         "GET",
    followRedirect: true
  };

  request(options, function (err, res, body) {
    if (err) {
      callback(err);
    } else {
      callback(null, parse(body));
    }
  });
}