net-hippie 0.1.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +20 -1
- data/bin/cibuild +1 -0
- data/lib/net/hippie.rb +17 -0
- data/lib/net/hippie/client.rb +28 -1
- data/lib/net/hippie/content_type_mapper.rb +1 -0
- data/lib/net/hippie/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f64cd42e98caffcf56a025383d07b7defe8991f191597859bfa1b12613127ea
|
4
|
+
data.tar.gz: ca7ac7d7d44f13e6241f87ff2e31bcf76c1f89051a6c562843ec419ddec0ecc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4f12a3384fd90f92f0470231c89ba1c2980701b5e3f75ea73751281750276b266c8dbc4f6ad9ab10088e073cf2f3b638c283e06045f6a3984b8e5c2d3e7eeea
|
7
|
+
data.tar.gz: 26cfa4495ca4ed7edac3f9533d257d199fd24a37e85c55f10c3017159230308799f10acceb9a94d3fb3c64c11fe6ce4495f824fb15d1ac76809590962a614c6d
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# Net::Hippie
|
2
2
|
|
3
|
+
[](https://rubygems.org/gems/net-hippie)
|
4
|
+
[](https://travis-ci.org/mokhan/net-hippie)
|
5
|
+
|
3
6
|
Net::Hippie is a light weight wrapper around `net/http` that defaults to
|
4
7
|
sending JSON messages.
|
5
8
|
|
@@ -40,7 +43,7 @@ puts JSON.parse(response.body)
|
|
40
43
|
```ruby
|
41
44
|
client = Net::Hippie::Client.new
|
42
45
|
body = { user: { name: 'hippie' } }
|
43
|
-
response = client.post(URI.parse('https://example.
|
46
|
+
response = client.post(URI.parse('https://example.org'), body: body)
|
44
47
|
puts JSON.parse(response.body)
|
45
48
|
```
|
46
49
|
|
@@ -63,6 +66,22 @@ client = Net::Hippie::Client.new(
|
|
63
66
|
)
|
64
67
|
```
|
65
68
|
|
69
|
+
### Basic Auth
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
client = Net::Hippie::Client.new
|
73
|
+
headers = { 'Authorization' => Net::Hippie.basic_auth('username', 'password') }
|
74
|
+
client.get('https://www.example.org', headers: headers)
|
75
|
+
```
|
76
|
+
|
77
|
+
### Bearer Auth
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
client = Net::Hippie::Client.new
|
81
|
+
headers = { 'Authorization' => Net::Hippie.bearer_auth('token') }
|
82
|
+
client.get('https://www.example.org', headers: headers)
|
83
|
+
```
|
84
|
+
|
66
85
|
## Development
|
67
86
|
|
68
87
|
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.
|
data/bin/cibuild
CHANGED
data/lib/net/hippie.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'base64'
|
3
4
|
require 'json'
|
4
5
|
require 'logger'
|
5
6
|
require 'net/http'
|
@@ -13,6 +14,14 @@ require 'net/hippie/api'
|
|
13
14
|
module Net
|
14
15
|
# net/http for hippies.
|
15
16
|
module Hippie
|
17
|
+
CONNECTION_ERRORS = [
|
18
|
+
EOFError,
|
19
|
+
Errno::ECONNRESET,
|
20
|
+
Errno::EINVAL,
|
21
|
+
Net::ProtocolError,
|
22
|
+
Timeout::Error
|
23
|
+
].freeze
|
24
|
+
|
16
25
|
def self.logger
|
17
26
|
@logger ||= Logger.new(STDOUT)
|
18
27
|
end
|
@@ -20,5 +29,13 @@ module Net
|
|
20
29
|
def self.logger=(logger)
|
21
30
|
@logger = logger
|
22
31
|
end
|
32
|
+
|
33
|
+
def self.basic_auth(username, password)
|
34
|
+
"Basic #{::Base64.strict_encode64("#{username}:#{password}")}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.bearer_auth(token)
|
38
|
+
"Bearer #{token}"
|
39
|
+
end
|
23
40
|
end
|
24
41
|
end
|
data/lib/net/hippie/client.rb
CHANGED
@@ -12,6 +12,7 @@ module Net
|
|
12
12
|
|
13
13
|
attr_accessor :mapper
|
14
14
|
attr_accessor :read_timeout
|
15
|
+
attr_accessor :logger
|
15
16
|
|
16
17
|
def initialize(
|
17
18
|
certificate: nil,
|
@@ -27,6 +28,7 @@ module Net
|
|
27
28
|
@passphrase = passphrase
|
28
29
|
@read_timeout = 30
|
29
30
|
@verify_mode = verify_mode
|
31
|
+
@logger = Net::Hippie.logger
|
30
32
|
end
|
31
33
|
|
32
34
|
def execute(uri, request)
|
@@ -60,6 +62,27 @@ module Net
|
|
60
62
|
execute(uri, request, &block)
|
61
63
|
end
|
62
64
|
|
65
|
+
# attempt 1 -> delay 1 second
|
66
|
+
# attempt 2 -> delay 2 second
|
67
|
+
# attempt 3 -> delay 4 second
|
68
|
+
# attempt 4 -> delay 8 second
|
69
|
+
# attempt 5 -> delay 16 second
|
70
|
+
# attempt 6 -> delay 32 second
|
71
|
+
# attempt 7 -> delay 64 second
|
72
|
+
# attempt 8 -> delay 128 second
|
73
|
+
def with_retry(retries: 3)
|
74
|
+
retries = 3 if retries <= 0
|
75
|
+
0.upto(retries) do |n|
|
76
|
+
return yield self
|
77
|
+
rescue *::Net::Hippie::CONNECTION_ERRORS => error
|
78
|
+
raise error if n >= retries
|
79
|
+
|
80
|
+
delay = (2**n) + rand(0.5) # delay + jitter
|
81
|
+
warn("`#{error.message}` Retry: #{n + 1}/#{retries} Delay: #{delay}s")
|
82
|
+
sleep delay
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
63
86
|
private
|
64
87
|
|
65
88
|
attr_reader :default_headers
|
@@ -71,7 +94,7 @@ module Net
|
|
71
94
|
http.read_timeout = read_timeout
|
72
95
|
http.use_ssl = uri.scheme == 'https'
|
73
96
|
http.verify_mode = verify_mode
|
74
|
-
http.set_debug_output(
|
97
|
+
http.set_debug_output(logger)
|
75
98
|
apply_client_tls_to(http)
|
76
99
|
http
|
77
100
|
end
|
@@ -97,6 +120,10 @@ module Net
|
|
97
120
|
http.cert = OpenSSL::X509::Certificate.new(certificate)
|
98
121
|
http.key = private_key
|
99
122
|
end
|
123
|
+
|
124
|
+
def warn(message)
|
125
|
+
logger.warn(message)
|
126
|
+
end
|
100
127
|
end
|
101
128
|
end
|
102
129
|
end
|
data/lib/net/hippie/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: net-hippie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -143,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
143
|
version: '0'
|
144
144
|
requirements: []
|
145
145
|
rubyforge_project:
|
146
|
-
rubygems_version: 2.7.
|
146
|
+
rubygems_version: 2.7.6
|
147
147
|
signing_key:
|
148
148
|
specification_version: 4
|
149
149
|
summary: net/http for hippies.
|