Skip to content

Commit 6dd20b7

Browse files
Added Retry Function
1 parent 7b97b1f commit 6dd20b7

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

lib/contentstack/api.rb

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
module Contentstack
88
class API
99
using Utility
10-
def self.init_api(api_key, delivery_token, environment, host, branch, live_preview, proxy)
10+
def self.init_api(api_key, delivery_token, environment, host, branch, live_preview, proxy, retry_options)
1111
@host = host
1212
@api_version = '/v3'
1313
@environment = environment
@@ -17,6 +17,10 @@ def self.init_api(api_key, delivery_token, environment, host, branch, live_previ
1717
@headers = {environment: @environment}
1818
@live_preview = live_preview
1919
@proxy_details = proxy
20+
@timeout = retry_options["timeout"]
21+
@retryDelay = retry_options["retryDelay"]
22+
@retryLimit = retry_options["retryLimit"]
23+
@errorRetry = retry_options["errorRetry"]
2024
end
2125

2226
def self.live_preview_query(query= {})
@@ -30,7 +34,7 @@ def self.fetch_content_types(uid="")
3034
else
3135
path = "/content_types"
3236
end
33-
send_request(path, {})
37+
fetch_retry(path, {})
3438
end
3539

3640
def self.fetch_entries(content_type, query)
@@ -39,7 +43,7 @@ def self.fetch_entries(content_type, query)
3943
send_preview_request(path, query)
4044
else
4145
path = "/content_types/#{content_type}/entries"
42-
send_request(path, query)
46+
fetch_retry(path, query)
4347
end
4448
end
4549

@@ -49,22 +53,37 @@ def self.fetch_entry(content_type, entry_uid, query)
4953
send_preview_request(path, query)
5054
else
5155
path = "/content_types/#{content_type}/entries/#{entry_uid}"
52-
send_request(path, query)
56+
fetch_retry(path, query)
5357
end
5458
end
5559

5660
def self.get_assets(asset_uid=nil)
5761
path = "/assets"
5862
path += "/#{asset_uid}" if !asset_uid.nil?
59-
send_request(path)
63+
fetch_retry(path)
6064
end
6165

6266
def self.get_sync_items(query)
6367
path = "/stacks/sync"
64-
send_request(path, query)
68+
fetch_retry(path, query)
6569
end
6670

6771
private
72+
def self.fetch_retry(path, query=nil, count=0)
73+
response = send_request(path, query)
74+
if @errorRetry.include?(response["status_code"].to_i)
75+
if count < @retryLimit
76+
retryDelay_in_seconds = @retryDelay / 1000 #converting retry_delay from milliseconds into seconds
77+
sleep(retryDelay_in_seconds.to_i) #sleep method requires time in seconds as parameter
78+
response = fetch_retry(path, query, (count + 1))
79+
else
80+
raise Contentstack::Error.new(response) #Retry Limit exceeded
81+
end
82+
else
83+
response
84+
end
85+
end
86+
6887
def self.send_request(path, q=nil)
6988
q ||= {}
7089

@@ -76,7 +95,8 @@ def self.send_request(path, q=nil)
7695
"api_key" => @api_key,
7796
"access_token"=> @access_token,
7897
"user_agent"=> "ruby-sdk/#{Contentstack::VERSION}",
79-
"x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}"
98+
"x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}",
99+
"read_timeout" => @timeout
80100
}
81101
if !@branch.nil? && !@branch.empty?
82102
params["branch"] = @branch
@@ -91,12 +111,12 @@ def self.send_request(path, q=nil)
91111
proxy_username = @proxy_details[:username]
92112
proxy_password = @proxy_details[:password]
93113

94-
ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", :proxy_http_basic_authentication => [proxy_uri, proxy_username, proxy_password], "api_key" => @api_key, "access_token"=> @access_token, "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}").read)
114+
ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", :proxy_http_basic_authentication => [proxy_uri, proxy_username, proxy_password], "api_key" => @api_key, "access_token"=> @access_token, "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout).read)
95115

96116
elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty?
97117
proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/")
98118

99-
ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", "proxy" => proxy_uri, "api_key" => @api_key, "access_token"=> @access_token, "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}").read)
119+
ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", "proxy" => proxy_uri, "api_key" => @api_key, "access_token"=> @access_token, "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout).read)
100120

101121
end
102122
end
@@ -110,9 +130,10 @@ def self.send_preview_request(path, q=nil)
110130
preview_host = @live_preview[:host]
111131
params = {
112132
"api_key" => @api_key,
113-
"authorization" => @live_preview[:management_token],
133+
"access_token"=> @access_token,
114134
"user_agent"=> "ruby-sdk/#{Contentstack::VERSION}",
115-
"x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}"
135+
"x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}",
136+
"read_timeout" => @timeout
116137
}
117138
if !@branch.nil? && !@branch.empty?
118139
params["branch"] = @branch
@@ -128,12 +149,12 @@ def self.send_preview_request(path, q=nil)
128149
proxy_username = @proxy_details[:username]
129150
proxy_password = @proxy_details[:password]
130151

131-
ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}", :proxy_http_basic_authentication => [proxy_uri, proxy_username, proxy_password], "api_key" => @api_key, "authorization" => @live_preview[:management_token], "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}").read)
152+
ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}", :proxy_http_basic_authentication => [proxy_uri, proxy_username, proxy_password], "api_key" => @api_key, "authorization" => @live_preview[:management_token], "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout).read)
132153

133154
elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty?
134155
proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/")
135156

136-
ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}", "proxy" => proxy_uri, "api_key" => @api_key, "authorization" => @live_preview[:management_token], "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}").read)
157+
ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}", "proxy" => proxy_uri, "api_key" => @api_key, "authorization" => @live_preview[:management_token], "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout).read)
137158

138159
end
139160
end

lib/contentstack/client.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,19 @@ def initialize(api_key, delivery_token, environment, options={})
1414
@live_preview = !options.key?(:live_preview) ? {} : options[:live_preview]
1515
@branch = options[:branch].nil? ? "" : options[:branch]
1616
@proxy_details = options[:proxy].nil? ? "" : options[:proxy]
17+
@timeout = options[:timeout].nil? ? 3000 : options[:timeout]
18+
@retryDelay = options[:retryDelay].nil? ? 3000 : options[:retryDelay]
19+
@retryLimit = options[:retryLimit].nil? ? 5 : options[:retryLimit]
20+
@errorRetry = options[:errorRetry].nil? ? [408, 429] : options[:errorRetry]
21+
retry_options = {
22+
"timeout" => @timeout.to_s,
23+
"retryDelay"=> @retryDelay,
24+
"retryLimit"=> @retryLimit,
25+
"errorRetry" => @errorRetry
26+
}
1727
raise Contentstack::Error.new("Proxy URL Should not be Empty") if @proxy_details.present? && @proxy_details[:url].empty?
1828
raise Contentstack::Error.new("Proxy Port Should not be Empty") if @proxy_details.present? && @proxy_details[:port].empty?
19-
API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview, @proxy_details)
29+
API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview, @proxy_details, retry_options)
2030
end
2131

2232
def content_types

0 commit comments

Comments
 (0)