Skip to content

Commit

Permalink
reuse cloudtruth api cookies across all requests
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Nov 10, 2023
1 parent 685ab8d commit e5f2d9f
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 27 deletions.
43 changes: 21 additions & 22 deletions lib/kubetruth/ctapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@

module Kubetruth
class CtApi
include GemLogger::LoggerSupport

@@api_key = nil
@@api_url = nil
@@config = nil

def self.configure(api_key:, api_url:)
@@api_key = api_key
@@api_url = api_url
if api_key.nil? || api_url.nil?
@@config = nil
return
end
uri = URI(api_url)
config = ApiConfiguration.new
config.server_index = nil
config.scheme = uri.scheme
host_port = uri.host
host_port << ":#{uri.port}" unless [80, 443].include?(uri.port)
config.host = host_port
config.base_path = uri.path
config.api_key = {'ApiKeyAuth' => api_key}
config.api_key_prefix = {'ApiKeyAuth' => "Api-Key"}
config.logger = self.logger
config.use(:cookie_jar, jar: HTTP::CookieJar.new) # supply the cookie jar so that the same one is used across all connections
@@config = config
end

include GemLogger::LoggerSupport

attr_reader :client, :apis

class ApiConfiguration < CloudtruthClient::Configuration
Expand All @@ -42,26 +55,12 @@ def initialize(environment: "default", tag: nil)
@projects_mutex = Mutex.new
@templates_mutex = Mutex.new

raise ArgumentError.new("CtApi has not been configured") if @@api_key.nil? || @@api_url.nil?
@api_key = @@api_key
@api_url = @@api_url
raise ArgumentError.new("CtApi has not been configured") if @@config.nil?

@environment = environment
@tag = tag
uri = URI(@api_url)
config = ApiConfiguration.new
config.server_index = nil
config.scheme = uri.scheme
host_port = uri.host
host_port << ":#{uri.port}" unless [80, 443].include?(uri.port)
config.host = host_port
config.base_path = uri.path
config.api_key = {'ApiKeyAuth' => @api_key}
config.api_key_prefix = {'ApiKeyAuth' => "Api-Key"}
config.logger = logger
config.use(:cookie_jar)
# config.debugging = logger.debug?
@client = CloudtruthClient::ApiClient.new(config)
@client = CloudtruthClient::ApiClient.new(@@config)
@client.user_agent = "kubetruth/#{Kubetruth::VERSION}"
@apis = {
api: CloudtruthClient::ApiApi.new(@client),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e5f2d9f

Please sign in to comment.