Skip to content

Commit

Permalink
Add proxy to connection config
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan Paratskyi committed Jul 8, 2024
1 parent a8d7b21 commit 3e4a611
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/zoho_hub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/zoho_hub/cli/read_modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
13 changes: 11 additions & 2 deletions lib/zoho_hub/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = {})
Expand Down Expand Up @@ -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
Expand All @@ -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
11 changes: 11 additions & 0 deletions spec/zoho_hub/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3e4a611

Please sign in to comment.