lib/growthforecast/client.rb in growthforecast-client-0.62.3 vs lib/growthforecast/client.rb in growthforecast-client-0.62.4
- old
+ new
@@ -10,59 +10,169 @@
class AlreadyExists < Error; end
class Client
attr_accessor :debug
attr_accessor :client
- attr_reader :debug_dev
attr_reader :base_uri
+ attr_accessor :keepalive
# @param [String] base_uri The base uri of GrowthForecast
- def initialize(base_uri = 'http://127.0.0.1:5125')
+ def initialize(base_uri = 'http://127.0.0.1:5125', opts = {})
+ @client = HTTPClient.new
@base_uri = base_uri
+ opts = stringify_keys(opts)
+ @keepalive = opts.delete('keepalive') # bool
+ self.debug_dev = opts.delete('debug_dev') # IO object such as STDOUT
+ # cf. https://github.com/nahi/httpclient/blob/0a16401e7892fbbd195a0254344bd48ac8a8bb26/lib/httpclient/session.rb#L133-L139
+ # self.connect_timeout = 60
+ # self.connect_retry = 1
+ # self.send_timeout = 120
+ # self.receive_timeout = 60 # For each read_block_size bytes
+ # self.keep_alive_timeout = 15 # '15' is from Apache 2 default
+ # self.read_block_size = 1024 * 16 # follows net/http change in 1.8.7
+ # self.protocol_retry_count = 5
end
- def client
- @client ||= HTTPClient.new
+ def stringify_keys(hash)
+ {}.tap {|h| hash.each {|key, val| h[key.to_s] = val } }
end
- # set the `debug_dev` attribute of HTTPClient
- # @param [IO] debug_dev such as STDOUT
- def debug_dev=(debug_dev)
- client.debug_dev = debug_dev
+ class << self
+ def attr_proxy_reader(symbol)
+ attr_proxy(symbol)
+ end
+
+ def attr_proxy_accessor(symbol)
+ attr_proxy(symbol, true)
+ end
+
+ def attr_proxy(symbol, assignable = false)
+ name = symbol.to_s
+ define_method(name) {
+ @client.__send__(name)
+ }
+ if assignable
+ aname = name + '='
+ define_method(aname) { |rhs|
+ @client.__send__(aname, rhs)
+ }
+ end
+ end
end
+ # cf. https://github.com/nahi/httpclient/blob/0a16401e7892fbbd195a0254344bd48ac8a8bb26/lib/httpclient.rb#L309-L355
+ # cf. https://github.com/nahi/httpclient/blob/0a16401e7892fbbd195a0254344bd48ac8a8bb26/lib/httpclient/session.rb#L89-L158
+ # proxy::SSLConfig:: SSL configurator.
+ attr_proxy_reader :ssl_config
+ # WebAgent::CookieManager:: Cookies configurator.
+ attr_proxy_accessor :cookie_manager
+ # An array of response HTTP message body String which is used for loop-back
+ # test. See test/* to see how to use it. If you want to do loop-back test
+ # of HTTP header, use test_loopback_http_response instead.
+ attr_proxy_reader :test_loopback_response
+ # An array of request filter which can trap HTTP request/response.
+ # See proxy::WWWAuth to see how to use it.
+ attr_proxy_reader :request_filter
+ # proxy::ProxyAuth:: Proxy authentication handler.
+ attr_proxy_reader :proxy_auth
+ # proxy::WWWAuth:: WWW authentication handler.
+ attr_proxy_reader :www_auth
+ # How many times get_content and post_content follows HTTP redirect.
+ # 10 by default.
+ attr_proxy_accessor :follow_redirect_count
+
+ # Set HTTP version as a String:: 'HTTP/1.0' or 'HTTP/1.1'
+ attr_proxy(:protocol_version, true)
+ # Connect timeout in sec.
+ attr_proxy(:connect_timeout, true)
+ # Request sending timeout in sec.
+ attr_proxy(:send_timeout, true)
+ # Response receiving timeout in sec.
+ attr_proxy(:receive_timeout, true)
+ # Reuse the same connection within this timeout in sec. from last used.
+ attr_proxy(:keep_alive_timeout, true)
+ # Size of reading block for non-chunked response.
+ attr_proxy(:read_block_size, true)
+ # Negotiation retry count for authentication. 5 by default.
+ attr_proxy(:protocol_retry_count, true)
+ # if your ruby is older than 2005-09-06, do not set socket_sync = false to
+ # avoid an SSL socket blocking bug in openssl/buffering.rb.
+ attr_proxy(:socket_sync, true)
+ # User-Agent header in HTTP request.
+ attr_proxy(:agent_name, true)
+ # From header in HTTP request.
+ attr_proxy(:from, true)
+ # An array of response HTTP String (not a HTTP message body) which is used
+ # for loopback test. See test/* to see how to use it.
+ attr_proxy(:test_loopback_http_response)
+ # Decompress a compressed (with gzip or deflate) content body transparently. false by default.
+ attr_proxy(:transparent_gzip_decompression, true)
+ # Local socket address. Set HTTPClient#socket_local.host and HTTPClient#socket_local.port to specify local binding hostname and port of TCP socket.
+ attr_proxy(:socket_local, true)
+
+ # cf. https://github.com/nahi/httpclient/blob/0a16401e7892fbbd195a0254344bd48ac8a8bb26/lib/httpclient.rb#L416-L569
+ attr_proxy_accessor :debug_dev
+ attr_proxy_accessor :proxy
+ attr_proxy_accessor :no_proxy
+ def set_auth(domain, user, passwd)
+ @client.set_auth(domain, user, passwd)
+ end
+ def set_basic_auth(domain, user, passwd)
+ @client.set_basic_auth(domain, user, passwd)
+ end
+ def set_proxy_auth(user, passwd)
+ @client.set_proxy_auth(user, passwd)
+ end
+ def set_cookie_store(filename)
+ @client.set_cookie_store(filename)
+ end
+ attr_proxy_reader :save_cookie_store
+ attr_proxy_reader :cookies
+ attr_proxy_accessor :redirect_uri_callback
+
+ def last_request_uri
+ @request_uri
+ end
+
def last_response
@res
end
# GET the JSON API
# @param [String] path
# @return [Hash] response body
def get_json(path)
- @res = client.get("#{@base_uri}#{path}")
+ @request_uri = "#{@base_uri}#{path}"
+ query = nil
+ extheader = @keepalive ? { 'Connection' => 'Keep-Alive' } : {}
+ @res = client.get(@request_uri, query, extheader)
handle_error(@res)
JSON.parse(@res.body)
end
# POST the JSON API
# @param [String] path
# @param [Hash] data
# @return [Hash] response body
def post_json(path, data = {})
pp data if @debug
+ @request_uri = "#{@base_uri}#{path}"
json = JSON.generate(data)
- @res = client.post("#{@base_uri}#{path}", json)
+ extheader = @keepalive ? { 'Connection' => 'Keep-Alive' } : {}
+ @res = client.post(@request_uri, json, extheader)
handle_error(@res)
JSON.parse(@res.body)
end
# POST the non-JSON API
# @param [String] path
# @param [Hash] data
# @return [String] response body
def post_query(path, data = {})
pp data if @debug
- @res = client.post("#{@base_uri}#{path}", data)
+ @request_uri = "#{@base_uri}#{path}"
+ extheader = @keepalive ? { 'Connection' => 'Keep-Alive' } : {}
+ @res = client.post(@request_uri, data, extheader)
handle_error(@res)
JSON.parse(@res.body)
end
# Get the list of graphs, /json/list/graph