analytics-psw 0.3.7 → 0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/analytics-psw/utilities.rb +28 -16
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53f493ce435433361a616d0b779a87d335616a2b
|
4
|
+
data.tar.gz: 128a17668f84edeb98f19cc37a249641043a12dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ea406d43608bf5910d4b5b2ddbfb608d2638247a8297c9dbda440883180829b0b14c001abb68abc14a27ab28df81c5539abfad2534712c424d93b8c4104d285
|
7
|
+
data.tar.gz: d151d52b9a8bdb9558eb5b0d0bde63a5e589ec30825fabd63447611bbdc78dafae851fb190ffd32103e5826a4709f4a2c4f79e9808306aa81b328749aa873dd5
|
@@ -4,30 +4,39 @@ module AnalyticsPSW
|
|
4
4
|
|
5
5
|
def update_token
|
6
6
|
@expires = nil
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
response = @conn.post do |request|
|
8
|
+
request.url "#{@server_url}/oauth/token?client_id=#{@client_id}&client_secret=#{@client_secret}"
|
9
|
+
request.headers['Content-Type'] = 'application/json'
|
10
|
+
end
|
11
|
+
if response['body'].present?
|
12
|
+
@token = response['body']['access_token']
|
13
|
+
@expires = Time.now + response['body']['expires_in']
|
14
|
+
end
|
11
15
|
end
|
12
16
|
|
13
17
|
def faraday_connection
|
14
18
|
@proxy ? Faraday.new(@server_url, {proxy: {uri: @proxy}}) : Faraday.new(@server_url)
|
15
19
|
end
|
16
20
|
|
17
|
-
def http_request(http_method, url, params = nil, body = nil)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
def http_request(http_method, url, params = nil, body = nil, retries = 2)
|
22
|
+
while retries > 0
|
23
|
+
json_block = ->(req) do
|
24
|
+
req.url url
|
25
|
+
req.headers['Content-Type'] = 'application/json'
|
26
|
+
req.headers['Authorization']= "Bearer #{@token}" if @token
|
27
|
+
req.params = params if params
|
28
|
+
req.body = body if body
|
29
|
+
end
|
30
|
+
response = parse_json_response(@conn.send(http_method, &json_block))
|
31
|
+
break if [200, 201, 202].include?(response['status'])
|
32
|
+
update_token if response['status'] == 401
|
33
|
+
retries -= 1
|
25
34
|
end
|
26
|
-
|
35
|
+
response
|
27
36
|
end
|
28
|
-
|
37
|
+
|
29
38
|
def multi_get_json(service_location, query_parameters)
|
30
|
-
update_token if
|
39
|
+
update_token if need_token_update?
|
31
40
|
results = []
|
32
41
|
EventMachine.run do
|
33
42
|
multi = EventMachine::MultiRequest.new
|
@@ -83,6 +92,9 @@ module AnalyticsPSW
|
|
83
92
|
end
|
84
93
|
parsed_response
|
85
94
|
end
|
86
|
-
|
95
|
+
|
96
|
+
def need_token_update?
|
97
|
+
(@client_id and @client_secret and @expires.nil?) or (@expires and Time.now > @expires)
|
98
|
+
end
|
87
99
|
end
|
88
100
|
end
|