Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/prometheus/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ module ApiClient
# @option options [String] :url Server base URL.
# @option options [Hash] :credentials Authentication credentials.
# @option options [Hash] :options Options used to define connection.
# @option options [Hash] :params URI query unencoded key/value pairs.
# @option options [Hash] :headers Unencoded HTTP header key/value pairs.
# @option options [Hash] :request Request options.
# @option options [Hash] :ssl SSL options.
# @option options [Hash] :proxy Proxy options.
# @option options [String] :proxy Proxy url.
#
# A default client is created if options is omitted.
def self.client(options = {})
Expand Down
64 changes: 41 additions & 23 deletions lib/prometheus/api_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ class RequestError < StandardError; end
# Create a Prometheus API client:
#
# @param [Hash] options
# @option options [String] :url server base URL.
# @option options [String] :url Server base URL.
# @option options [Hash] :credentials Authentication credentials.
# @option options [Hash] :options Options used to define connection.
# @option options [Hash] :params URI query unencoded key/value pairs.
# @option options [Hash] :headers Unencoded HTTP header key/value pairs.
# @option options [Hash] :request Request options.
# @option options [Hash] :ssl SSL options.
# @option options [Hash] :proxy Proxy options.
# @option options [String] :proxy Proxy url.
#
# A default client is created if options is omitted.
def initialize(options = {})
Expand Down Expand Up @@ -109,39 +108,58 @@ def run_command(command, options)

# Helper function to evalueate the low level proxy option
def faraday_proxy(options)
options[:http_proxy_uri] if options[:http_proxy_uri]
return options[:proxy] if options[:proxy]

proxy = options[:options]
proxy[:http_proxy_uri] if proxy[:http_proxy_uri]
end

# Helper function to evalueate the low level ssl option
def faraday_verify_ssl(options)
return unless options[:verify_ssl]

{
verify: options[:verify_ssl] != OpenSSL::SSL::VERIFY_NONE,
cert_store: options[:ssl_cert_store],
}
def faraday_ssl(options)
return options[:ssl] if options[:ssl]

ssl = options[:options]
if ssl[:verify_ssl] || ssl[:ssl_cert_store]
{
verify: ssl[:verify_ssl] != OpenSSL::SSL::VERIFY_NONE,
cert_store: ssl[:ssl_cert_store],
}
end
end

# Helper function to evalueate the low level headers option
def faraday_headers(credentials)
return unless credentials[:token]
def faraday_headers(options)
return options[:headers] if options[:headers]

headers = options[:credentials]
if headers[:token]
{
Authorization: 'Bearer ' + headers[:token].to_s,
}
end
end

{
Authorization: 'Bearer ' + credentials[:token].to_s,
}
# Helper function to evalueate the low level headers option
def faraday_request(options)
return options[:request] if options[:request]

request = options[:options]
if request[:open_timeout] || request[:timeout]
{
open_timeout: request[:open_timeout],
timeout: request[:timeout],
}
end
end

# Helper function to create the args for the low level client
def faraday_options(options)
{
url: options[:url] + options[:path],
proxy: faraday_proxy(options[:options]),
ssl: faraday_verify_ssl(options[:options]),
headers: faraday_headers(options[:credentials]),
request: {
open_timeout: options[:options][:open_timeout],
timeout: options[:options][:timeout],
},
proxy: faraday_proxy(options),
ssl: faraday_ssl(options),
headers: faraday_headers(options),
request: faraday_request(options),
}
end
end
Expand Down