reso_api 0.4.5 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 783eaa57a1160b45fb74a5aa0c2e159c7e498abcc0ff2eb89b86caabbad019dc
4
- data.tar.gz: 2d31ba3674b7b3298fb85f33df80a544103de89a26465047a0e4b9e434f8a9e9
3
+ metadata.gz: d52973c086bdbeff331fc267832e7ece9c7002d5f10e9273c619e9ff985a2ffa
4
+ data.tar.gz: ac5cfd9c1416bb0a43d38d514ca7b3213f133749630869bc55fb2855c797e99c
5
5
  SHA512:
6
- metadata.gz: b19fed3dbd9fb764357b5a49d3347b43017e28722bfd3462726af79fe9235699c63c0723c7a69e2da6aae487cd1ee579e88d03ab394395d3e8467619f4d26b92
7
- data.tar.gz: 334a26aa6f3f2ed207573670e8bbc65ea4fa41bd9503285644b79cc1f102718ac845783e714d7ae2da4f2c162d467561ec3d17737e70328be70dc4260ecbbafc
6
+ metadata.gz: 13689a885600bd73802304c034c9b48bfa1f635fc2016e2be3780575a6a142f6e80809e6af010d65dd4d63be1567c5bf08f33cd89925343932d4dcb1ab3bc455
7
+ data.tar.gz: c770fb9aba3551dd8ee037a37a8ddb6853af78d6530b78856ad45a5d045caeeba0c32ca70f75889a8182a95f8283ac93e273945894284924210365c32b3d7bb5
@@ -50,6 +50,7 @@ module RESO
50
50
  define_method method_name do |*args, &block|
51
51
  hash = args.first.is_a?(Hash) ? args.first : {}
52
52
  endpoint = FILTERABLE_ENDPOINTS[method_name]
53
+ response = {}
53
54
  params = {
54
55
  "$select": hash[:select],
55
56
  "$filter": hash[:filter],
@@ -106,12 +107,17 @@ module RESO
106
107
  if expiration > DateTime.now.utc
107
108
  return payload.token
108
109
  else
109
- @oauth2_payload = oauth2_client.client_credentials.get_token
110
- File.write(oauth2_token_path, @oauth2_payload.to_hash.to_json)
110
+ @oauth2_payload = fresh_oauth2_payload
111
111
  return @oauth2_payload.token
112
112
  end
113
113
  end
114
114
 
115
+ def fresh_oauth2_payload
116
+ @oauth2_payload = oauth2_client.client_credentials.get_token
117
+ File.write(oauth2_token_path, @oauth2_payload.to_hash.to_json)
118
+ return @oauth2_payload
119
+ end
120
+
115
121
  def oauth2_token_path
116
122
  File.join(Dir.tmpdir, [base_url.parameterize, "-oauth-token.json"].join)
117
123
  end
@@ -137,17 +143,36 @@ module RESO
137
143
 
138
144
  def perform_call(endpoint, params)
139
145
  uri = uri_for_endpoint(endpoint)
146
+ retries = 0
140
147
  if params.present?
141
148
  query = params.present? ? URI.encode_www_form(params).gsub("+", " ") : ""
142
149
  uri.query && uri.query.length > 0 ? uri.query += '&' + query : uri.query = query
143
150
  return URI::decode(uri.request_uri) if params.dig(:$debug).present?
144
151
  end
145
- request = Net::HTTP::Get.new(uri.request_uri)
146
- request['Authorization'] = "Bearer #{oauth2_token}"
147
- response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
148
- http.request(request)
152
+ begin
153
+ req = Net::HTTP::Get.new(uri.request_uri)
154
+ req['Authorization'] = "Bearer #{oauth2_token}"
155
+ res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
156
+ http.request(req)
157
+ end
158
+ response = JSON(res.body) rescue res.body
159
+ if response.is_a?(String) && response.include?('Bad Gateway')
160
+ raise StandardError
161
+ elsif response.is_a?(String) && response.include?('Unauthorized')
162
+ fresh_oauth2_payload
163
+ raise StandardError
164
+ elsif response.is_a?(Hash) && response.has_key?("error")
165
+ raise StandardError
166
+ end
167
+ rescue Net::ReadTimeout, StandardError
168
+ if (retries += 1) <= 5
169
+ sleep 10
170
+ retry
171
+ else
172
+ raise
173
+ end
149
174
  end
150
- return JSON(response.body) rescue response.body
175
+ return response
151
176
  end
152
177
 
153
178
  end
@@ -1,3 +1,3 @@
1
1
  module ResoApi
2
- VERSION = "0.4.5"
2
+ VERSION = "0.4.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reso_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Edlund
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-26 00:00:00.000000000 Z
11
+ date: 2022-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler