Class: Genba::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/genba/client.rb,
lib/genba/client/ping.rb,
lib/genba/client/orders.rb,
lib/genba/client/prices.rb,
lib/genba/client/products.rb,
lib/genba/client/promotions.rb,
lib/genba/client/reservations.rb,
lib/genba/client/direct_entitlement.rb,
lib/genba/client/direct_entitlements/activations.rb,
lib/genba/client/direct_entitlements/redemptions.rb

Overview

Genba API Client

Defined Under Namespace

Classes: DirectEntitlement, Orders, Ping, Prices, Products, Promotions, Reservations

Constant Summary collapse

SANDBOX_API_URL =
'https://sandbox.genbadigital.io/api/v3-0'.freeze
PRODUCTION_API_URL =
'https://api.genbadigital.io/api/v3-0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, account_id:, cert:, key:, sandbox: false, options: {}) ⇒ Client

Desribe the behaviour of the method

Attributes

  • config - Genba API credential attribute

Options



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/genba/client.rb', line 22

def initialize(resource:, account_id:, cert:, key:, sandbox: false, options: {})
  @api_url = sandbox ? SANDBOX_API_URL : PRODUCTION_API_URL
  @resource = resource
  @account_id = 
  @cert = cert
  @key = key
  @tenant = 'aad.genbadigital.io'
  @authority_url = "https://login.microsoftonline.com/#{@tenant}"
  @client_id = "https://aad-snd.genbadigital.io/#{@account_id}"

  @open_timeout = options[:open_timeout] || 15
  @read_timeout = options[:read_timeout] || 60
  @max_retry = options[:max_retry] || 0
  @retry_delay = options[:retry_delay] || 2
end

Instance Attribute Details

#customer_account_idObject

Returns the value of attribute customer_account_id.



6
7
8
# File 'lib/genba/client.rb', line 6

def 
  @customer_account_id
end

#max_retryObject

Returns the value of attribute max_retry.



6
7
8
# File 'lib/genba/client.rb', line 6

def max_retry
  @max_retry
end

#open_timeoutObject

Returns the value of attribute open_timeout.



6
7
8
# File 'lib/genba/client.rb', line 6

def open_timeout
  @open_timeout
end

#read_timeoutObject

Returns the value of attribute read_timeout.



6
7
8
# File 'lib/genba/client.rb', line 6

def read_timeout
  @read_timeout
end

#retry_delayObject

Returns the value of attribute retry_delay.



6
7
8
# File 'lib/genba/client.rb', line 6

def retry_delay
  @retry_delay
end

Instance Method Details

#direct_entitlementObject



175
176
177
# File 'lib/genba/client.rb', line 175

def direct_entitlement
  DirectEntitlement.new(self)
end

#execute_request(method:, url:, payload: {}, headers: {}, options: {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/genba/client.rb', line 96

def execute_request(method:, url:, payload: {}, headers: {}, options: {})
  request_opts = {
    headers: headers,
    method: method,
    payload: payload,
    url: url
  }
  other_opts = {
    open_timeout: options[:open_timeout] || @open_timeout,
    read_timeout: options[:read_timeout] || @read_timeout,
    max_retry: options[:max_retry] || @max_retry
  }

  Genba::Util.log_debug "API Headers: #{headers.inspect}"
  Genba::Util.log_debug "Options: #{other_opts}"
  Genba::Util.log_info "#{method.upcase}: #{url}"
  Genba::Util.log_info "payload: #{payload}" if payload.present?

  request_opts.merge! other_opts
  execute_request_with_rescues(request_opts, other_opts[:max_retry])
end

#execute_request_with_rescues(request_opts, max_retry) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/genba/client.rb', line 118

def execute_request_with_rescues(request_opts, max_retry)
  num_try = 0
  begin
    response = RestClient::Request.execute(request_opts)
  rescue RestClient::Exception, SocketError, Errno::ECONNREFUSED, NoMethodError => e
    if e.class.parent == RestClient && valid_json?(e.http_body)
      Genba::Util.log_error "#{e.class} => #{e.message}\n#{e.http_body}"
      raise e
      # response = OpenStruct.new(code: e.http_code, body: e.http_body, headers: {})
      # return response
    end
    Genba::Util.log_error "#{e.class} => #{e.message}"

    num_try += 1
    sleep @retry_delay

    if num_try <= max_retry
      Genba::Util.log_error "retry ====> #{num_try}"
      retry
    end

    raise e
  end
  response
end

#generate_tokenObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/genba/client.rb', line 38

def generate_token
  unless token_valid?
    certificate = OpenSSL::X509::Certificate.new File.open(@cert)
    x5t = Base64.encode64 OpenSSL::Digest::SHA1.new(certificate.to_der).digest
    payload = {
      exp: (Time.now + 60*60*24).to_i,
      aud: "#{@authority_url}/oauth2/token",
      iss: @client_id,
      sub: @client_id,
    }
    rsa_private = OpenSSL::PKey::RSA.new File.open(@key)
    token = JWT.encode payload, rsa_private, 'RS256', { x5t: x5t }

    body = {
      resource: @resource,
      client_id: @client_id,
      grant_type: 'client_credentials',
      scope: 'https://graph.microsoft.com/.default',
      client_assertion_type: 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
      client_assertion: token,
      tenant: @tenant
    }

    response = RestClient.post(
      "#{@authority_url}/oauth2/token",
      body,
      headers: { accept: 'application/json', 'Content-Type': 'application/json' }
    )
    parsed_response = decode_json(response.body)
    @id_token = parsed_response['access_token']
    @expires_on = Time.at(parsed_response['expires_on'].to_i)
  end
  raw_token
end

#ordersObject



171
172
173
# File 'lib/genba/client.rb', line 171

def orders
  Orders.new(self)
end

#pingObject



151
152
153
# File 'lib/genba/client.rb', line 151

def ping
  Ping.new(self)
end

#pricesObject



159
160
161
# File 'lib/genba/client.rb', line 159

def prices
  Prices.new(self)
end

#productsObject



155
156
157
# File 'lib/genba/client.rb', line 155

def products
  Products.new(self)
end

#promotionsObject



163
164
165
# File 'lib/genba/client.rb', line 163

def promotions
  Promotions.new(self)
end

#reservationsObject



167
168
169
# File 'lib/genba/client.rb', line 167

def reservations
  Reservations.new(self)
end

#rest_get_with_token(path, query_params = {}, headers = {}, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/genba/client.rb', line 73

def rest_get_with_token(path, query_params = {}, headers = {}, options = {})
  genba_headers = token.merge(headers)
  api_url = "#{@api_url}#{path}"
  api_url += "?#{query_params.to_query}" unless query_params.empty?
  response = execute_request(method: :get, url: api_url,
                             headers: genba_headers, options: options)
  from_rest_client_response(response)
end

#rest_post_with_token(path, body = {}, headers = {}, options = {}) ⇒ Object



89
90
91
92
93
94
# File 'lib/genba/client.rb', line 89

def rest_post_with_token(path, body = {}, headers = {}, options = {})
  genba_headers = token.merge(headers)
  response = execute_request(method: :post, url: "#{@api_url}#{path}",
                             payload: encode_json(body), headers: genba_headers, options: options)
  from_rest_client_response(response)
end

#rest_put_with_token(path, body = {}, headers = {}, options = {}) ⇒ Object



82
83
84
85
86
87
# File 'lib/genba/client.rb', line 82

def rest_put_with_token(path, body = {}, headers = {}, options = {})
  genba_headers = token.merge(headers)
  response = execute_request(method: :put, url: "#{@api_url}#{path}",
                             payload: encode_json(body), headers: genba_headers, options: options)
  from_rest_client_response(response)
end

#valid_json?(json) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
147
148
149
# File 'lib/genba/client.rb', line 144

def valid_json?(json)
  JSON.parse(json)
  return true
rescue JSON::ParserError => e
  return false
end