network-client 1.0.8 → 1.0.9
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/.gitignore +3 -0
- data/Gemfile.lock +3 -1
- data/README.md +2 -1
- data/lib/network-client/core.rb +36 -17
- data/lib/network-client/version.rb +1 -1
- data/network-client.gemspec +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64ff58738532ca1e75915fdf62d59dbffe8fb3b3
|
4
|
+
data.tar.gz: bf9d4e529a0d104ff1e72dc7a02da3a9c23a5bd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a525076e0a9b9d6a9a2f9252af12a2e32fa7598b6fb8c0999d94b1ba6b65aeab37780157f3d42495cc4c3b005a6d9f13fb39460c9cdcfc15705222fb4fb6273c
|
7
|
+
data.tar.gz: e90d925d53cf26b219c0ea15c75fed6e71b46bcb8bf05b447a1b09759c166ed141d819af8ae55adfe7fad23257155433310f62a1f151da6ada5adae470788f83
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
network-client (1.0.
|
4
|
+
network-client (1.0.9)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -16,6 +16,7 @@ GEM
|
|
16
16
|
concurrent-ruby (1.0.5)
|
17
17
|
diff-lcs (1.3)
|
18
18
|
docile (1.1.5)
|
19
|
+
dotenv (2.2.1)
|
19
20
|
factory_girl (4.8.0)
|
20
21
|
activesupport (>= 3.0.0)
|
21
22
|
i18n (0.8.1)
|
@@ -50,6 +51,7 @@ PLATFORMS
|
|
50
51
|
DEPENDENCIES
|
51
52
|
bundler (~> 1.14)
|
52
53
|
codeclimate-test-reporter (~> 1.0)
|
54
|
+
dotenv (~> 2.2)
|
53
55
|
factory_girl (~> 4.5)
|
54
56
|
network-client!
|
55
57
|
rake (~> 10.0)
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ And then execute:
|
|
20
20
|
$ bundle
|
21
21
|
```
|
22
22
|
|
23
|
-
Or install
|
23
|
+
Or install the gem directly:
|
24
24
|
|
25
25
|
```sh
|
26
26
|
$ gem install network-client
|
@@ -29,6 +29,7 @@ $ gem install network-client
|
|
29
29
|
## Usage
|
30
30
|
|
31
31
|
|
32
|
+
For more refer to [the API docuemntation](#soon).
|
32
33
|
|
33
34
|
## Contributing
|
34
35
|
|
data/lib/network-client/core.rb
CHANGED
@@ -6,12 +6,9 @@ require 'logger'
|
|
6
6
|
|
7
7
|
|
8
8
|
module NetworkClient
|
9
|
-
# This class is simple HTTP client that meant to be initialized configured with a single URI.
|
9
|
+
# This class is simple HTTP client that is meant to be initialized configured with a single URI.
|
10
10
|
# Subsequent calls should target endpoints/paths of that URI.
|
11
11
|
#
|
12
|
-
# Return values of its rest-like methods is Struct holding two values for the code and response
|
13
|
-
# body parsed as JSON.
|
14
|
-
#
|
15
12
|
class Client
|
16
13
|
|
17
14
|
HTTP_VERBS = {
|
@@ -21,6 +18,25 @@ module NetworkClient
|
|
21
18
|
:delete => Net::HTTP::Delete
|
22
19
|
}
|
23
20
|
|
21
|
+
DEFAULT_HEADERS = { 'accept' => 'application/json',
|
22
|
+
'Content-Type' => 'application/json' }.freeze
|
23
|
+
|
24
|
+
# Return values of rest-like methods is a struct holding two values:
|
25
|
+
# the response code, and body (parsed as JSON in json request).
|
26
|
+
RESPONSE = Struct.new(:code, :body)
|
27
|
+
|
28
|
+
# Construct and prepare client for requests targeting :endpoint.
|
29
|
+
#
|
30
|
+
# = *endpoint*:
|
31
|
+
# Uri for the host with scheam and port. any other segment like paths will be discarded.
|
32
|
+
# = *tries*:
|
33
|
+
# Number to specify how many is to repeat falied calls.
|
34
|
+
# = *headers*:
|
35
|
+
# Hash to contain any common HTTP headers to be set in client calls.
|
36
|
+
#
|
37
|
+
# Example:
|
38
|
+
# =>
|
39
|
+
#
|
24
40
|
def initialize(endpoint:, tries: 1, headers: {})
|
25
41
|
@uri = URI.parse(endpoint)
|
26
42
|
@tries = tries
|
@@ -28,7 +44,6 @@ module NetworkClient
|
|
28
44
|
set_http_client
|
29
45
|
set_default_headers(headers)
|
30
46
|
set_logger
|
31
|
-
set_response_struct
|
32
47
|
end
|
33
48
|
|
34
49
|
def get(path, params = {}, headers = {})
|
@@ -76,19 +91,14 @@ module NetworkClient
|
|
76
91
|
end
|
77
92
|
|
78
93
|
def set_default_headers(headers)
|
79
|
-
|
80
|
-
@default_headers = defaults.merge(headers)
|
81
|
-
end
|
82
|
-
|
83
|
-
def set_response_struct
|
84
|
-
@response_struct = Struct.new(:code, :body)
|
94
|
+
@default_headers = DEFAULT_HEADERS.merge(headers)
|
85
95
|
end
|
86
96
|
|
87
97
|
def request_json(http_method, path, params, headers)
|
88
98
|
response = request(http_method, path, params, headers)
|
89
99
|
body = JSON.parse(response.body)
|
90
100
|
|
91
|
-
|
101
|
+
RESPONSE.new(response.code, body)
|
92
102
|
|
93
103
|
rescue JSON::ParserError => error
|
94
104
|
@logger.error "parsing response body as json failed.\n Details: \n #{error.message}"
|
@@ -97,6 +107,7 @@ module NetworkClient
|
|
97
107
|
|
98
108
|
def request(http_method, path, params, headers)
|
99
109
|
headers = @default_headers.merge(headers)
|
110
|
+
path = formulate_path(path)
|
100
111
|
|
101
112
|
case http_method
|
102
113
|
when :get
|
@@ -123,7 +134,7 @@ module NetworkClient
|
|
123
134
|
tries_count ||= @tries
|
124
135
|
response = @http.request(request)
|
125
136
|
rescue *errors_to_recover_by_retry => error
|
126
|
-
@logger.warn
|
137
|
+
@logger.warn "[Error]: #{error.message} \nRetry .."
|
127
138
|
(tries_count -= 1).zero? ? raise : retry
|
128
139
|
else
|
129
140
|
response
|
@@ -132,16 +143,24 @@ module NetworkClient
|
|
132
143
|
|
133
144
|
def errors_to_recover_by_retry
|
134
145
|
[Errno::ECONNREFUSED, Net::HTTPServiceUnavailable, Net::ProtocolError, Net::ReadTimeout,
|
135
|
-
Net::OpenTimeout, OpenSSL::SSL::SSLError]
|
146
|
+
Net::OpenTimeout, OpenSSL::SSL::SSLError, SocketError]
|
136
147
|
end
|
137
148
|
|
138
149
|
def errors_to_recover_by_propogate
|
150
|
+
# TODO: make configurable set of errors that stop net call without retry.
|
139
151
|
end
|
140
152
|
|
141
153
|
def encode_path_params(path, params)
|
142
|
-
|
143
|
-
|
144
|
-
|
154
|
+
if params.empty?
|
155
|
+
path
|
156
|
+
else
|
157
|
+
encoded = URI.encode_www_form(params)
|
158
|
+
[path, encoded].join("?")
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def formulate_path(path)
|
163
|
+
path.chars.last.nil? ? "#{path}/" : path
|
145
164
|
end
|
146
165
|
end
|
147
166
|
end
|
data/network-client.gemspec
CHANGED
@@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.add_development_dependency "factory_girl", "~> 4.5"
|
31
31
|
spec.add_development_dependency "simplecov", "~> 0.14.1"
|
32
32
|
spec.add_development_dependency "codeclimate-test-reporter", "~> 1.0"
|
33
|
+
spec.add_development_dependency "dotenv", "~> 2.2"
|
33
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: network-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Abdullah Barrak (abarrak)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '1.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: dotenv
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.2'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.2'
|
97
111
|
description: network-client gem is almost a drop-in thin layer around Net::HTTP classes with
|
98
112
|
simple error handling and retry functionality implemented.
|
99
113
|
email:
|