diff --git a/lib/zoho_hub.rb b/lib/zoho_hub.rb index 03f8d90..8768fe8 100644 --- a/lib/zoho_hub.rb +++ b/lib/zoho_hub.rb @@ -39,7 +39,13 @@ def on_refresh(&block) def setup_connection(params = {}) raise "ERROR: #{params[:error]}" if params[:error] - connection_params = params.dup.slice(:access_token, :expires_in, :api_domain, :refresh_token) + connection_params = params.dup.slice( + :access_token, + :expires_in, + :api_domain, + :refresh_token, + :proxy + ) @connection = Connection.new(**connection_params) end diff --git a/lib/zoho_hub/cli/read_modules.rb b/lib/zoho_hub/cli/read_modules.rb index 2a8b7c0..fce5f72 100644 --- a/lib/zoho_hub/cli/read_modules.rb +++ b/lib/zoho_hub/cli/read_modules.rb @@ -58,8 +58,10 @@ def good_run(argv, env) def setup_connection ZohoHub.configure do |config| - config.client_id = @options[:client_id] || ENV['ZOHO_CLIENT_ID'] - config.secret = @options[:secret] || ENV['ZOHO_SECRET'] + config.client_id = @options[:client_id] || ENV['ZOHO_CLIENT_ID'] + config.secret = @options[:secret] || ENV['ZOHO_SECRET'] + config.proxy_address = @options[:proxy][:proxy_address] || nil + config.proxy_port = @options[:proxy][:proxy_port] || nil end refresh_token = @options[:refresh_token] || ENV['ZOHO_REFRESH_TOKEN'] diff --git a/lib/zoho_hub/connection.rb b/lib/zoho_hub/connection.rb index d2b8a42..886db1f 100644 --- a/lib/zoho_hub/connection.rb +++ b/lib/zoho_hub/connection.rb @@ -22,6 +22,7 @@ def infer_api_domain end attr_accessor :debug, :access_token, :expires_in, :api_domain, :refresh_token + attr_reader :proxy_address, :proxy_port # This is a block to be run when the token is refreshed. This way you can do whatever you want # with the new parameters returned by the refresh method. @@ -31,11 +32,13 @@ def infer_api_domain BASE_PATH = '/crm/v2/' - def initialize(access_token: nil, api_domain: nil, expires_in: 3600, refresh_token: nil) + def initialize(access_token: nil, api_domain: nil, expires_in: 3600, refresh_token: nil, proxy: {}) @access_token = access_token @expires_in = expires_in @api_domain = api_domain || self.class.infer_api_domain @refresh_token ||= refresh_token # do not overwrite if it's already set + @proxy_address = proxy[:proxy_address] + @proxy_port = proxy[:proxy_port] end def get(path, params = {}) @@ -114,7 +117,7 @@ def authorization_header end def adapter - Faraday.new(url: base_url) do |conn| + Faraday.new(url: base_url, proxy: proxy_url) do |conn| conn.headers = authorization_header if access_token? conn.use FaradayMiddleware::EncodeJson conn.use FaradayMiddleware::ParseJson @@ -123,5 +126,11 @@ def adapter conn.adapter Faraday.default_adapter end end + + def proxy_url + return unless proxy_address && proxy_port + + "https://#{proxy_address}:#{proxy_port}" + end end end diff --git a/spec/zoho_hub/connection_spec.rb b/spec/zoho_hub/connection_spec.rb index d49cfa7..38635ac 100644 --- a/spec/zoho_hub/connection_spec.rb +++ b/spec/zoho_hub/connection_spec.rb @@ -55,6 +55,17 @@ expect(result).to eq('custom domain') end + + it 'allows to set proxy' do + connection = described_class.new(access_token: '', + proxy: { + address: 'proxy address', + port: 'proxy port' + }) + + expect(connection.proxy_address).to eq('proxy address') + expect(connection.proxy_port).to eq('proxy port') + end end end end