Skip to content

Commit 1e344ff

Browse files
Added proxy feature in ruby SDK
1 parent 6cf647f commit 1e344ff

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/contentstack/api.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,25 @@ def self.send_request(path, q=nil)
8080
if !@branch.nil? && !@branch.empty?
8181
params["branch"] = @branch
8282
end
83-
ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", params).read)
83+
if @proxy_details.empty?
84+
85+
ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", params).read)
86+
87+
elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].present? && @proxy_details[:password].present?
88+
89+
proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}")
90+
proxy_username = @proxy_details[:username]
91+
proxy_password = @proxy_details[:password]
92+
93+
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)
94+
95+
elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty?
96+
proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}")
97+
proxy_auth = {"proxy" => proxy_uri}
98+
params_with_proxy = params.merge(proxy_auth)
99+
ActiveSupport::JSON.decode(open("#{@host}#{@api_version}#{path}#{query}", params_with_proxy).read)
100+
101+
end
84102
end
85103

86104
def self.send_preview_request(path, q=nil)

lib/contentstack/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def initialize(api_key, delivery_token, environment, options={})
1313
@host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host]
1414
@live_preview = !options.key?(:live_preview) ? {} : options[:live_preview]
1515
@branch = options[:branch].nil? ? "" : options[:branch]
16+
@proxy_details = options[:proxy].nil? ? "" : options[:proxy]
17+
raise Contentstack::Error.new("Proxy URL Should not be Empty") if @proxy_details.present? && @proxy_details[:url].empty?
18+
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)
1620
API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview)
1721
end
1822

0 commit comments

Comments
 (0)