infopark_cloud_connector 6.8.0.348.160665197 → 6.8.0.356.19698103
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rails_connector/content_service.rb +98 -21
- metadata +9 -9
@@ -1,30 +1,107 @@
|
|
1
1
|
class ContentService
|
2
|
+
DEFAULT_PROTOCOL = 'https'.freeze
|
3
|
+
SOCKET_ERRORS = [EOFError, IOError, Errno::ECONNABORTED, Errno::ECONNRESET,
|
4
|
+
Errno::EPIPE, Errno::EINVAL].freeze
|
5
|
+
|
2
6
|
def self.query(path, payload)
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
:content_type => :json,
|
10
|
-
:accept => :json,
|
11
|
-
:user_agent => user_agent,
|
12
|
-
},
|
13
|
-
:payload => MultiJson.dump(payload)
|
14
|
-
))
|
7
|
+
retry_once_on_socket_error do
|
8
|
+
ConnectionManager.ensure_started(uri)
|
9
|
+
request = build_request(path, payload)
|
10
|
+
response = ConnectionManager.connection.request(request)
|
11
|
+
MultiJson.load(response.body)
|
12
|
+
end
|
15
13
|
end
|
16
14
|
|
17
|
-
|
15
|
+
class << self
|
16
|
+
private
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
def retry_once_on_socket_error
|
19
|
+
retried = false
|
20
|
+
begin
|
21
|
+
yield
|
22
|
+
rescue *SOCKET_ERRORS
|
23
|
+
raise if retried
|
24
|
+
ConnectionManager.ensure_finished
|
25
|
+
retried = true
|
26
|
+
retry
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def build_request(path, payload)
|
31
|
+
request = Net::HTTP::Post.new("#{uri.path}/#{path}".squeeze('/'), request_headers)
|
32
|
+
request.basic_auth(config['login'], config['api_key'])
|
33
|
+
request.body = MultiJson.dump(payload)
|
34
|
+
request
|
35
|
+
end
|
36
|
+
|
37
|
+
def uri
|
38
|
+
url = config['url']
|
39
|
+
url = "#{DEFAULT_PROTOCOL}://#{url}" unless url.match /^http/
|
40
|
+
URI.parse(url)
|
41
|
+
end
|
42
|
+
|
43
|
+
def request_headers
|
44
|
+
headers = {
|
45
|
+
'Content-Type' => 'application/json',
|
46
|
+
'Accept' => 'application/json',
|
47
|
+
'User-Agent' => user_agent,
|
48
|
+
}
|
49
|
+
if http_host = config['http_host']
|
50
|
+
headers['Host'] = http_host
|
51
|
+
end
|
52
|
+
headers
|
53
|
+
end
|
54
|
+
|
55
|
+
def config
|
56
|
+
RailsConnector::Configuration.content_service
|
57
|
+
end
|
22
58
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
59
|
+
def user_agent
|
60
|
+
@user_agent ||= (
|
61
|
+
gem_info = Gem.loaded_specs["infopark_cloud_connector"]
|
62
|
+
"#{gem_info.name}-#{gem_info.version}"
|
63
|
+
)
|
64
|
+
end
|
28
65
|
end
|
29
66
|
|
67
|
+
module ConnectionManager
|
68
|
+
def self.connection
|
69
|
+
@connection
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.connection=(conn)
|
73
|
+
@connection = conn
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.ensure_started(uri)
|
77
|
+
return if @connection && @connection.started?
|
78
|
+
conn = Net::HTTP.new(uri.host, uri.port)
|
79
|
+
if uri.scheme == 'https'
|
80
|
+
conn.use_ssl = true
|
81
|
+
conn.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
82
|
+
end
|
83
|
+
retry_twice_on_socket_error { conn.start }
|
84
|
+
@connection = conn
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.ensure_finished
|
88
|
+
@connection.finish if @connection && @connection.started?
|
89
|
+
@connection = nil
|
90
|
+
end
|
91
|
+
|
92
|
+
class << self
|
93
|
+
private
|
94
|
+
|
95
|
+
def retry_twice_on_socket_error
|
96
|
+
attempt = 0
|
97
|
+
begin
|
98
|
+
yield
|
99
|
+
rescue *ContentService::SOCKET_ERRORS
|
100
|
+
raise if attempt == 2
|
101
|
+
attempt += 1
|
102
|
+
retry
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
30
107
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: infopark_cloud_connector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 39396865
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 6
|
8
8
|
- 8
|
9
9
|
- 0
|
10
|
-
-
|
11
|
-
-
|
12
|
-
version: 6.8.0.
|
10
|
+
- 356
|
11
|
+
- 19698103
|
12
|
+
version: 6.8.0.356.19698103
|
13
13
|
platform: ruby
|
14
14
|
authors:
|
15
15
|
- Infopark AG
|
@@ -17,7 +17,7 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date: 2012-11-
|
20
|
+
date: 2012-11-27 00:00:00 +01:00
|
21
21
|
default_executable:
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|
@@ -57,14 +57,14 @@ dependencies:
|
|
57
57
|
requirements:
|
58
58
|
- - "="
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
hash:
|
60
|
+
hash: 39396865
|
61
61
|
segments:
|
62
62
|
- 6
|
63
63
|
- 8
|
64
64
|
- 0
|
65
|
-
-
|
66
|
-
-
|
67
|
-
version: 6.8.0.
|
65
|
+
- 356
|
66
|
+
- 19698103
|
67
|
+
version: 6.8.0.356.19698103
|
68
68
|
version_requirements: *id003
|
69
69
|
name: kvom
|
70
70
|
prerelease: false
|