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

Config | Proxy settings #372

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ and api_client_secret. You can setup these ENV variables:
ENV['IOKI_OAUTH_APP_URL']
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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