Class: Agx::Sync::Client
- Inherits:
-
Object
- Object
- Agx::Sync::Client
- Defined in:
- lib/agx/sync/client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#authorize_url ⇒ Object
Returns the value of attribute authorize_url.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#host ⇒ Object
Returns the value of attribute host.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#site ⇒ Object
Returns the value of attribute site.
-
#sync_id ⇒ Object
Returns the value of attribute sync_id.
-
#token_expires_at ⇒ Object
Returns the value of attribute token_expires_at.
-
#token_url ⇒ Object
Returns the value of attribute token_url.
-
#transaction_id ⇒ Object
Returns the value of attribute transaction_id.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #current_token ⇒ Object
- #end_transaction ⇒ Object
- #get(resource, start_time = nil) ⇒ Object
- #get_nt(resource, start_time = nil) ⇒ Object
-
#initialize(client_id: nil, client_secret: nil, version: nil, sync_id: nil, access_token: nil, refresh_token: nil, token_expires_at: nil, transaction_id: nil, prod: true) ⇒ Client
constructor
A new instance of Client.
- #put(resource, body) ⇒ Object
- #start_transaction ⇒ Object
Constructor Details
#initialize(client_id: nil, client_secret: nil, version: nil, sync_id: nil, access_token: nil, refresh_token: nil, token_expires_at: nil, transaction_id: nil, prod: true) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/agx/sync/client.rb', line 8 def initialize(client_id: nil, client_secret: nil, version: nil, sync_id: nil, access_token: nil, refresh_token: nil, token_expires_at: nil, transaction_id: nil, prod: true) domain = (prod ? "agxplatform.com" : "qaagxplatform.com") @client_id = client_id || ENV['AGX_SYNC_CLIENT_ID'] @client_secret = client_secret || ENV['AGX_SYNC_CLIENT_SECRET'] @site = "https://sync.#{domain}" @host = host || "sync.#{domain}" @authorize_url = "https://auth.#{domain}/identity/connect/Authorize" @token_url = "https://auth.#{domain}/identity/connect/Token" @oic_config_url = "https://auth.#{domain}/.well-known/openid-configuration" @version = version || "v4" @sync_id = sync_id @api_url = "#{@site}/api/#{@version}/Account/#{@sync_id}/" @headers = { 'Content-Type' => "application/json", 'Accept' => "application/json", 'oauth-scopes' => "Sync", 'Host' => @host } @client = set_client @token = { access_token: access_token, refresh_token: refresh_token, expires_at: token_expires_at } @transaction_id = transaction_id end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def access_token @access_token end |
#authorize_url ⇒ Object
Returns the value of attribute authorize_url.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def @authorize_url end |
#client_id ⇒ Object
Returns the value of attribute client_id.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def client_secret @client_secret end |
#host ⇒ Object
Returns the value of attribute host.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def host @host end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def refresh_token @refresh_token end |
#site ⇒ Object
Returns the value of attribute site.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def site @site end |
#sync_id ⇒ Object
Returns the value of attribute sync_id.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def sync_id @sync_id end |
#token_expires_at ⇒ Object
Returns the value of attribute token_expires_at.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def token_expires_at @token_expires_at end |
#token_url ⇒ Object
Returns the value of attribute token_url.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def token_url @token_url end |
#transaction_id ⇒ Object
Returns the value of attribute transaction_id.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def transaction_id @transaction_id end |
#version ⇒ Object
Returns the value of attribute version.
4 5 6 |
# File 'lib/agx/sync/client.rb', line 4 def version @version end |
Instance Method Details
#current_token ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/agx/sync/client.rb', line 119 def current_token new_token = OAuth2::AccessToken.new @client, @token[:access_token], { expires_at: @token[:expires_at], refresh_token: @token[:refresh_token] } if Time.now.to_i + 90 >= @token[:expires_at] new_token = new_token.refresh! @token[:access_token] = new_token.token @token[:refresh_token] = new_token.refresh_token @token[:expires_at] = new_token.expires_at end new_token end |
#end_transaction ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/agx/sync/client.rb', line 101 def end_transaction validate_credentials begin end_transaction_request = current_token.delete( "#{@api_url}Transaction/#{@transaction_id}", :headers => @headers ) return true rescue => e if e.response && e.response.status && e.response.status.to_i >= 400 return true else handle_error(e) end end end |
#get(resource, start_time = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/agx/sync/client.rb', line 37 def get(resource, start_time = nil) validate_sync_attributes url = "#{@api_url}#{resource}?transactionId=#{@transaction_id}" if !start_time.nil? url = "#{@api_url}#{resource}?startTime=#{start_time}&transactionId=#{@transaction_id}" end begin response = current_token.get(url, :headers => @headers) parse_response(response.body) rescue => e handle_error(e) end end |
#get_nt(resource, start_time = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/agx/sync/client.rb', line 53 def get_nt(resource, start_time = nil) validate_nt_sync_attributes url = "#{@api_url}#{resource}" if !start_time.nil? url = "#{@api_url}#{resource}?startTime=#{start_time}" end begin response = current_token.get(url, :headers => @headers) parse_response(response.body) rescue => e handle_error(e) end end |
#put(resource, body) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/agx/sync/client.rb', line 69 def put(resource, body) validate_sync_attributes url = "#{@api_url}#{resource}?transactionId=#{@transaction_id}" begin response = current_token.put(url, {:body => body, :headers => @headers}) parse_response(response.body) rescue => e handle_error(e) end end |
#start_transaction ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/agx/sync/client.rb', line 82 def start_transaction validate_credentials if !@transaction_id.nil? end_transaction end begin transaction_request = current_token.get( "#{@api_url}Transaction", :headers => @headers ) @transaction_id = Oj.load(transaction_request.body) rescue => e handle_error(e) end @transaction_id end |