class Agent
Implements an HTTP agent
Constants
- OPTS_DEFAULT
Public Class Methods
default()
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 20 def self.default @default ||= new end
get(*args, &block)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 12 def self.get(*args, &block) default.get(*args, &block) end
new()
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 24 def initialize @pools = Hash.new do |h, k| h[k] = SiteConnectionManager.new(k) end end
post(*args, &block)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 16 def self.post(*args, &block) default.post(*args, &block) end
Public Instance Methods
do_request(ctx, &block)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 103 def do_request(ctx, &block) key = uri_key(ctx[:uri]) @pools[key].acquire do |adapter| send_request_and_check_response(adapter, ctx, &block) end rescue Exception => e p e puts e.backtrace.join("\n") end
format_uri(url, ctx)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 72 def format_uri(url, ctx) format( '%<scheme>s://%<host>s%<url>s', scheme: ctx[:uri].scheme, host: ctx[:uri].host, url: url ) end
get(url, opts = OPTS_DEFAULT, &block)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 32 def get(url, opts = OPTS_DEFAULT, &block) request(url, opts.merge(method: :GET), &block) end
post(url, opts = OPTS_DEFAULT, &block)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 36 def post(url, opts = OPTS_DEFAULT, &block) request(url, opts.merge(method: :POST), &block) end
redirect(url, ctx, opts, &block)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 54 def redirect(url, ctx, opts, &block) url = redirect_url(url, ctx) request(url, opts, &block) end
redirect_url(url, ctx)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 59 def redirect_url(url, ctx) case url when /^http(?:s)?\:\/\// url when /^\/\/(.+)$/ ctx[:uri].scheme + url when /^\// format_uri(url, ctx) else ctx[:uri] + url end end
request(url, opts = OPTS_DEFAULT, &block)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 40 def request(url, opts = OPTS_DEFAULT, &block) ctx = request_ctx(url, opts) response = do_request(ctx, &block) case response.status_code when 301, 302 redirect(response.headers['Location'], ctx, opts, &block) when 200, 204 response else raise "Error received from server: #{response.status_code}" end end
request_ctx(url, opts)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 81 def request_ctx(url, opts) { method: opts[:method] || :GET, uri: url_to_uri(url, opts), opts: opts, retry: 0 } end
send_request_and_check_response(adapter, ctx, &block)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 114 def send_request_and_check_response(adapter, ctx, &block) response = adapter.request(ctx) case response.status_code when 200, 204 if block block.(response) else # read body response.body end end response end
uri_key(uri)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 128 def uri_key(uri) { scheme: uri.scheme, host: uri.host, port: uri.port } end
url_to_uri(url, opts)
click to toggle source
# File lib/polyphony/http/client/agent.rb, line 90 def url_to_uri(url, opts) uri = URI(url) if opts[:query] query = opts[:query].map { |k, v| "#{k}=#{v}" }.join('&') if uri.query v.query = "#{uri.query}&#{query}" else uri.query = query end end uri end