lib/agx/content/client.rb in agx-0.3.1 vs lib/agx/content/client.rb in agx-0.3.2
- old
+ new
@@ -1,21 +1,21 @@
module Agx
module Content
class Client
attr_accessor :client_id, :client_secret, :site, :token_url, :version
- def initialize(client_id: nil, client_secret: nil, version: nil, prod: true)
+ def initialize(client_id: nil, client_secret: nil, version: nil, prod: true, access_token: nil, expires_at: nil)
domain = (prod ? "agxplatform.com" : "qaagxplatform.com")
@client_id = client_id || ENV['AGX_CONTENT_CLIENT_ID']
@client_secret = client_secret || ENV['AGX_CONTENT_CLIENT_SECRET']
@site = "https://refdata.#{domain}"
@token_url = "https://auth.#{domain}/identity/connect/token"
@version = version || "v1"
@client = set_client
@token = {
- access_token: nil,
- expires_at: nil
+ access_token: access_token,
+ expires_at: expires_at
}
end
def get(resource, params = {})
validate_credentials
@@ -27,10 +27,29 @@
rescue => e
handle_error(e)
end
end
+ def current_token
+ if @token[:access_token].nil? || @token[:expires_at].nil?
+ new_token = api_token
+ else
+ oauth_token = OAuth2::AccessToken.new(
+ @client,
+ @token[:access_token],
+ {expires_at: @token[:expires_at]}
+ )
+ if Time.now.to_i + 180 >= @token[:expires_at] || oauth_token.expired?
+ new_token = api_token
+ else
+ new_token = oauth_token
+ end
+ end
+
+ new_token
+ end
+
protected
def validate_credentials
unless @client_id && @client_secret
error = Agx::Error.new("agX Client Credentials Not Set", {title: "AGX_CREDENTIALS_ERROR"})
@@ -70,28 +89,9 @@
rescue Oj::ParseError
end
error_to_raise = Agx::Error.new(error.message, error_params)
raise error_to_raise
- end
-
- def current_token
- if @token[:access_token].nil? || @token[:expires_at].nil?
- new_token = api_token
- else
- oauth_token = OAuth2::AccessToken.new(
- @client,
- @token[:access_token],
- {expires_at: @token[:expires_at]}
- )
- if Time.now.to_i + 180 >= @token[:expires_at] || oauth_token.expired?
- new_token = api_token
- else
- new_token = oauth_token
- end
- end
-
- new_token
end
def api_token
begin
new_token = @client.client_credentials.get_token(