bill_forward 1.2014.296 → 1.2015.183
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +29 -27
- data/.idea/compiler.xml +23 -23
- data/.idea/copyright/profiles_settings.xml +2 -2
- data/.idea/encodings.xml +5 -5
- data/.idea/inspectionProfiles/Project_Default.xml +10 -10
- data/.idea/inspectionProfiles/profiles_settings.xml +6 -6
- data/.idea/misc.xml +23 -23
- data/.idea/modules.xml +9 -9
- data/.idea/scopes/scope_settings.xml +4 -4
- data/.idea/vcs.xml +7 -7
- data/.rspec +1 -1
- data/Gemfile +13 -9
- data/LICENSE.md +22 -22
- data/README.md +285 -227
- data/Rakefile +73 -73
- data/bill_forward.gemspec +28 -29
- data/bill_forward.iml +28 -28
- data/lib/bill_forward/billing_entity.rb +295 -262
- data/lib/bill_forward/client.rb +350 -355
- data/lib/bill_forward/entities/account.rb +18 -18
- data/lib/bill_forward/entities/amendments/issue_invoice_amendment.rb +10 -0
- data/lib/bill_forward/entities/amendments/product_rate_plan_migration_amendment.rb +18 -0
- data/lib/bill_forward/entities/api_configuration.rb +12 -0
- data/lib/bill_forward/entities/invoice.rb +29 -3
- data/lib/bill_forward/entities/invoice_parts/taxation_link.rb +5 -5
- data/lib/bill_forward/entities/organisation.rb +36 -36
- data/lib/bill_forward/entities/payment_method.rb +4 -4
- data/lib/bill_forward/entities/payment_method_subscription_link.rb +5 -5
- data/lib/bill_forward/entities/pricing_component.rb +21 -21
- data/lib/bill_forward/entities/pricing_component_tier.rb +5 -5
- data/lib/bill_forward/entities/pricing_component_value.rb +5 -5
- data/lib/bill_forward/entities/pricing_component_value_migration_amendment_mapping.rb +31 -0
- data/lib/bill_forward/entities/product.rb +5 -5
- data/lib/bill_forward/entities/product_rate_plan.rb +43 -19
- data/lib/bill_forward/entities/profile.rb +14 -14
- data/lib/bill_forward/entities/refund.rb +5 -0
- data/lib/bill_forward/entities/role.rb +3 -3
- data/lib/bill_forward/entities/stripe_ach_token.rb +9 -0
- data/lib/bill_forward/entities/subscription.rb +83 -53
- data/lib/bill_forward/entities/subscription_charge.rb +14 -0
- data/lib/bill_forward/insertable_entity.rb +21 -31
- data/lib/bill_forward/mutable_entity.rb +26 -46
- data/lib/bill_forward/resource_path.rb +10 -10
- data/lib/bill_forward/type_check.rb +20 -20
- data/lib/bill_forward/version.rb +4 -4
- data/lib/bill_forward.rb +17 -17
- data/scratch/Gemfile +9 -0
- data/scratch/scratch.example.rb +8 -0
- data/spec/component/account_spec.rb +199 -199
- data/spec/component/billing_entity_spec.rb +152 -152
- data/spec/functional/account_spec.rb +24 -24
- data/spec/functional/bad_citizen/account_spec.rb +102 -102
- data/spec/functional/bad_citizen/credit_note_spec.rb +14 -13
- data/spec/functional/bad_citizen/payment_method_spec.rb +1 -1
- data/spec/functional/bad_citizen/product_rate_plan_migration_amendment_spec.rb +379 -0
- data/spec/functional/bad_citizen/product_rate_plan_spec.rb +103 -47
- data/spec/functional/bad_citizen/product_spec.rb +16 -9
- data/spec/functional/bad_citizen/situational/authorize_net_token_spec.rb +4 -0
- data/spec/functional/bad_citizen/situational/malordered_entity_spec.rb +1 -1
- data/spec/functional/bad_citizen/situational/payment_method_spec.rb +6 -1
- data/spec/functional/bad_citizen/situational/subscription_chargeable_spec.rb +96 -65
- data/spec/functional/bad_citizen/subscription_spec.rb +99 -60
- data/spec/functional/bad_citizen/subscription_with_credit_spec.rb +121 -91
- data/spec/functional/bad_citizen/unit_of_measure_spec.rb +14 -7
- data/spec/functional/client_spec.rb +23 -23
- data/spec/functional/organisation_spec.rb +27 -27
- data/spec/setup_test_constants.rb +72 -72
- data/spec/spec_helper.rb +10 -10
- data/spec/syntax/account_spec.rb +23 -23
- data/spec/syntax/billing_entity_spec.rb +92 -92
- data/tools/RSpec hardcoded.sublime-build +16 -0
- data/tools/RSpec.sublime-build +13 -0
- data/tools/Ruby legacy.sublime-build +7 -0
- data/tools/local_bundle_build.sh +8 -0
- data/tools/local_bundle_install.sh +9 -0
- metadata +30 -85
data/lib/bill_forward/client.rb
CHANGED
@@ -1,355 +1,350 @@
|
|
1
|
-
module BillForward
|
2
|
-
class ClientException < Exception
|
3
|
-
attr_accessor :response
|
4
|
-
|
5
|
-
def initialize(message, response=nil)
|
6
|
-
super(message)
|
7
|
-
|
8
|
-
begin
|
9
|
-
if response.nil?
|
10
|
-
self.response = nil
|
11
|
-
else
|
12
|
-
self.response = JSON.parse response
|
13
|
-
end
|
14
|
-
rescue => e
|
15
|
-
self.response = nil
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class ClientInstantiationException < Exception
|
21
|
-
end
|
22
|
-
|
23
|
-
class ApiError < Exception
|
24
|
-
attr_reader :json
|
25
|
-
attr_reader :raw
|
26
|
-
|
27
|
-
def initialize(json, raw)
|
28
|
-
@json = json
|
29
|
-
@raw = raw
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
class ApiAuthorizationError < ApiError
|
34
|
-
end
|
35
|
-
|
36
|
-
class
|
37
|
-
|
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
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
-
|
124
|
-
def
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
end
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
when
|
266
|
-
message = "
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
#
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
end
|
352
|
-
end
|
353
|
-
end
|
354
|
-
end
|
355
|
-
end
|
1
|
+
module BillForward
|
2
|
+
class ClientException < Exception
|
3
|
+
attr_accessor :response
|
4
|
+
|
5
|
+
def initialize(message, response=nil)
|
6
|
+
super(message)
|
7
|
+
|
8
|
+
begin
|
9
|
+
if response.nil?
|
10
|
+
self.response = nil
|
11
|
+
else
|
12
|
+
self.response = JSON.parse response
|
13
|
+
end
|
14
|
+
rescue => e
|
15
|
+
self.response = nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class ClientInstantiationException < Exception
|
21
|
+
end
|
22
|
+
|
23
|
+
class ApiError < Exception
|
24
|
+
attr_reader :json
|
25
|
+
attr_reader :raw
|
26
|
+
|
27
|
+
def initialize(json, raw)
|
28
|
+
@json = json
|
29
|
+
@raw = raw
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class ApiAuthorizationError < ApiError
|
34
|
+
end
|
35
|
+
|
36
|
+
class ApiUnexpectedResponseFormatError < ApiError
|
37
|
+
end
|
38
|
+
|
39
|
+
class ApiTokenException < ClientException
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
class Client
|
44
|
+
@@payload_verbs = ['post', 'put']
|
45
|
+
@@no_payload_verbs = ['get', 'delete']
|
46
|
+
@@all_verbs = @@payload_verbs + @@no_payload_verbs
|
47
|
+
|
48
|
+
attr_accessor :host
|
49
|
+
attr_accessor :use_logging
|
50
|
+
attr_accessor :api_token
|
51
|
+
|
52
|
+
# provide access to self statics
|
53
|
+
class << self
|
54
|
+
attr_accessor :all_verbs
|
55
|
+
|
56
|
+
# default client is a singleton client
|
57
|
+
attr_reader :default_client
|
58
|
+
def default_client=(default_client)
|
59
|
+
if (default_client == nil)
|
60
|
+
# meaningless, but required for resetting this class after a test run
|
61
|
+
@default_client = nil
|
62
|
+
return
|
63
|
+
end
|
64
|
+
|
65
|
+
TypeCheck.verifyObj(Client, default_client, 'default_client')
|
66
|
+
@default_client = default_client
|
67
|
+
end
|
68
|
+
def default_client()
|
69
|
+
raise ClientInstantiationException.new("Failed to get default BillForward API Client; " +
|
70
|
+
"'default_client' is nil. Please set a 'default_client' first.") if
|
71
|
+
@default_client.nil?
|
72
|
+
@default_client
|
73
|
+
end
|
74
|
+
end
|
75
|
+
@all_verbs = @@all_verbs
|
76
|
+
|
77
|
+
|
78
|
+
# Constructs a client, and sets it to be used as the default client.
|
79
|
+
# @param options={} [Hash] Options with which to construct client
|
80
|
+
#
|
81
|
+
# @return [Client] The constructed client
|
82
|
+
def self.make_default_client(options)
|
83
|
+
constructedClient = self.new(options)
|
84
|
+
self.default_client = constructedClient
|
85
|
+
end
|
86
|
+
|
87
|
+
def initialize(options={})
|
88
|
+
TypeCheck.verifyObj(Hash, options, 'options')
|
89
|
+
@use_logging = options[:use_logging]
|
90
|
+
|
91
|
+
if options[:host]
|
92
|
+
@host = options[:host]
|
93
|
+
else
|
94
|
+
raise ClientInstantiationException.new "Failed to initialize BillForward API Client\n" +
|
95
|
+
"Required parameters: :host, and either [:api_token] or all of [:client_id, :client_secret, :username, :password].\n" +
|
96
|
+
"Supplied Parameters: #{options}"
|
97
|
+
end
|
98
|
+
|
99
|
+
if options[:use_proxy]
|
100
|
+
@use_proxy = options[:use_proxy]
|
101
|
+
@proxy_url = options[:proxy_url]
|
102
|
+
end
|
103
|
+
|
104
|
+
if options[:api_token]
|
105
|
+
@api_token = options[:api_token]
|
106
|
+
else
|
107
|
+
@api_token = nil
|
108
|
+
if options[:client_id] and options[:client_secret] and options[:username] and options[:password]
|
109
|
+
@client_id = options[:client_id]
|
110
|
+
@client_secret = options[:client_secret]
|
111
|
+
@username = options[:username]
|
112
|
+
@password = options[:password]
|
113
|
+
else
|
114
|
+
raise ClientException.new "Failed to initialize BillForward API Client\n"+
|
115
|
+
"Required parameters: :host and :use_logging, and either [:api_token] or all of [:client_id, :client_secret, :username, :password].\n" +
|
116
|
+
"Supplied Parameters: #{options}"
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
@authorization = nil
|
122
|
+
end
|
123
|
+
|
124
|
+
def execute_request(verb, url, token, payload=nil)
|
125
|
+
# Enable Fiddler:
|
126
|
+
if @use_proxy
|
127
|
+
RestClient.proxy = @proxy_url
|
128
|
+
end
|
129
|
+
|
130
|
+
# content_type seems to be broken on generic execute.
|
131
|
+
# darn.
|
132
|
+
# RestClient::Request.execute(options)
|
133
|
+
options = {
|
134
|
+
:Authorization => "Bearer #{token}",
|
135
|
+
:accept => 'application/json'
|
136
|
+
}
|
137
|
+
|
138
|
+
haspayload = @@payload_verbs.include?(verb)
|
139
|
+
|
140
|
+
if (haspayload)
|
141
|
+
options.update(:content_type => 'application/json')
|
142
|
+
end
|
143
|
+
|
144
|
+
args = [url, options]
|
145
|
+
args.insert(1, payload) if haspayload
|
146
|
+
|
147
|
+
RestClient.send(verb.intern, *args)
|
148
|
+
end
|
149
|
+
|
150
|
+
@@all_verbs.each do |action|
|
151
|
+
define_method(action.intern) do |*args|
|
152
|
+
verb = action
|
153
|
+
url = args.shift
|
154
|
+
payload = nil
|
155
|
+
if @@payload_verbs.include?(verb)
|
156
|
+
payload = args.shift
|
157
|
+
TypeCheck.verifyObj(String, payload, 'payload')
|
158
|
+
end
|
159
|
+
|
160
|
+
query_params = args.shift || {}
|
161
|
+
TypeCheck.verifyObj(Hash, query_params, 'query_params')
|
162
|
+
|
163
|
+
self.send(:request, *[verb, url, query_params, payload])
|
164
|
+
end
|
165
|
+
define_method("#{action}_many".intern) do |*args|
|
166
|
+
response = self.send(action.intern, *args)
|
167
|
+
results = response["results"]
|
168
|
+
if results.nil?
|
169
|
+
raise ApiUnexpectedResponseFormatError.new("Response did not contain a results array.")
|
170
|
+
end
|
171
|
+
results
|
172
|
+
end
|
173
|
+
|
174
|
+
define_method("#{action}_first".intern) do |*args|
|
175
|
+
results = self.send("#{action}_many".intern, *args)
|
176
|
+
|
177
|
+
if results.nil? or results.length == 0
|
178
|
+
raise IndexError.new("Cannot get first; request returned empty list of results.")
|
179
|
+
end
|
180
|
+
|
181
|
+
results.first
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
alias_method :retire, :delete
|
186
|
+
alias_method :retire_first, :delete_first
|
187
|
+
alias_method :get_results, :get_many
|
188
|
+
|
189
|
+
private
|
190
|
+
def uri_encode(params = {})
|
191
|
+
TypeCheck.verifyObj(Hash, params, 'params')
|
192
|
+
|
193
|
+
encoded_params = params.reduce([]) do |accumulator, (iterand_key, iterand_value)|
|
194
|
+
encoded_key = ERB::Util.url_encode iterand_key
|
195
|
+
encoded_value = ERB::Util.url_encode iterand_value
|
196
|
+
accumulator + ["#{encoded_key}=#{encoded_value}"]
|
197
|
+
end
|
198
|
+
query = encoded_params.join '&'
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
def request(verb, url, params={}, payload=nil)
|
203
|
+
qualified_url = "#{@host}#{url}"
|
204
|
+
|
205
|
+
split = qualified_url.split('?')
|
206
|
+
distilled_url = split.first
|
207
|
+
override_params = split.length > 1 \
|
208
|
+
? split[1] \
|
209
|
+
: ''
|
210
|
+
|
211
|
+
param_string = override_params.empty? \
|
212
|
+
? ((params && params.any?) \
|
213
|
+
? uri_encode(params) \
|
214
|
+
: '') \
|
215
|
+
: override_params
|
216
|
+
|
217
|
+
# Make params into query parameters
|
218
|
+
full_url = param_string.empty? \
|
219
|
+
? distilled_url \
|
220
|
+
: [distilled_url, param_string].join('?')
|
221
|
+
|
222
|
+
token = get_token
|
223
|
+
|
224
|
+
log "#{verb} #{url}"
|
225
|
+
log "token: #{token}"
|
226
|
+
|
227
|
+
begin
|
228
|
+
response = execute_request(verb, full_url, token, payload)
|
229
|
+
|
230
|
+
parsed = JSON.parse(response.to_str)
|
231
|
+
pretty = JSON.pretty_generate(parsed)
|
232
|
+
log "response: \n#{pretty}"
|
233
|
+
|
234
|
+
return parsed
|
235
|
+
rescue SocketError => e
|
236
|
+
handle_restclient_error(e)
|
237
|
+
rescue NoMethodError => e
|
238
|
+
# Work around RestClient bug
|
239
|
+
if e.message =~ /\WRequestFailed\W/
|
240
|
+
e = APIConnectionError.new('Unexpected HTTP response code')
|
241
|
+
handle_restclient_error(e)
|
242
|
+
else
|
243
|
+
raise
|
244
|
+
end
|
245
|
+
rescue RestClient::ExceptionWithResponse => e
|
246
|
+
if rcode = e.http_code and rbody = e.http_body
|
247
|
+
handle_api_error(rcode, rbody)
|
248
|
+
else
|
249
|
+
handle_restclient_error(e)
|
250
|
+
end
|
251
|
+
rescue RestClient::Exception, Errno::ECONNREFUSED => e
|
252
|
+
handle_restclient_error(e)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
def handle_restclient_error(e)
|
257
|
+
connection_message = "Please check your internet connection and try again. "
|
258
|
+
|
259
|
+
case e
|
260
|
+
when RestClient::RequestTimeout
|
261
|
+
message = "Could not connect to BillForward (#{@host}). #{connection_message}"
|
262
|
+
when RestClient::ServerBrokeConnection
|
263
|
+
message = "The connection to the server (#{@host}) broke before the " \
|
264
|
+
"request completed. #{connection_message}"
|
265
|
+
when SocketError
|
266
|
+
message = "Unexpected error communicating when trying to connect to BillForward. " \
|
267
|
+
"Please confirm that (#{@host}) is a BillForward API URL. "
|
268
|
+
else
|
269
|
+
message = "Unexpected error communicating with BillForward. "
|
270
|
+
end
|
271
|
+
|
272
|
+
raise ClientException.new(message + "\n\n(Network error: #{e.message})")
|
273
|
+
end
|
274
|
+
|
275
|
+
def handle_api_error(rcode, rbody)
|
276
|
+
begin
|
277
|
+
# Example error JSON:
|
278
|
+
# {
|
279
|
+
# "errorType" : "ValidationError",
|
280
|
+
# "errorMessage" : "Validation Error - Entity: Subscription Field: type Value: null Message: may not be null\nValidation Error - Entity: Subscription Field: productID Value: null Message: may not be null\nValidation Error - Entity: Subscription Field: name Value: null Message: may not be null\n",
|
281
|
+
# "errorParameters" : [ "type", "productID", "name" ]
|
282
|
+
# }
|
283
|
+
|
284
|
+
error = JSON.parse(rbody)
|
285
|
+
|
286
|
+
errorType = error['errorType']
|
287
|
+
errorMessage = error['errorMessage']
|
288
|
+
if (error.key? 'errorParameters')
|
289
|
+
errorParameters = error['errorParameters']
|
290
|
+
raise_message = "\n====\n#{rcode} API Error.\nType: #{errorType}\nMessage: #{errorMessage}\nParameters: #{errorParameters}\n====\n"
|
291
|
+
else
|
292
|
+
if (errorType == 'Oauth')
|
293
|
+
split = errorMessage.split(', ')
|
294
|
+
|
295
|
+
error = split.first.split('=').last
|
296
|
+
description = split.last.split('=').last
|
297
|
+
|
298
|
+
raise_message = "\n====\n#{rcode} Authorization failed.\nType: #{type}\nError: #{error}\nDescription: #{description}\n====\n"
|
299
|
+
|
300
|
+
raise ApiAuthorizationError.new(error, rbody), raise_message
|
301
|
+
else
|
302
|
+
raise_message = "\n====\n#{rcode} API Error.\nType: #{errorType}\nMessage: #{errorMessage}\n====\n"
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
raise ApiError.new(error, rbody), raise_message
|
307
|
+
end
|
308
|
+
|
309
|
+
raise_message = "\n====\n#{rcode} API Error.\n Response body: #{rbody}\n====\n"
|
310
|
+
raise ApiError.new(nil, rbody), raise_message
|
311
|
+
end
|
312
|
+
|
313
|
+
def log(*args)
|
314
|
+
if @use_logging
|
315
|
+
puts *args
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
def get_token
|
320
|
+
if @api_token
|
321
|
+
@api_token
|
322
|
+
else
|
323
|
+
if @authorization and Time.now < @authorization["expires_at"]
|
324
|
+
return @authorization["access_token"]
|
325
|
+
end
|
326
|
+
begin
|
327
|
+
response = RestClient.get("#{@host}oauth/token", :params => {
|
328
|
+
:username => @username,
|
329
|
+
:password => @password,
|
330
|
+
:client_id => @client_id,
|
331
|
+
:client_secret => @client_secret,
|
332
|
+
:grant_type => "password"
|
333
|
+
}, :accept => :json)
|
334
|
+
|
335
|
+
@authorization = JSON.parse(response.to_str)
|
336
|
+
@authorization["expires_at"] = Time.now + @authorization["expires_in"]
|
337
|
+
|
338
|
+
@authorization["access_token"]
|
339
|
+
rescue => e
|
340
|
+
if e.respond_to? "response"
|
341
|
+
log "BILL FORWARD CLIENT ERROR", e.response
|
342
|
+
else
|
343
|
+
log "BILL FORWARD CLIENT ERROR", e, e.to_json
|
344
|
+
end
|
345
|
+
nil
|
346
|
+
end
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|
350
|
+
end
|