Skip to content

Commit

Permalink
Add support for proxy settings
Browse files Browse the repository at this point in the history
  • Loading branch information
phylor committed Apr 22, 2024
1 parent d69f1f8 commit e4a4448
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -117,6 +117,8 @@ and api_client_secret. You can setup these ENV variables:
ENV['IOKI_RETRY_COUNT']
ENV['IOKI_RETRY_SLEEP_SECONDS']
ENV['IOKI_IGNORE_DEPRECATED_ATTRIBUTES']
ENV['IOKI_PROXY_URL']
ENV['IOKI_VERIFY_SSL']
```

or define them for one of the three supported apis with a prefix:
Expand Down
14 changes: 11 additions & 3 deletions lib/ioki/configuration.rb
Expand Up @@ -11,7 +11,9 @@ class Configuration
logger_options: { headers: true, bodies: false, log_level: :info },
retry_count: 3,
retry_sleep_seconds: 1,
ignore_deprecated_attributes: false
ignore_deprecated_attributes: false,
proxy_url: nil,
verify_ssl: true
}.freeze

CONFIG_KEYS = [
Expand All @@ -34,7 +36,9 @@ class Configuration
:oauth_token_callback,
:retry_count,
:retry_sleep_seconds,
:ignore_deprecated_attributes
:ignore_deprecated_attributes,
:proxy_url,
:verify_ssl
].freeze

attr_accessor(*CONFIG_KEYS)
Expand All @@ -61,6 +65,8 @@ def initialize(params = {})
@retry_count = params[:retry_count]
@retry_sleep_seconds = params[:retry_sleep_seconds]
@ignore_deprecated_attributes = params[:ignore_deprecated_attributes]
@proxy_url = params[:proxy_url]
@verify_ssl = params[:verify_ssl]
# you can pass in a custom Faraday::Connection instance:
@http_adapter = params[:http_adapter] || Ioki::HttpAdapter.get(self)
@custom_http_adapter = !!params[:http_adapter]
Expand Down Expand Up @@ -93,7 +99,9 @@ def self.values_from_env(env_prefix = '')
oauth_app_url: ENV.fetch("#{prefix}_OAUTH_APP_URL", nil),
retry_count: ENV.fetch("#{prefix}_RETRY_COUNT", nil),
retry_sleep_seconds: ENV.fetch("#{prefix}_RETRY_SLEEP_SECONDS", nil),
ignore_deprecated_attributes: ENV.fetch("#{prefix}_IGNORE_DEPRECATED_ATTRIBUTES", nil)
ignore_deprecated_attributes: ENV.fetch("#{prefix}_IGNORE_DEPRECATED_ATTRIBUTES", nil),
proxy_url: ENV.fetch("#{prefix}_PROXY_URL", nil),
verify_ssl: ENV.fetch("#{prefix}_VERIFY_SSL", 'true')&.downcase == 'true'
}.reject { |_key, value| value.nil? || value.to_s == '' }
end

Expand Down
8 changes: 7 additions & 1 deletion lib/ioki/http_adapter.rb
Expand Up @@ -7,7 +7,13 @@ module Ioki
class HttpAdapter

def self.get(config)
::Faraday.new(config.api_base_url, headers: headers(config)) do |f|
options = {
proxy: config.proxy_url,
ssl: { verify: config.verify_ssl },
headers: headers(config)
}

::Faraday.new(config.api_base_url, options) do |f|
f.adapter :net_http

f.request :authorization, 'Bearer', -> { config.token }
Expand Down
12 changes: 9 additions & 3 deletions spec/ioki/configuration_spec.rb
Expand Up @@ -36,7 +36,9 @@
:oauth_token_callback,
:retry_count,
:retry_sleep_seconds,
:ignore_deprecated_attributes
:ignore_deprecated_attributes,
:proxy_url,
:verify_ssl
)
end

Expand All @@ -52,6 +54,7 @@
describe 'default values and resetting' do
before do
allow(ENV).to receive(:fetch).and_return(nil)
allow(ENV).to receive(:fetch).with('IOKI_VERIFY_SSL', anything).and_return('true')
stub_const(
'EXPECTED_DEFAULTS',
{
Expand All @@ -66,7 +69,9 @@
logger_options: described_class::DEFAULT_VALUES[:logger_options],
retry_count: 3,
retry_sleep_seconds: 1,
ignore_deprecated_attributes: false
ignore_deprecated_attributes: false,
proxy_url: nil,
verify_ssl: true
}.freeze
)
end
Expand All @@ -89,7 +94,8 @@

expect { config.reset! }.to change {
config.send(attribute)
}.from(:dummy_value).to(EXPECTED_DEFAULTS[attribute])
}.from(:dummy_value).to(EXPECTED_DEFAULTS[attribute]),
"#{attribute} was expected to change to #{EXPECTED_DEFAULTS[attribute]}"
end
end
end
Expand Down

0 comments on commit e4a4448

Please sign in to comment.