Class: Killbill::Aviate::AviateClient

Inherits:
KillBillClient::Model::Resource
  • Object
show all
Defined in:
lib/aviate/client.rb

Constant Summary collapse

KILLBILL_AVIATE_PREFIX =
'/plugins/aviate-plugin/v1'
CATALOG_STATE_AVIATE =
'CATALOG_STATE_AVIATE'

Class Method Summary collapse

Class Method Details

.authenticate(email, password, options = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/aviate/client.rb', line 140

def authenticate(email, password, options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/auth"
  auth = Base64.strict_encode64("#{email}:#{password}")

  base_url = KillBillClient::API.base_uri
  uri = URI("#{base_url}#{path}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  request = Net::HTTP::Post.new(uri.path)
  request['Content-Type'] = 'application/json'
  request['X-Killbill-ApiKey'] = options&.dig(:api_key)
  request['X-Killbill-ApiSecret'] = options&.dig(:api_secret)
  request['Authorization'] = "Basic #{auth}"
  request.body = {}.to_json
  response = http.request(request)
  JSON.parse(response.body)
rescue StandardError => e
  e.message.to_s
end

.aviate_catalog?(account_id, options = nil) ⇒ Boolean

Returns true if the account/subscription is billed via an Aviate catalog (as opposed to a standard XML one). Any error (plugin not installed, missing/invalid JWT, network issue, etc.) is treated as "not Aviate" so callers can safely fall back to the standard catalog behavior.

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
# File 'lib/aviate/client.rb', line 25

def aviate_catalog?(, options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/catalog/info"
  request_options = build_request_options(options)

  response = KillBillClient::API.get path, catalog_params(), request_options
  JSON.parse(response.body)['state'] == CATALOG_STATE_AVIATE
rescue StandardError
  false
end

.aviate_plugin_available?(options = nil) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
# File 'lib/aviate/client.rb', line 11

def aviate_plugin_available?(options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/health/data"
  request_options = build_request_options(options)

  KillBillClient::API.get path, {}, request_options

  [true, nil]
rescue KillBillClient::API::ResponseError => e
  [false, e.message.to_s]
end

.aviate_plugin_installed(options = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/aviate/client.rb', line 63

def aviate_plugin_installed(options = nil)
  nodes_info = KillBillClient::Model::NodesInfo.nodes_info(options) || []
  plugins_info = nodes_info.empty? ? [] : (nodes_info.first.plugins_info || [])

  return [true, nil] if plugins_info.any? { |plugin| (plugin.plugin_name.include? 'aviate') && plugin.state == 'RUNNING' }

  [false, nil]
rescue KillBillClient::API::ResponseError => e
  [false, e.message.to_s]
end

.billing_meter_codes_for_plan(plan_name, account_id, options = nil) ⇒ Object

Returns the list of billing meter codes configured on the given plan's usage phases. Aviate catalogs record usage against a billing meter code rather than a standard catalog unit type, so this must be used instead of the plain KillBillClient catalog/phase lookup whenever aviate_catalog? is true.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/aviate/client.rb', line 39

def billing_meter_codes_for_plan(plan_name, , options = nil)
  return [] if plan_name.blank?

  path = "#{KILLBILL_AVIATE_PREFIX}/catalog/#{plan_name}/plan"
  request_options = build_request_options(options)

  response = KillBillClient::API.get path, catalog_params(), request_options
  plan = JSON.parse(response.body)

  codes = []
  Array(plan['phases']).each do |phase|
    Array(phase['usages']).each do |usage|
      Array(usage['tiers']).each do |tier|
        Array(tier['blocks']).each do |block|
          codes << block['billingMeterCode'] if block['billingMeterCode'].present?
        end
      end
    end
  end
  codes.uniq
rescue StandardError
  []
end

.create_wallet(account_id, wallet, options = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/aviate/client.rb', line 98

def create_wallet(, wallet, options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/wallet"
  body = {
    kbAccountId: ,
    currency: wallet[:currency],
    initCredit: {
      creditType: wallet[:credit_type],
      amount: wallet[:amount],
      expDate: wallet[:exp_date]
    },
    topOff: {
      topOffType: wallet[:top_off_type],
      lowWatermark: wallet[:low_watermark],
      amount: wallet[:top_off_amount],
      expDurationUnit: wallet[:exp_duration_unit],
      expDurationLength: wallet[:exp_duration_length]
    }
  }

  request_options = build_request_options(options)

  response = KillBillClient::API.post path, body.to_json, {}, request_options
  JSON.parse(response.body)
rescue StandardError => e
  JSON.parse(e.message)
end

.host_samples(subscription_id, sample_kind, from_date, to_date, options = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/aviate/client.rb', line 125

def host_samples(subscription_id, sample_kind, from_date, to_date, options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/health/host_samples"
  query_params = {
    from: from_date,
    to: to_date,
    category_and_sample_kind: "#{subscription_id},#{sample_kind}"
  }

  request_options = build_request_options(options)
  response = KillBillClient::API.get path, query_params, request_options
  parse_csv_response(response.body)
rescue KillBillClient::API::ResponseError
  []
end

.modify_wallet(wallet_params, options = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/aviate/client.rb', line 82

def modify_wallet(wallet_params, options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/wallet/#{wallet_params[:wallet_id]}/topOffConfig"
  body = {
    topOffType: wallet_params[:top_off_type],
    lowWatermark: wallet_params[:low_watermark],
    amount: wallet_params[:top_off_amount],
    expDurationUnit: wallet_params[:exp_duration_unit],
    expDurationLength: wallet_params[:exp_duration_length]
  }

  request_options = build_request_options(options)

  response = KillBillClient::API.put path, body.to_json, {}, request_options
  response.code
end

.retrieve_wallets(account_id, options = nil) ⇒ Object



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

def retrieve_wallets(, options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/wallet/#{}"
  request_options = build_request_options(options)

  response = KillBillClient::API.get path, {}, request_options
  JSON.parse(response.body)
end