Sha256: 7b90632405299439e9d2967016a8accec208c6067ece3c8d439fba0832960f01

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

require 'httpclient'
# httpclient would require something (cookie manager) while initialized,
# so we should try to force requiring them to avoid require deadlock!
HTTPClient.new

require 'rest-core/engine'

class RestCore::HttpClient < RestCore::Engine
  private
  def request promise, env
    client = ::HTTPClient.new
    client.cookie_manager = nil
    client.follow_redirect_count = 0
    client.transparent_gzip_decompression = true
    config = config_engine(env) and config.call(client)
    payload, headers = payload_and_headers(env)

    if env[HIJACK]
      request_async(client, payload, headers, promise, env)
    else
      request_sync(client, payload, headers, promise, env)
    end
  end

  def request_sync client, payload, headers, promise, env
    res = client.request(env[REQUEST_METHOD], env[REQUEST_URI], nil,
            payload, {'User-Agent' => 'Ruby'}.merge(headers))

    promise.fulfill(res.content, res.status,
                    normalize_headers(res.header.all))
  end

  def request_async client, payload, headers, promise, env
    res = client.request_async(env[REQUEST_METHOD], env[REQUEST_URI], nil,
            payload, {'User-Agent' => 'Ruby'}.merge(headers)).pop

    promise.fulfill('', res.status,
                    normalize_headers(res.header.all), res.content)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rest-core-3.6.0 lib/rest-core/engine/http-client.rb
rest-core-3.5.92 lib/rest-core/engine/http-client.rb
rest-core-3.5.91 lib/rest-core/engine/http-client.rb