reso_transport 1.5.7 → 1.5.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 49293478c0827ccb4bdca1bb6c9974900f8c0c16853b247487120a0ed3bb79df
4
- data.tar.gz: c9d8c93979377ed18d22a179bae44175961b44e8641eb091930f99542d1e610f
3
+ metadata.gz: 91e46c54493b29b1afd7cf10e4193209a6f91692494f5d5f9b35c48e176f0e50
4
+ data.tar.gz: 5778db15cafacd613b1262c88434c4f484e508da600d62d7ef960ea3a4e6e1c5
5
5
  SHA512:
6
- metadata.gz: 4464fe0a9aa0006f25c5eab29dab8422401ac2ebf435b712e65d5727e43606bb0df4fdf895e5a1d6a1e3e44d4937d19c42389577d28568351832ccae64d55ebb
7
- data.tar.gz: 729d643455ddd8e7a827ddc6aea2a8512f93bc65a3e469993da5d2cd5085ecdc5eed4befee4f376cd9986f51a81d363fdeb042a1f8f51e2e51d7230f265c2d7a
6
+ metadata.gz: 140b63e766d92ec632abf864803dd7a6beffde76069b5f9cec4b7f37f0c659b437a15b3b47bbde9692cf84070276e5d189520fbb45c35c7edc8af7396ed86ff6
7
+ data.tar.gz: 17525cbd4fb045a43b5c14082fd076f7798ff7d3abede002e36efecb85a526d5ab4b245d77c2cd08dc191942b7cf0b572979339c51bdda518061c209f889155a
data/README.md CHANGED
@@ -278,6 +278,19 @@ When querying for an enumeration value, you can provide either the system name,
278
278
  #=> {"$top"=>1, "$filter"=>"StandardStatus eq 'ActiveUnderContract'"}
279
279
  ```
280
280
 
281
+ ### Troubleshooting
282
+
283
+ In the event there are connection issues, the following errors are raised:
284
+
285
+ * `ResoTransport::NoResponse` - The server did not respond to the request
286
+ * `ResoTransport::RequestError` - The server responded with a status code outside the 200 range
287
+ * `ResoTransport::ResponseError` - The server responded with errors in the body
288
+ * `ResoTransport::AccessDenied` - Check your authentication details
289
+ * `ResoTransport::LocalizationRequired` - Provide one of the required localizations through the `localization` method
290
+ * `ResoTransport::EncodeError` - No match was found for one or more of the properties
291
+
292
+ The Faraday Request hash is attached to the error for `NoResponse`, `RequestError`, `ResponseError`, and `AccessDenied`. A Faraday Response is attached on `RequestError`, `ResponseError`, and `AccessDenied`.
293
+
281
294
  ## Development
282
295
 
283
296
  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.
@@ -21,13 +21,11 @@ require 'reso_transport/entity_type'
21
21
  require 'reso_transport/enum'
22
22
  require 'reso_transport/property'
23
23
  require 'reso_transport/query'
24
+ require 'reso_transport/errors'
24
25
 
25
26
  Faraday::Utils.default_space_encoding = '%20'
26
27
 
27
28
  module ResoTransport
28
- class Error < StandardError; end
29
-
30
- class AccessDenied < StandardError; end
31
29
  ODATA_TIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'.freeze
32
30
 
33
31
  class << self
@@ -19,6 +19,7 @@ module ResoTransport
19
19
  @endpoint = options.fetch(:endpoint)
20
20
  @username = options.fetch(:username, nil)
21
21
  @password = options.fetch(:password, nil)
22
+ @request = nil
22
23
  end
23
24
 
24
25
  def connection
@@ -31,13 +32,10 @@ module ResoTransport
31
32
  end
32
33
 
33
34
  def authenticate
34
- response = connection.post nil, auth_params
35
+ response = connection.post(nil, auth_params { |req| @request = req })
35
36
  json = JSON.parse response.body
36
37
 
37
- unless response.success?
38
- message = "#{response.reason_phrase}: #{json['error'] || response.body}"
39
- raise ResoTransport::AccessDenied, response: response, message: message
40
- end
38
+ raise AccessDenied.new(request, response, 'token') unless response.success?
41
39
 
42
40
  Access.new({
43
41
  access_token: json.fetch('access_token'),
@@ -46,6 +44,12 @@ module ResoTransport
46
44
  })
47
45
  end
48
46
 
47
+ def request
48
+ return @request.to_h if @request.respond_to? :to_h
49
+
50
+ {}
51
+ end
52
+
49
53
  private
50
54
 
51
55
  def auth_params
@@ -18,10 +18,11 @@ module ResoTransport
18
18
  authorize_request(request_env)
19
19
 
20
20
  @app.call(request_env).on_complete do |response_env|
21
- raise_if_unauthorized(response_env)
21
+ raise_if_unauthorized(request_env, response_env)
22
22
  end
23
23
  rescue ResoTransport::AccessDenied
24
- raise if retries == 0
24
+ raise if retries.zero?
25
+
25
26
  @auth.reset
26
27
  retries -= 1
27
28
  retry
@@ -38,8 +39,8 @@ module ResoTransport
38
39
  )
39
40
  end
40
41
 
41
- def raise_if_unauthorized(response_env)
42
- raise ResoTransport::AccessDenied if response_env[:status] == 401
42
+ def raise_if_unauthorized(request_env, response_env)
43
+ raise ResoTransport::AccessDenied.new(request_env.to_h, response_env) if response_env[:status] == 401
43
44
  end
44
45
  end
45
46
  end
@@ -46,16 +46,19 @@ module ResoTransport
46
46
  end
47
47
 
48
48
  def raw
49
- if response.success?
50
- response.body
51
- else
52
- puts response.body
53
- raise "Error getting #{classname}!"
54
- end
49
+ return response.body if response.success?
50
+
51
+ raise RequestError.new(request, response, classname)
55
52
  end
56
53
 
57
54
  def response
58
55
  raise 'Must implement response method'
59
56
  end
57
+
58
+ def request
59
+ return @request.to_h if @request.respond_to? :to_h
60
+
61
+ {}
62
+ end
60
63
  end
61
64
  end
@@ -16,7 +16,10 @@ module ResoTransport
16
16
  def response
17
17
  @response ||= client.connection.get('DataSystem') do |req|
18
18
  req.headers['Accept'] = 'application/json'
19
+ @request = req
19
20
  end
21
+ rescue Faraday::ConnectionFailed
22
+ raise NoResponse.new(request, nil, 'DataSystem')
20
23
  end
21
24
  end
22
25
  end
@@ -0,0 +1,69 @@
1
+ module ResoTransport
2
+ class ResourceError < StandardError
3
+ attr_reader :resource
4
+
5
+ def initialize(resource)
6
+ @resource = resource
7
+ super message
8
+ end
9
+
10
+ def resource_name
11
+ return resource.name if resource.respond_to?(:name)
12
+
13
+ resource || 'unknown'
14
+ end
15
+
16
+ def message
17
+ "Request error for #{resource_name}"
18
+ end
19
+ end
20
+
21
+ class EncodeError < ResourceError
22
+ def initialize(resource, property)
23
+ @property = property
24
+ super resource
25
+ end
26
+
27
+ def message
28
+ "Property #{@property} not found for #{resource_name}"
29
+ end
30
+ end
31
+
32
+ class LocalizationRequired < ResourceError
33
+ def message
34
+ "Localization required for #{resource_name}"
35
+ end
36
+ end
37
+
38
+ class RequestError < ResourceError
39
+ attr_reader :request, :response
40
+
41
+ def initialize(request, response, resource = nil)
42
+ @response = response.respond_to?(:to_hash) ? response.to_hash : response
43
+ @request = request
44
+ super resource
45
+ end
46
+
47
+ def message
48
+ "Received #{response[:status]} for #{resource_name}"
49
+ end
50
+ end
51
+
52
+ class NoResponse < RequestError
53
+ def message
54
+ "No response for #{resource_name}"
55
+ end
56
+ end
57
+
58
+ class ResponseError < RequestError
59
+ def message
60
+ "Request succeeded for #{resource_name} with response errors"
61
+ end
62
+ end
63
+
64
+ class AccessDenied < RequestError
65
+ def message
66
+ "Access denied: #{response.reason_phrase}"
67
+ end
68
+ end
69
+ end
@@ -23,7 +23,10 @@ module ResoTransport
23
23
  def response
24
24
  @response ||= client.connection.get('$metadata') do |req|
25
25
  req.headers['Accept'] = MIME_TYPES[client.vendor.fetch(:metadata_format, :xml).to_sym]
26
+ @request = req
26
27
  end
28
+ rescue Faraday::ConnectionFailed
29
+ raise NoResponse.new(request, nil, '$metadata')
27
30
  end
28
31
  end
29
32
  end
@@ -58,41 +58,31 @@ module ResoTransport
58
58
  end
59
59
 
60
60
  def count
61
- p compile_params
62
61
  limit(1).include_count
63
- resp = resource.get(compile_params)
64
- parsed_body = JSON.parse(resp.body)
65
- parsed_body.fetch('@odata.count', 0)
62
+ parsed = handle_response response
63
+ parsed.fetch('@odata.count', 0)
66
64
  end
67
65
 
68
66
  def results
69
- resp = execute
67
+ parsed = handle_response response
70
68
 
71
- if resp[:success]
72
- resp[:results]
73
- else
74
- puts resp[:meta]
75
- raise 'Request Failed'
76
- end
69
+ results = Array(parsed.delete('value'))
70
+ resource.parse(results)
77
71
  end
78
72
 
79
- def execute
80
- resp = resource.get(compile_params)
81
- if resp.success?
82
- parsed_body = JSON.parse(resp.body)
83
- results = Array(parsed_body.delete('value'))
84
-
85
- {
86
- success: resp.success? && !parsed_body.key?('error'),
87
- meta: parsed_body,
88
- results: resource.parse(results)
89
- }
90
- else
91
- {
92
- success: false,
93
- meta: resp.body
94
- }
95
- end
73
+ def response
74
+ resource.get(compile_params)
75
+ rescue Faraday::ConnectionFailed
76
+ raise NoResponse.new(resource.request, nil, resource)
77
+ end
78
+
79
+ def handle_response(response)
80
+ raise RequestError.new(resource.request, response, resource) unless response.success?
81
+
82
+ parsed = JSON.parse(response.body)
83
+ raise ResponseError.new(resource.request, response, resource) if parsed.key?('error')
84
+
85
+ parsed
96
86
  end
97
87
 
98
88
  def new_query_context(context)
@@ -149,7 +139,7 @@ module ResoTransport
149
139
 
150
140
  def encode_value(key, val)
151
141
  field = resource.property(key.to_s)
152
- raise "Couldn't find property #{key} for #{resource.name}" if field.nil?
142
+ raise EncodeError.new(resource, key) if field.nil?
153
143
 
154
144
  field.encode(val)
155
145
  end
@@ -39,13 +39,14 @@ module ResoTransport
39
39
  def get(params)
40
40
  client.connection.get(url, params) do |req|
41
41
  req.headers['Accept'] = 'application/json'
42
+ @request = req
42
43
  end
43
44
  end
44
45
 
45
46
  def url
46
47
  return local['ResourcePath'].gsub(%r{^/}, '') if local
47
48
 
48
- raise 'Localization required' if localizations.any? && local.nil?
49
+ raise LocalizationRequired, self if localizations.any? && local.nil?
49
50
 
50
51
  return "#{name}/replication" if client.use_replication_endpoint
51
52
 
@@ -64,5 +65,11 @@ module ResoTransport
64
65
  def inspect
65
66
  to_s
66
67
  end
68
+
69
+ def request
70
+ return @request.to_h if @request.respond_to? :to_h
71
+
72
+ {}
73
+ end
67
74
  end
68
75
  end
@@ -1,3 +1,3 @@
1
1
  module ResoTransport
2
- VERSION = '1.5.7'.freeze
2
+ VERSION = '1.5.8'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reso_transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.7
4
+ version: 1.5.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Druse
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-18 00:00:00.000000000 Z
11
+ date: 2021-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -144,6 +144,7 @@ files:
144
144
  - lib/reso_transport/entity_set.rb
145
145
  - lib/reso_transport/entity_type.rb
146
146
  - lib/reso_transport/enum.rb
147
+ - lib/reso_transport/errors.rb
147
148
  - lib/reso_transport/metadata.rb
148
149
  - lib/reso_transport/metadata_cache.rb
149
150
  - lib/reso_transport/metadata_parser.rb