frederick_api 0.4.6 → 0.5

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
- SHA1:
3
- metadata.gz: e5ec62b4c9ba2fe7d5e1724f45b0104c95642e82
4
- data.tar.gz: 5216931a23bf50e10bf3ff9697c47e0310189dae
2
+ SHA256:
3
+ metadata.gz: 7131c40a141f015fe0057617182b61268033205cf7c1e12a63e1d89466e0ea5f
4
+ data.tar.gz: 937cd61c99a614ba7425223d699583e19f24452e554f41ccbf0cd2189616685d
5
5
  SHA512:
6
- metadata.gz: b8f4e02a2941809f5e92e83120a90cd527a47da319e8e56f557c2cffa939bc4c7e7d63166437325d88285a67d11633a5b3f53ff8dcea6f0ec0ae84bade98c4b4
7
- data.tar.gz: a19981df22a71b41c2b896f15af49c146eeccb5802993ade16ed0feadf1c1e6667b38141e1b9e6c8346d2e98c605710e611883b794c708618c5f5b3cf19985e6
6
+ metadata.gz: 03f21ac6d2f71bed9bcf24e04475555bed790a421978f9ce59ebd84d9c15bfe0a509ad54f4436de1b7a8157409f0394bc39cad7f11f79486e13c4ff7bcbdcdce
7
+ data.tar.gz: 9f50eb76e2907162c452971f40c135e68bd64ebceb543cd6bef9aaf5b6a30d8dbf3fd525116051e8a93953bb5e71a72187da4be8d159078c145e0366d02a3d86
data/README.md CHANGED
@@ -101,3 +101,14 @@ FrederickAPI::V2::Location.with_access_token(access_token) do
101
101
  # => [...]
102
102
  end
103
103
  ```
104
+ ### Background Jobs
105
+
106
+ FrederickAPI Gem handles asynchronous responses as suggested in
107
+ [JSONApi Recommendations](https://jsonapi.org/recommendations/#asynchronous-processing)
108
+ [Asynchronous Processing](https://jsonapi.org/recommendations/#asynchronous-processing).
109
+ Polling until the job is complete, fetching and returning the completed resource.
110
+
111
+ * A FrederickAPI::V2::Errors::BackgroundJobFailure exception is raised if the API returns
112
+ an error on an asyncronous job.
113
+ * A BackgroundJob Resource will be returned from the client in the case that a successful
114
+ job does not return a resources.
@@ -12,11 +12,13 @@ require 'frederick_api/v2/helpers/has_many'
12
12
  require 'frederick_api/v2/helpers/paginator'
13
13
  require 'frederick_api/v2/helpers/query_builder'
14
14
  require 'frederick_api/v2/helpers/requestor'
15
+ require 'frederick_api/v2/helpers/backgroundable_parser'
15
16
  require 'frederick_api/v2/resource'
16
17
  require 'frederick_api/v2/public_resource'
17
18
 
18
19
  require 'frederick_api/v2/user'
19
20
  require 'frederick_api/v2/location'
21
+ require 'frederick_api/v2/background_job'
20
22
 
21
23
  # Public resources
22
24
  require 'frederick_api/v2/business_category'
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FrederickAPI
4
+ module V2
5
+ # V2 Frederick API async background job class for parsing
6
+ # background job responses coming from API.
7
+ class BackgroundJob < Resource
8
+ attr_accessor :response
9
+
10
+ def has_errors?
11
+ @attributes['status'] == 'error'
12
+ end
13
+
14
+ def retry_after
15
+ try_time = @response[:headers]['retry-after'].to_i
16
+ @retry_after ||= try_time > 1 ? try_time : 1
17
+ end
18
+
19
+ def response_code
20
+ @response_code ||= @response[:status]
21
+ end
22
+
23
+ def status
24
+ @attributes['status']
25
+ end
26
+
27
+ def errors
28
+ @attributes['messages']
29
+ end
30
+
31
+ def id
32
+ @attributes['id']
33
+ end
34
+ end
35
+ end
36
+ end
@@ -23,6 +23,10 @@ module FrederickAPI
23
23
  class BadRequest < Error; end
24
24
  class UnprocessableEntity < Error; end
25
25
 
26
+ # an exception class for when the server reports that a
27
+ # long running job has failed.
28
+ class BackgroundJobFailure < Error; end
29
+
26
30
  ERROR_CODES = {
27
31
  '400' => BadRequest,
28
32
  '422' => UnprocessableEntity
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FrederickAPI
4
+ module V2
5
+ module Helpers
6
+ # Custom Parser for parsing BackgroundJob resources for FrederickAPI V2
7
+ class BackgroundableParser < ::JsonApiClient::Parsers::Parser
8
+ def self.parse(klass, response)
9
+ result_set = super(klass, response)
10
+ return result_set unless result_set&.first&.type == 'background_jobs'
11
+ result_set = super(::FrederickAPI::V2::BackgroundJob, response)
12
+ result_set&.first&.response = { headers: response.headers, status: response.status }
13
+ result_set
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -33,7 +33,6 @@ module FrederickAPI
33
33
 
34
34
  params.delete(klass.primary_key)
35
35
  return request(:post, path, params, 'X-Request-Method' => 'GET') if get_via_post_path?(path)
36
-
37
36
  request(:get, path, params)
38
37
  end
39
38
 
@@ -49,15 +48,23 @@ module FrederickAPI
49
48
  # Retry once on unhandled server errors
50
49
  def request(type, path, params, additional_headers = {})
51
50
  headers = klass.custom_headers.merge(additional_headers)
51
+ make_request = proc { handle_background(handle_errors(make_request(type, path, params, headers))) }
52
52
  begin
53
- handle_errors(make_request(type, path, params, headers))
53
+ make_request.call
54
54
  rescue JsonApiClient::Errors::ConnectionError, JsonApiClient::Errors::ServerError => ex
55
55
  raise ex if ex.is_a?(JsonApiClient::Errors::NotFound) || ex.is_a?(JsonApiClient::Errors::Conflict)
56
- handle_errors(make_request(type, path, params, headers))
56
+ make_request.call
57
57
  end
58
58
  end
59
59
 
60
60
  private
61
+ def handle_background(response)
62
+ return response unless
63
+ (job = response&.first).is_a?(::FrederickAPI::V2::BackgroundJob) && job.status != 'complete'
64
+ raise FrederickAPI::V2::Errors::BackgroundJobFailure, job if job.has_errors?
65
+ sleep job.retry_after
66
+ linked(job.links.attributes['self'])
67
+ end
61
68
 
62
69
  def handle_errors(result)
63
70
  return result unless result.has_errors?
@@ -67,7 +74,9 @@ module FrederickAPI
67
74
  end
68
75
 
69
76
  def make_request(type, path, params, headers)
70
- klass.parser.parse(klass, connection.run(type, path, params, headers))
77
+ faraday_response = connection.run(type, path, params, headers)
78
+ return klass.parser.parse(klass, faraday_response) unless faraday_response.status == 303
79
+ linked(faraday_response.headers['location'])
71
80
  end
72
81
 
73
82
  def get_via_post_path?(path)
@@ -10,6 +10,7 @@ module FrederickAPI
10
10
  self.query_builder = FrederickAPI::V2::Helpers::QueryBuilder
11
11
  self.paginator = FrederickAPI::V2::Helpers::Paginator
12
12
  self.requestor_class = FrederickAPI::V2::Helpers::Requestor
13
+ self.parser = ::FrederickAPI::V2::Helpers::BackgroundableParser
13
14
 
14
15
  attr_accessor :custom_headers
15
16
 
@@ -2,5 +2,5 @@
2
2
 
3
3
  module FrederickAPI
4
4
  # Current gem version
5
- VERSION = '0.4.6'
5
+ VERSION = '0.5'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frederick_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederick Engineering
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-29 00:00:00.000000000 Z
11
+ date: 2019-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_api_client
@@ -36,6 +36,7 @@ files:
36
36
  - lib/frederick_api.rb
37
37
  - lib/frederick_api/configuration.rb
38
38
  - lib/frederick_api/v2/automation.rb
39
+ - lib/frederick_api/v2/background_job.rb
39
40
  - lib/frederick_api/v2/business_category.rb
40
41
  - lib/frederick_api/v2/communication_content.rb
41
42
  - lib/frederick_api/v2/contact.rb
@@ -43,6 +44,7 @@ files:
43
44
  - lib/frederick_api/v2/contact_property.rb
44
45
  - lib/frederick_api/v2/contact_type.rb
45
46
  - lib/frederick_api/v2/errors/errors.rb
47
+ - lib/frederick_api/v2/helpers/backgroundable_parser.rb
46
48
  - lib/frederick_api/v2/helpers/has_many.rb
47
49
  - lib/frederick_api/v2/helpers/paginator.rb
48
50
  - lib/frederick_api/v2/helpers/query_builder.rb
@@ -74,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
76
  version: '0'
75
77
  requirements: []
76
78
  rubyforge_project:
77
- rubygems_version: 2.6.14
79
+ rubygems_version: 2.7.9
78
80
  signing_key:
79
81
  specification_version: 4
80
82
  summary: Frederick API Client