diff options
| author | mo khan <mo.khan@gmail.com> | 2020-06-12 17:06:14 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-06-12 17:08:46 -0600 |
| commit | 5e327cc4abe45f53918f7afce937c37fa1736a76 (patch) | |
| tree | ec54bd4c8137e05c6330ca485c2eceddc202a93b /lib | |
| parent | 35fc5298fd9102cc99a63fa7671d09bfdc53b602 (diff) | |
Create default_client to simplify usage
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/net/hippie.rb | 10 | ||||
| -rw-r--r-- | lib/net/hippie/client.rb | 48 |
2 files changed, 36 insertions, 22 deletions
diff --git a/lib/net/hippie.rb b/lib/net/hippie.rb index 65158b8..fdc282c 100644 --- a/lib/net/hippie.rb +++ b/lib/net/hippie.rb @@ -52,5 +52,15 @@ module Net def self.bearer_auth(token) "Bearer #{token}" end + + def self.method_missing(symbol, *args) + default_client.with_retry(retries: 3) do |client| + client.public_send(symbol, *args) + end + end + + def self.default_client + @subject ||= Client.new + end end end diff --git a/lib/net/hippie/client.rb b/lib/net/hippie/client.rb index 2aa8e20..0019abb 100644 --- a/lib/net/hippie/client.rb +++ b/lib/net/hippie/client.rb @@ -12,18 +12,20 @@ module Net attr_accessor :mapper, :read_timeout, :open_timeout, :logger attr_accessor :follow_redirects - - def initialize(certificate: nil, headers: DEFAULT_HEADERS, - key: nil, passphrase: nil, verify_mode: Net::Hippie.verify_mode) - @certificate = certificate - @default_headers = headers - @key = key - @mapper = ContentTypeMapper.new - @passphrase = passphrase - @read_timeout = 30 - @verify_mode = verify_mode - @logger = Net::Hippie.logger - @follow_redirects = 0 + attr_accessor :certificate, :key, :passphrase + + def initialize(options = {}) + @default_headers = options.fetch(:headers, DEFAULT_HEADERS) + @mapper = options.fetch(:mapper, ContentTypeMapper.new) + @read_timeout = options.fetch(:read_timeout, 10) + @open_timeout = options.fetch(:open_timeout, 10) + @verify_mode = options.fetch(:verify_mode, Net::Hippie.verify_mode) + @logger = options.fetch(:logger, Net::Hippie.logger) + @follow_redirects = options.fetch(:follow_redirects, 0) + @certificate = options[:certificate] + @key = options[:key] + @passphrase = options[:passphrase] + @connections = {} end def execute(uri, request, limit: follow_redirects, &block) @@ -79,7 +81,6 @@ module Net private attr_reader :default_headers, :verify_mode - attr_reader :certificate, :key, :passphrase def attempt(attempt, max) yield @@ -92,15 +93,18 @@ module Net end def http_for(uri) - uri = URI.parse(uri.to_s) - http = Net::HTTP.new(uri.host, uri.port) - http.read_timeout = read_timeout - http.open_timeout = open_timeout if open_timeout - http.use_ssl = uri.scheme == 'https' - http.verify_mode = verify_mode - http.set_debug_output(logger) - apply_client_tls_to(http) - http + @connections.fetch(uri.to_s) do |key| + uri = URI.parse(uri.to_s) + http = Net::HTTP.new(uri.host, uri.port) + http.read_timeout = read_timeout + http.open_timeout = open_timeout + http.use_ssl = uri.scheme == 'https' + http.verify_mode = verify_mode + http.set_debug_output(logger) + apply_client_tls_to(http) + @connections[key] = http + http + end end def request_for(type, uri, headers: {}, body: {}) |
