From e374cdddd398f6028335a697d6806b1864e9e2b5 Mon Sep 17 00:00:00 2001 From: Sunil Lakshman Date: Mon, 20 Jun 2022 11:26:03 +0530 Subject: [PATCH 1/9] Added document for Array group and modular blocks --- lib/contentstack/query.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/contentstack/query.rb b/lib/contentstack/query.rb index 7b8361e..85d9389 100644 --- a/lib/contentstack/query.rb +++ b/lib/contentstack/query.rb @@ -249,10 +249,14 @@ def not_equal_to(field_uid, value) # @param [String] field_uid UID of the field for which query should be executed # @param [String] values The possible values for the key's object # - # Example + # Example 1 - Array Equals Operator Within Group # @query = @stack.content_type('category').query # @query.contained_in("title", ["Electronics", "Apparel"]) # + # Example 2 - Array Equals Operator Within Modular Blocks + # @query = @stack.content_type('category').query + # @query.contained_in("additional_info.deals.deal_name", ["Christmas Deal", "Summer Deal"]) + # # @return [Contentstack::Query] def contained_in(field_uid, values) add_query_hash({:"#{field_uid}" => {"$in" => values}}) @@ -264,10 +268,14 @@ def contained_in(field_uid, values) # @param [String] field_uid UID of the field for which query should be executed # @param [String] values The possible values for the key's object # - # Example + # Example 1 - Array Not-equals Operator Within Group # @query = @stack.content_type('category').query # @query.not_contained_in("title", ["Electronics", "Apparel"]) # + # Example 2 - Array Not-equals Operator Within Modular Blocks + # @query = @stack.content_type('category').query + # @query.not_contained_in("additional_info.deals.deal_name", ["Christmas Deal", "Summer Deal"]) + # # @return [Contentstack::Query] def not_contained_in(field_uid, values) add_query_hash({:"#{field_uid}" => {"$nin" => values}}) @@ -377,6 +385,7 @@ def descending(field_uid) # @param [String] code The locale code of the entry # # Example + # Change language method # @query = @stack.content_type('category').query # @query.locale('en-us') # From e39f96afce276e6a15a3060ccc0e43401b225ae6 Mon Sep 17 00:00:00 2001 From: Sunil Lakshman Date: Thu, 30 Jun 2022 11:30:12 +0530 Subject: [PATCH 2/9] Added exception method for displaying error messages --- lib/contentstack/api.rb | 24 ++++++++++++++++++++++-- lib/contentstack/client.rb | 7 +++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/contentstack/api.rb b/lib/contentstack/api.rb index a5ce74e..16d51db 100644 --- a/lib/contentstack/api.rb +++ b/lib/contentstack/api.rb @@ -80,7 +80,17 @@ def self.send_request(path, q=nil) if !@branch.nil? && !@branch.empty? params["branch"] = @branch end - ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", params).read) + begin + ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", params).read) + rescue OpenURI::HTTPError => error + response = error.io + #response.status + # => ["503", "Service Unavailable"] + error_response = JSON.parse(response.string) + error_status = {"status_code" => response.status[0], "status_message" => response.status[1]} + error = error_response.merge(error_status) + raise Contentstack::Error.new(error.to_s) + end end def self.send_preview_request(path, q=nil) @@ -99,7 +109,17 @@ def self.send_preview_request(path, q=nil) if !@branch.nil? && !@branch.empty? params["branch"] = @branch end - ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}",params).read) + begin + ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}",params).read) + rescue OpenURI::HTTPError => error + response = error.io + #response.status + # => ["503", "Service Unavailable"] + error_response = JSON.parse(response.string) + error_status = {"status_code" => response.status[0], "status_message" => response.status[1]} + error = error_response.merge(error_status) + raise Contentstack::Error.new(error.to_s) + end end end end diff --git a/lib/contentstack/client.rb b/lib/contentstack/client.rb index dcabe86..a8c0b3f 100644 --- a/lib/contentstack/client.rb +++ b/lib/contentstack/client.rb @@ -3,12 +3,19 @@ require 'contentstack/asset_collection' require 'contentstack/sync_result' require 'util' +require 'contentstack/error' module Contentstack class Client using Utility attr_reader :region, :host # Initialize "Contentstack" Client instance def initialize(api_key, delivery_token, environment, options={}) + raise Contentstack::Error.new("Api Key is not valid") if api_key.class != String + raise Contentstack::Error.new("Api Key Field Should not be Empty") if api_key.empty? + raise Contentstack::Error.new("Delivery Token is not valid") if delivery_token.class != String + raise Contentstack::Error.new("Delivery Token Field Should not be Empty") if delivery_token.empty? + raise Contentstack::Error.new("Envirnoment Field is not valid") if environment.class != String + raise Contentstack::Error.new("Envirnoment Field Should not be Empty") if environment.empty? @region = options[:region].nil? ? Contentstack::Region::US : options[:region] @host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host] @live_preview = !options.key?(:live_preview) ? {} : options[:live_preview] From 1e344ff111638c1239acd4f814b0ed9aa4b77c9d Mon Sep 17 00:00:00 2001 From: Sunil Lakshman Date: Tue, 19 Jul 2022 18:25:13 +0530 Subject: [PATCH 3/9] Added proxy feature in ruby SDK --- lib/contentstack/api.rb | 20 +++++++++++++++++++- lib/contentstack/client.rb | 4 ++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/contentstack/api.rb b/lib/contentstack/api.rb index a5ce74e..7b5d0dc 100644 --- a/lib/contentstack/api.rb +++ b/lib/contentstack/api.rb @@ -80,7 +80,25 @@ def self.send_request(path, q=nil) if !@branch.nil? && !@branch.empty? params["branch"] = @branch end - ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", params).read) + if @proxy_details.empty? + + ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", params).read) + + elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].present? && @proxy_details[:password].present? + + proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}") + proxy_username = @proxy_details[:username] + proxy_password = @proxy_details[:password] + + ActiveSupport::JSON.decode(URI.open("#{@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) + + elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty? + proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}") + proxy_auth = {"proxy" => proxy_uri} + params_with_proxy = params.merge(proxy_auth) + ActiveSupport::JSON.decode(open("#{@host}#{@api_version}#{path}#{query}", params_with_proxy).read) + + end end def self.send_preview_request(path, q=nil) diff --git a/lib/contentstack/client.rb b/lib/contentstack/client.rb index dcabe86..da2fcba 100644 --- a/lib/contentstack/client.rb +++ b/lib/contentstack/client.rb @@ -13,6 +13,10 @@ def initialize(api_key, delivery_token, environment, options={}) @host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host] @live_preview = !options.key?(:live_preview) ? {} : options[:live_preview] @branch = options[:branch].nil? ? "" : options[:branch] + @proxy_details = options[:proxy].nil? ? "" : options[:proxy] + raise Contentstack::Error.new("Proxy URL Should not be Empty") if @proxy_details.present? && @proxy_details[:url].empty? + raise Contentstack::Error.new("Proxy Port Should not be Empty") if @proxy_details.present? && @proxy_details[:port].empty? + API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview, @proxy_details) API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview) end From cb250aa6daf252a66eda1d13afc1b895c265e424 Mon Sep 17 00:00:00 2001 From: Sunil Lakshman Date: Tue, 19 Jul 2022 18:38:32 +0530 Subject: [PATCH 4/9] Added Proxy Feature to Ruby SDK --- lib/contentstack/api.rb | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/contentstack/api.rb b/lib/contentstack/api.rb index 7b5d0dc..0c87027 100644 --- a/lib/contentstack/api.rb +++ b/lib/contentstack/api.rb @@ -7,7 +7,7 @@ module Contentstack class API using Utility - def self.init_api(api_key, delivery_token, environment, host, branch, live_preview) + def self.init_api(api_key, delivery_token, environment, host, branch, live_preview, proxy) @host = host @api_version = '/v3' @environment = environment @@ -16,6 +16,7 @@ def self.init_api(api_key, delivery_token, environment, host, branch, live_previ @branch = branch @headers = {environment: @environment} @live_preview = live_preview + @proxy_details = proxy end def self.live_preview_query(query= {}) @@ -117,7 +118,26 @@ def self.send_preview_request(path, q=nil) if !@branch.nil? && !@branch.empty? params["branch"] = @branch end - ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}",params).read) + + if @proxy_details.empty? + + ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}",params).read) + + elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].present? && @proxy_details[:password].present? + + proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}") + proxy_username = @proxy_details[:username] + proxy_password = @proxy_details[:password] + + 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) + + elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty? + proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}") + proxy_auth = {"proxy" => proxy_uri} + params_with_proxy = params.merge(proxy_auth) + ActiveSupport::JSON.decode(open("#{preview_host}#{@api_version}#{path}#{query}", params_with_proxy).read) + + end end end end From b8198d0a6704634538c500872a4b06cbcab80f90 Mon Sep 17 00:00:00 2001 From: Sunil Lakshman Date: Wed, 20 Jul 2022 10:21:44 +0530 Subject: [PATCH 5/9] Added params for live preview method --- lib/contentstack/api.rb | 20 +++++++++----------- lib/contentstack/client.rb | 1 - 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/contentstack/api.rb b/lib/contentstack/api.rb index 0c87027..9bddca0 100644 --- a/lib/contentstack/api.rb +++ b/lib/contentstack/api.rb @@ -87,17 +87,16 @@ def self.send_request(path, q=nil) elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].present? && @proxy_details[:password].present? - proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}") + proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/") proxy_username = @proxy_details[:username] proxy_password = @proxy_details[:password] - ActiveSupport::JSON.decode(URI.open("#{@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) + 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) elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty? - proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}") - proxy_auth = {"proxy" => proxy_uri} - params_with_proxy = params.merge(proxy_auth) - ActiveSupport::JSON.decode(open("#{@host}#{@api_version}#{path}#{query}", params_with_proxy).read) + proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/") + + 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) end end @@ -125,17 +124,16 @@ def self.send_preview_request(path, q=nil) elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].present? && @proxy_details[:password].present? - proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}") + proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/") proxy_username = @proxy_details[:username] proxy_password = @proxy_details[:password] 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) elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty? - proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}") - proxy_auth = {"proxy" => proxy_uri} - params_with_proxy = params.merge(proxy_auth) - ActiveSupport::JSON.decode(open("#{preview_host}#{@api_version}#{path}#{query}", params_with_proxy).read) + proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/") + + ActiveSupport::JSON.decode(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) end end diff --git a/lib/contentstack/client.rb b/lib/contentstack/client.rb index da2fcba..434211c 100644 --- a/lib/contentstack/client.rb +++ b/lib/contentstack/client.rb @@ -17,7 +17,6 @@ def initialize(api_key, delivery_token, environment, options={}) raise Contentstack::Error.new("Proxy URL Should not be Empty") if @proxy_details.present? && @proxy_details[:url].empty? raise Contentstack::Error.new("Proxy Port Should not be Empty") if @proxy_details.present? && @proxy_details[:port].empty? API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview, @proxy_details) - API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview) end def content_types From 7b97b1f5a59bac662bb7134fccb7d0e074b84cb8 Mon Sep 17 00:00:00 2001 From: Sunil Lakshman Date: Wed, 20 Jul 2022 13:01:31 +0530 Subject: [PATCH 6/9] Added URI.open in live preview method --- lib/contentstack/api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/contentstack/api.rb b/lib/contentstack/api.rb index 9bddca0..8ec72fe 100644 --- a/lib/contentstack/api.rb +++ b/lib/contentstack/api.rb @@ -133,7 +133,7 @@ def self.send_preview_request(path, q=nil) elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty? proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/") - ActiveSupport::JSON.decode(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) + 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) end end From 6dd20b7d1d5d18b569b7b7e0e58d5087bfd01faf Mon Sep 17 00:00:00 2001 From: Sunil Lakshman Date: Fri, 29 Jul 2022 11:38:31 +0530 Subject: [PATCH 7/9] Added Retry Function --- lib/contentstack/api.rb | 47 +++++++++++++++++++++++++++----------- lib/contentstack/client.rb | 12 +++++++++- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/lib/contentstack/api.rb b/lib/contentstack/api.rb index 8ec72fe..139348b 100644 --- a/lib/contentstack/api.rb +++ b/lib/contentstack/api.rb @@ -7,7 +7,7 @@ module Contentstack class API using Utility - def self.init_api(api_key, delivery_token, environment, host, branch, live_preview, proxy) + def self.init_api(api_key, delivery_token, environment, host, branch, live_preview, proxy, retry_options) @host = host @api_version = '/v3' @environment = environment @@ -17,6 +17,10 @@ def self.init_api(api_key, delivery_token, environment, host, branch, live_previ @headers = {environment: @environment} @live_preview = live_preview @proxy_details = proxy + @timeout = retry_options["timeout"] + @retryDelay = retry_options["retryDelay"] + @retryLimit = retry_options["retryLimit"] + @errorRetry = retry_options["errorRetry"] end def self.live_preview_query(query= {}) @@ -30,7 +34,7 @@ def self.fetch_content_types(uid="") else path = "/content_types" end - send_request(path, {}) + fetch_retry(path, {}) end def self.fetch_entries(content_type, query) @@ -39,7 +43,7 @@ def self.fetch_entries(content_type, query) send_preview_request(path, query) else path = "/content_types/#{content_type}/entries" - send_request(path, query) + fetch_retry(path, query) end end @@ -49,22 +53,37 @@ def self.fetch_entry(content_type, entry_uid, query) send_preview_request(path, query) else path = "/content_types/#{content_type}/entries/#{entry_uid}" - send_request(path, query) + fetch_retry(path, query) end end def self.get_assets(asset_uid=nil) path = "/assets" path += "/#{asset_uid}" if !asset_uid.nil? - send_request(path) + fetch_retry(path) end def self.get_sync_items(query) path = "/stacks/sync" - send_request(path, query) + fetch_retry(path, query) end private + def self.fetch_retry(path, query=nil, count=0) + response = send_request(path, query) + if @errorRetry.include?(response["status_code"].to_i) + if count < @retryLimit + retryDelay_in_seconds = @retryDelay / 1000 #converting retry_delay from milliseconds into seconds + sleep(retryDelay_in_seconds.to_i) #sleep method requires time in seconds as parameter + response = fetch_retry(path, query, (count + 1)) + else + raise Contentstack::Error.new(response) #Retry Limit exceeded + end + else + response + end + end + def self.send_request(path, q=nil) q ||= {} @@ -76,7 +95,8 @@ def self.send_request(path, q=nil) "api_key" => @api_key, "access_token"=> @access_token, "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", - "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}" + "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", + "read_timeout" => @timeout } if !@branch.nil? && !@branch.empty? params["branch"] = @branch @@ -91,12 +111,12 @@ def self.send_request(path, q=nil) proxy_username = @proxy_details[:username] proxy_password = @proxy_details[:password] - 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) + 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) elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty? proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/") - 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) + 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) end end @@ -110,9 +130,10 @@ def self.send_preview_request(path, q=nil) preview_host = @live_preview[:host] params = { "api_key" => @api_key, - "authorization" => @live_preview[:management_token], + "access_token"=> @access_token, "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", - "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}" + "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", + "read_timeout" => @timeout } if !@branch.nil? && !@branch.empty? params["branch"] = @branch @@ -128,12 +149,12 @@ def self.send_preview_request(path, q=nil) proxy_username = @proxy_details[:username] proxy_password = @proxy_details[:password] - 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) + 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) elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty? proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/") - 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) + 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) end end diff --git a/lib/contentstack/client.rb b/lib/contentstack/client.rb index 434211c..db6d26d 100644 --- a/lib/contentstack/client.rb +++ b/lib/contentstack/client.rb @@ -14,9 +14,19 @@ def initialize(api_key, delivery_token, environment, options={}) @live_preview = !options.key?(:live_preview) ? {} : options[:live_preview] @branch = options[:branch].nil? ? "" : options[:branch] @proxy_details = options[:proxy].nil? ? "" : options[:proxy] + @timeout = options[:timeout].nil? ? 3000 : options[:timeout] + @retryDelay = options[:retryDelay].nil? ? 3000 : options[:retryDelay] + @retryLimit = options[:retryLimit].nil? ? 5 : options[:retryLimit] + @errorRetry = options[:errorRetry].nil? ? [408, 429] : options[:errorRetry] + retry_options = { + "timeout" => @timeout.to_s, + "retryDelay"=> @retryDelay, + "retryLimit"=> @retryLimit, + "errorRetry" => @errorRetry + } raise Contentstack::Error.new("Proxy URL Should not be Empty") if @proxy_details.present? && @proxy_details[:url].empty? raise Contentstack::Error.new("Proxy Port Should not be Empty") if @proxy_details.present? && @proxy_details[:port].empty? - API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview, @proxy_details) + API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview, @proxy_details, retry_options) end def content_types From a870429cd1ec3d6295c3bc9047e061929246b1ce Mon Sep 17 00:00:00 2001 From: Sunil Lakshman Date: Fri, 29 Jul 2022 11:46:09 +0530 Subject: [PATCH 8/9] Added Retry Functionality --- lib/contentstack/api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/contentstack/api.rb b/lib/contentstack/api.rb index 139348b..0ae08be 100644 --- a/lib/contentstack/api.rb +++ b/lib/contentstack/api.rb @@ -130,7 +130,7 @@ def self.send_preview_request(path, q=nil) preview_host = @live_preview[:host] params = { "api_key" => @api_key, - "access_token"=> @access_token, + "authorization" => @live_preview[:management_token], "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout From 7c8d367367d25fda7bd1c7cb4167ec9ecb051fdb Mon Sep 17 00:00:00 2001 From: Sunil Lakshman Date: Wed, 3 Aug 2022 16:31:21 +0530 Subject: [PATCH 9/9] Added branch parameter in proxy request --- lib/contentstack/api.rb | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/contentstack/api.rb b/lib/contentstack/api.rb index 0ae08be..98a743e 100644 --- a/lib/contentstack/api.rb +++ b/lib/contentstack/api.rb @@ -111,12 +111,24 @@ def self.send_request(path, q=nil) proxy_username = @proxy_details[:username] proxy_password = @proxy_details[:password] - 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) + if !@branch.nil? && !@branch.empty? + 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, "branch" => @branch).read) + else + 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) + end + elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty? proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/") + + if !@branch.nil? && !@branch.empty? + 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, "branch" => @branch).read) + else + 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) + end + - 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) + end end @@ -148,13 +160,26 @@ def self.send_preview_request(path, q=nil) proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/") proxy_username = @proxy_details[:username] proxy_password = @proxy_details[:password] + + if !@branch.nil? && !@branch.empty? + 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, "branch" => @branch).read) + else + 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) + end + - 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) + elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty? proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/") - 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) + if !@branch.nil? && !@branch.empty? + 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, "branch" => @branch).read) + else + 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) + end + + end end