Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxy to connection config #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading