delivery-sdk-ruby 1.0.2 → 1.0.3

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: bcfd75f3bee26c46149ed8faef71642ead7dfc5948f583d87056e0cdf2556f0a
4
- data.tar.gz: 0b18049d740dd31e8abf162bcd26b6b9a06ed35b155a446dd891fe33e8969e9b
3
+ metadata.gz: c6d87b3a7e5e623b5a4cb2a7895f013a728f0db61b1262d45b54164dd5643f1f
4
+ data.tar.gz: d67e6da84bbb02eb12c7c12c79e3e7ef0dd19e37344b9009205106c41b61a9c2
5
5
  SHA512:
6
- metadata.gz: 2a68250979aa4433a51288ea4e0df5a32d92c4cf42bebc4c97fa4ba140f7ea29f1675fdefb15ee2278af37cb34a1c896a22e393e4916246ca2f90903ed4251c9
7
- data.tar.gz: 276eef68b913365d19e49db57eace88f0325caeb534fd0928fe237521bca34b0c3395610bbd58011017162f5703504fb85b1c739c0cc9411c4557a9ee1cecce4
6
+ metadata.gz: 21039d8c46e5404f0997f7ffbb71ed997d1b3ce5d8d83770da3701dca4b2263533ab20e3b17cd10e1370c24dd4bf84ca4735784efc00c38dfe097bd5bfe055c1
7
+ data.tar.gz: 2c1e1f3a77db72f6e19943d932d0357f8f06efbf2945a2780d8e09287dfa784381d493a4c3b981d0785ebddd332de49308030d0d05644dbf94faad1e53122c33
data/README.md CHANGED
@@ -90,6 +90,26 @@ KenticoCloud::Delivery::DeliveryClient.new project_id: '<your-project-id>',
90
90
  secure_key: '<your-secure-key>'
91
91
  ```
92
92
 
93
+ ### Retry policy
94
+
95
+ By default, the SDK uses a retry policy, asking for requested content again in case of an error. The default policy retries the HTTP requests if the following status codes are returned:
96
+
97
+ * 408 - `RequestTimeout`
98
+ * 500 - `InternalServerError`
99
+ * 502 - `BadGateway`
100
+ * 503 - `ServiceUnavailable`
101
+ * 504 - `GatewayTimeout`
102
+
103
+ The default policy retries requests 5 times, totaling 6 overall attempts to retrieve content before returning a `ResponseBase` object containing the error. The consecutive attempts are delayed exponentially: 200 milliseconds, 400 milliseconds, 800 milliseconds, etc.
104
+
105
+ To disable the retry policy, you can use the `with_retry_policy` argument:
106
+
107
+ ```ruby
108
+ KenticoCloud::Delivery::DeliveryClient.new project_id: '<your-project-id>',
109
+ secure_key: '<your-secure-key>',
110
+ with_retry_policy: false
111
+ ```
112
+
93
113
  ## Listing items
94
114
 
95
115
 
@@ -354,7 +374,7 @@ link_resolver = KenticoCloud::Delivery::Resolvers::ContentLinkResolver.new(lambd
354
374
  return "/coffees/#{link.url_slug}" if link.type.eql? 'coffee'
355
375
  return "/brewers/#{link.url_slug}" if link.type.eql? 'brewer'
356
376
  end, lambda do |id|
357
- # link broken
377
+ # Link broken
358
378
  return "/notfound?id=#{id}"
359
379
  end)
360
380
  delivery_client = KenticoCloud::Delivery::DeliveryClient.new project_id: '<your-project-id>',
@@ -1,5 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/delivery/client/delivery_client'
2
2
  require File.dirname(__FILE__) + '/delivery/client/delivery_query'
3
+ require File.dirname(__FILE__) + '/delivery/client/request_manager'
3
4
  require File.dirname(__FILE__) + '/delivery/models/content_item'
4
5
  require File.dirname(__FILE__) + '/delivery/models/content_type'
5
6
  require File.dirname(__FILE__) + '/delivery/models/taxonomy_group'
@@ -23,12 +23,14 @@ module KenticoCloud
23
23
  # - secure_key (+string+)
24
24
  # - content_link_url_resolver ( KenticoCloud::Delivery::Resolvers::ContentLinkResolver )
25
25
  # - inline_content_item_resolver ( KenticoCloud::Delivery::Resolvers::InlineContentItemResolver )
26
+ # - with_retry_policy (+bool+)
26
27
  def initialize(config)
27
28
  @project_id = config.fetch(:project_id)
28
29
  @preview_key = config.fetch(:preview_key, nil)
29
30
  @secure_key = config.fetch(:secure_key, nil)
30
31
  @content_link_url_resolver = config.fetch(:content_link_url_resolver, nil)
31
32
  @inline_content_item_resolver = config.fetch(:inline_content_item_resolver, nil)
33
+ @with_retry_policy = config.fetch(:with_retry_policy, true)
32
34
  self.use_preview = !@preview_key.nil?
33
35
  end
34
36
 
@@ -39,7 +41,8 @@ module KenticoCloud
39
41
  def types
40
42
  DeliveryQuery.new project_id: @project_id,
41
43
  secure_key: @secure_key,
42
- query_type: QUERY_TYPE_TYPES
44
+ query_type: QUERY_TYPE_TYPES,
45
+ with_retry_policy: @with_retry_policy
43
46
  end
44
47
 
45
48
  # Return a single content type of the project
@@ -53,7 +56,8 @@ module KenticoCloud
53
56
  DeliveryQuery.new project_id: @project_id,
54
57
  secure_key: @secure_key,
55
58
  code_name: code_name,
56
- query_type: QUERY_TYPE_TYPES
59
+ query_type: QUERY_TYPE_TYPES,
60
+ with_retry_policy: @with_retry_policy
57
61
  end
58
62
 
59
63
  # Return all content items of the project
@@ -69,7 +73,8 @@ module KenticoCloud
69
73
  qp: query_parameters,
70
74
  content_link_url_resolver: @content_link_url_resolver,
71
75
  inline_content_item_resolver: @inline_content_item_resolver,
72
- query_type: QUERY_TYPE_ITEMS
76
+ query_type: QUERY_TYPE_ITEMS,
77
+ with_retry_policy: @with_retry_policy
73
78
  q.use_preview = use_preview
74
79
  q.preview_key = @preview_key
75
80
  q
@@ -90,7 +95,8 @@ module KenticoCloud
90
95
  qp: query_parameters,
91
96
  content_link_url_resolver: @content_link_url_resolver,
92
97
  inline_content_item_resolver: @inline_content_item_resolver,
93
- query_type: QUERY_TYPE_ITEMS
98
+ query_type: QUERY_TYPE_ITEMS,
99
+ with_retry_policy: @with_retry_policy
94
100
  q.use_preview = use_preview
95
101
  q.preview_key = @preview_key
96
102
  q
@@ -107,7 +113,8 @@ module KenticoCloud
107
113
  DeliveryQuery.new project_id: @project_id,
108
114
  secure_key: @secure_key,
109
115
  qp: query_parameters,
110
- query_type: QUERY_TYPE_TAXONOMIES
116
+ query_type: QUERY_TYPE_TAXONOMIES,
117
+ with_retry_policy: @with_retry_policy
111
118
  end
112
119
 
113
120
  # Return a single taxonomy group of the project
@@ -121,7 +128,8 @@ module KenticoCloud
121
128
  DeliveryQuery.new project_id: @project_id,
122
129
  secure_key: @secure_key,
123
130
  code_name: code_name,
124
- query_type: QUERY_TYPE_TAXONOMIES
131
+ query_type: QUERY_TYPE_TAXONOMIES,
132
+ with_retry_policy: @with_retry_policy
125
133
  end
126
134
 
127
135
  # Return a single element of a content type
@@ -137,7 +145,8 @@ module KenticoCloud
137
145
  secure_key: @secure_key,
138
146
  code_name: element,
139
147
  content_type: content_type,
140
- query_type: QUERY_TYPE_ELEMENT
148
+ query_type: QUERY_TYPE_ELEMENT,
149
+ with_retry_policy: @with_retry_policy
141
150
  end
142
151
  end
143
152
  end
@@ -1,4 +1,3 @@
1
- require 'rest-client'
2
1
  require 'delivery/builders/url_builder'
3
2
  require 'delivery/query_parameters/query_string'
4
3
  require 'delivery/version'
@@ -22,7 +21,8 @@ module KenticoCloud
22
21
  :inline_content_item_resolver,
23
22
  :query_type,
24
23
  :query_string,
25
- :content_type
24
+ :content_type,
25
+ :with_retry_policy
26
26
 
27
27
  # Setter for a custom URL.
28
28
  #
@@ -61,21 +61,14 @@ module KenticoCloud
61
61
  # * *Returns*:
62
62
  # - KenticoCloud::Delivery::Responses::ResponseBase or a class extending it
63
63
  def execute
64
- provide_url
65
- begin
66
- resp = execute_rest
67
- rescue RestClient::ExceptionWithResponse => err
68
- resp = KenticoCloud::Delivery::Responses::ResponseBase.new err.http_code, err.response
69
- rescue RestClient::SSLCertificateNotVerified => err
70
- resp = KenticoCloud::Delivery::Responses::ResponseBase.new 500, err
71
- rescue SocketError => err
72
- resp = KenticoCloud::Delivery::Responses::ResponseBase.new 500, err.message
73
- else
74
- resp = make_response resp
75
- ensure
76
- yield resp if block_given?
77
- resp
78
- end
64
+ headers = @headers.clone
65
+ headers['X-KC-SDKID'] = provide_sdk_header
66
+ headers['Authorization'] = "Bearer #{preview_key}" if should_preview
67
+ headers['Authorization'] = "Bearer #{secure_key}" if !should_preview && secure_key
68
+
69
+ resp = KenticoCloud::Delivery::RequestManager.start self, headers
70
+ yield resp if block_given?
71
+ resp
79
72
  end
80
73
 
81
74
  # Sets a content link resolver to render links contained in rich text. See
@@ -198,18 +191,22 @@ module KenticoCloud
198
191
  self
199
192
  end
200
193
 
201
- private
202
-
203
194
  # Uses KenticoCloud::Delivery::Builders::UrlBuilder.provide_url to set
204
195
  # the URL for the query. The +UrlBuilder+ also validates the URL.
205
196
  #
206
197
  # * *Raises*:
207
198
  # - +UriFormatException+ if the URL is 65,519 characters or more
199
+ #
200
+ # * *Returns*:
201
+ # - +string+ The full URL for this query
208
202
  def provide_url
209
203
  @url = KenticoCloud::Delivery::Builders::UrlBuilder.provide_url self if @url.nil?
210
204
  KenticoCloud::Delivery::Builders::UrlBuilder.validate_url @url
205
+ @url
211
206
  end
212
207
 
208
+ private
209
+
213
210
  # Initializes the +query_string+ attribute with the passed array of
214
211
  # KenticoCloud::Delivery::QueryParameters::Filter objects.
215
212
  #
@@ -242,72 +239,9 @@ module KenticoCloud
242
239
  use_preview && !preview_key.nil?
243
240
  end
244
241
 
245
- # Executes the REST request using the +rest-client+ gem.
246
- #
247
- # * *Returns*:
248
- # - +Object+ The response from the server
249
- def execute_rest
250
- headers = @headers.clone
251
-
252
- headers['X-KC-SDKID'] = provide_sdk_header
253
- headers['Authorization'] = "Bearer #{preview_key}" if should_preview
254
- headers['Authorization'] = "Bearer #{secure_key}" if !should_preview && secure_key
255
-
256
- RestClient.get @url, headers
257
- end
258
-
259
242
  def provide_sdk_header
260
243
  "rubygems.org;delivery-sdk-ruby;#{KenticoCloud::Delivery::VERSION}"
261
244
  end
262
-
263
- # Converts a standard REST response based on the type of query.
264
- #
265
- # * *Returns*:
266
- # - An object derived from the KenticoCloud::Delivery::Responses::ResponseBase class
267
- def make_response(response)
268
- case query_type
269
- when KenticoCloud::Delivery::QUERY_TYPE_ITEMS
270
- respond_item response
271
- when KenticoCloud::Delivery::QUERY_TYPE_TYPES
272
- respond_type response
273
- when KenticoCloud::Delivery::QUERY_TYPE_TAXONOMIES
274
- respond_taxonomy response
275
- when KenticoCloud::Delivery::QUERY_TYPE_ELEMENT
276
- KenticoCloud::Delivery::Responses::DeliveryElementResponse.new JSON.parse(response)
277
- end
278
- end
279
-
280
- def respond_type(response)
281
- if code_name.nil?
282
- KenticoCloud::Delivery::Responses::DeliveryTypeListingResponse.new JSON.parse(response)
283
- else
284
- KenticoCloud::Delivery::Responses::DeliveryTypeResponse.new JSON.parse(response)
285
- end
286
- end
287
-
288
- def respond_taxonomy(response)
289
- if code_name.nil?
290
- KenticoCloud::Delivery::Responses::DeliveryTaxonomyListingResponse.new JSON.parse(response)
291
- else
292
- KenticoCloud::Delivery::Responses::DeliveryTaxonomyResponse.new JSON.parse(response)
293
- end
294
- end
295
-
296
- def respond_item(response)
297
- if code_name.nil?
298
- KenticoCloud::Delivery::Responses::DeliveryItemListingResponse.new(
299
- JSON.parse(response),
300
- content_link_url_resolver,
301
- inline_content_item_resolver
302
- )
303
- else
304
- KenticoCloud::Delivery::Responses::DeliveryItemResponse.new(
305
- JSON.parse(response),
306
- content_link_url_resolver,
307
- inline_content_item_resolver
308
- )
309
- end
310
- end
311
245
  end
312
246
  end
313
247
  end
@@ -0,0 +1,96 @@
1
+ require 'rest-client'
2
+
3
+ module KenticoCloud
4
+ module Delivery
5
+ class RequestManager
6
+ class << self
7
+ MAX_ATTEMPTS = 6
8
+ INITIAL_DELAY = 0.2
9
+ RETRY_WHEN_CODE = [408, 500, 502, 503, 504].freeze
10
+
11
+ def start(query, headers)
12
+ @query = query
13
+ @headers = headers
14
+ @times_run = 1
15
+ @delay = INITIAL_DELAY
16
+ @url = @query.provide_url
17
+ continue
18
+ end
19
+
20
+ private
21
+
22
+ def should_retry(potential_response)
23
+ return potential_response if @times_run == MAX_ATTEMPTS ||
24
+ !RETRY_WHEN_CODE.include?(potential_response.http_code) ||
25
+ !@query.with_retry_policy
26
+
27
+ @times_run += 1
28
+ @delay *= 2
29
+ sleep(@delay)
30
+ continue
31
+ end
32
+
33
+ def continue
34
+ resp = RestClient.get @url, @headers
35
+ rescue RestClient::ExceptionWithResponse => err
36
+ should_retry KenticoCloud::Delivery::Responses::ResponseBase.new err.http_code, err.response
37
+ rescue RestClient::SSLCertificateNotVerified => err
38
+ should_retry KenticoCloud::Delivery::Responses::ResponseBase.new 500, err
39
+ rescue SocketError => err
40
+ should_retry KenticoCloud::Delivery::Responses::ResponseBase.new 500, err.message
41
+ else
42
+ make_response resp
43
+ end
44
+
45
+ # Converts a standard REST response based on the type of query.
46
+ #
47
+ # * *Returns*:
48
+ # - An object derived from the KenticoCloud::Delivery::Responses::ResponseBase class
49
+ def make_response(response)
50
+ case @query.query_type
51
+ when KenticoCloud::Delivery::QUERY_TYPE_ITEMS
52
+ respond_item response
53
+ when KenticoCloud::Delivery::QUERY_TYPE_TYPES
54
+ respond_type response
55
+ when KenticoCloud::Delivery::QUERY_TYPE_TAXONOMIES
56
+ respond_taxonomy response
57
+ when KenticoCloud::Delivery::QUERY_TYPE_ELEMENT
58
+ KenticoCloud::Delivery::Responses::DeliveryElementResponse.new JSON.parse(response)
59
+ end
60
+ end
61
+
62
+ def respond_type(response)
63
+ if @query.code_name.nil?
64
+ KenticoCloud::Delivery::Responses::DeliveryTypeListingResponse.new JSON.parse(response)
65
+ else
66
+ KenticoCloud::Delivery::Responses::DeliveryTypeResponse.new JSON.parse(response)
67
+ end
68
+ end
69
+
70
+ def respond_taxonomy(response)
71
+ if @query.code_name.nil?
72
+ KenticoCloud::Delivery::Responses::DeliveryTaxonomyListingResponse.new JSON.parse(response)
73
+ else
74
+ KenticoCloud::Delivery::Responses::DeliveryTaxonomyResponse.new JSON.parse(response)
75
+ end
76
+ end
77
+
78
+ def respond_item(response)
79
+ if @query.code_name.nil?
80
+ KenticoCloud::Delivery::Responses::DeliveryItemListingResponse.new(
81
+ JSON.parse(response),
82
+ @query.content_link_url_resolver,
83
+ @query.inline_content_item_resolver
84
+ )
85
+ else
86
+ KenticoCloud::Delivery::Responses::DeliveryItemResponse.new(
87
+ JSON.parse(response),
88
+ @query.content_link_url_resolver,
89
+ @query.inline_content_item_resolver
90
+ )
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -1,5 +1,5 @@
1
1
  module KenticoCloud
2
2
  module Delivery
3
- VERSION = '1.0.2'.freeze
3
+ VERSION = '1.0.3'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delivery-sdk-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Dugre
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-22 00:00:00.000000000 Z
11
+ date: 2019-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -122,6 +122,7 @@ files:
122
122
  - lib/delivery/builders/url_builder.rb
123
123
  - lib/delivery/client/delivery_client.rb
124
124
  - lib/delivery/client/delivery_query.rb
125
+ - lib/delivery/client/request_manager.rb
125
126
  - lib/delivery/models/content_item.rb
126
127
  - lib/delivery/models/content_type.rb
127
128
  - lib/delivery/models/pagination.rb