summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-06-12 17:06:14 -0600
committermo khan <mo.khan@gmail.com>2020-06-12 17:08:46 -0600
commit5e327cc4abe45f53918f7afce937c37fa1736a76 (patch)
treeec54bd4c8137e05c6330ca485c2eceddc202a93b
parent35fc5298fd9102cc99a63fa7671d09bfdc53b602 (diff)
Create default_client to simplify usage
-rw-r--r--README.md39
-rw-r--r--lib/net/hippie.rb10
-rw-r--r--lib/net/hippie/client.rb48
-rw-r--r--test/net/client_test.rb4
-rw-r--r--test/net/hippie_test.rb13
5 files changed, 73 insertions, 41 deletions
diff --git a/README.md b/README.md
index 42f0859..0a84769 100644
--- a/README.md
+++ b/README.md
@@ -3,8 +3,8 @@
[![Gem Version](https://badge.fury.io/rb/net-hippie.svg)](https://rubygems.org/gems/net-hippie)
[![Build Status](https://travis-ci.org/mokhan/net-hippie.svg?branch=master)](https://travis-ci.org/mokhan/net-hippie)
-Net::Hippie is a light weight wrapper around `net/http` that defaults to
-sending JSON messages.
+`Net::Hippie` is a light weight wrapper around `net/http` that defaults to
+sending `JSON` messages.
## Installation
@@ -27,23 +27,22 @@ Or install it yourself as:
```ruby
require 'net/hippie'
-Net::Hippie.logger = Rails.logger
-
-client = Net::Hippie::Client.new
+Net::Hippie.logger = Logger.new(STDERR)
headers = {
'Accept' => 'application/vnd.haveibeenpwned.v2+json'
}
-uri = URI.parse('https://haveibeenpwned.com/api/breaches')
-response = client.get(uri, headers: headers)
+uri = 'https://haveibeenpwned.com/api/breaches'
+response = Net::Hippie.get(uri, headers: headers)
puts JSON.parse(response.body)
```
```ruby
-client = Net::Hippie::Client.new
-body = { user: { name: 'hippie' } }
-response = client.post(URI.parse('https://example.org'), body: body)
+response = Net::Hippie.post(
+ 'https://example.org',
+ body: { name: 'hippie' }
+)
puts JSON.parse(response.body)
```
@@ -69,24 +68,30 @@ client = Net::Hippie::Client.new(
### Basic Auth
```ruby
-client = Net::Hippie::Client.new
-headers = { 'Authorization' => Net::Hippie.basic_auth('username', 'password') }
-client.get('https://www.example.org', headers: headers)
+Net::Hippie.get(
+ 'https://www.example.org',
+ headers: {
+ 'Authorization' => Net::Hippie.basic_auth('username', 'password')
+ }
+)
```
### Bearer Auth
```ruby
-client = Net::Hippie::Client.new
headers = { 'Authorization' => Net::Hippie.bearer_auth('token') }
-client.get('https://www.example.org', headers: headers)
+Net::Hippie.get('https://www.example.org', headers: headers)
```
## Development
-After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
+After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/test` to run the tests.
+You can also run `bin/console` for an interactive prompt that will allow you to experiment.
-To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
+To install this gem onto your local machine, run `bundle exec rake install`.
+To release a new version, update the version number in `version.rb`,
+and then run `bin/shipit`, which will create a git tag for the version,
+push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
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: {})
diff --git a/test/net/client_test.rb b/test/net/client_test.rb
index fa15c4f..9ef6b03 100644
--- a/test/net/client_test.rb
+++ b/test/net/client_test.rb
@@ -306,8 +306,8 @@ class ClientTest < Minitest::Test
end
def test_open_timeout_setting
- assert_nil subject.open_timeout
- @subject.open_timeout = 10
assert_equal subject.open_timeout, 10
+ @subject.open_timeout = 5
+ assert_equal subject.open_timeout, 5
end
end
diff --git a/test/net/hippie_test.rb b/test/net/hippie_test.rb
index c5a0312..9da4abe 100644
--- a/test/net/hippie_test.rb
+++ b/test/net/hippie_test.rb
@@ -21,4 +21,17 @@ class HippieTest < Minitest::Test
Net::Hippie.verify_mode = OpenSSL::SSL::VERIFY_NONE
assert Net::Hippie.verify_mode == OpenSSL::SSL::VERIFY_NONE
end
+
+ def test_get_with_retry
+ uri = URI.parse('https://www.example.org/api/scim/v2/schemas')
+ WebMock.stub_request(:get, uri.to_s)
+ .to_timeout.then
+ .to_timeout.then
+ .to_timeout.then
+ .to_return(status: 200, body: { 'success' => 'true' }.to_json)
+ response = Net::Hippie.get(uri)
+ refute_nil response
+ assert_equal Net::HTTPOK, response.class
+ assert_equal JSON.parse(response.body)['success'], 'true'
+ end
end