flowcommerce 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +35 -0
- data/lib/clients/flow_catalog_v0_client.rb +1672 -0
- data/lib/clients/flow_common_v0_client.rb +1449 -0
- data/lib/flow_commerce/client.rb +19 -0
- data/lib/flowcommerce.rb +6 -0
- metadata +76 -0
@@ -0,0 +1,1449 @@
|
|
1
|
+
# Generated by apidoc - http://www.apidoc.me
|
2
|
+
# Service version: 0.0.11
|
3
|
+
# apidoc:0.11.17 http://www.apidoc.me/flow/common/0.0.11/ruby_client
|
4
|
+
|
5
|
+
require 'cgi'
|
6
|
+
require 'net/http'
|
7
|
+
require 'net/https'
|
8
|
+
require 'uri'
|
9
|
+
require 'base64'
|
10
|
+
|
11
|
+
require 'date'
|
12
|
+
require 'rubygems'
|
13
|
+
require 'json'
|
14
|
+
require 'bigdecimal'
|
15
|
+
|
16
|
+
module Io
|
17
|
+
module Flow
|
18
|
+
module Common
|
19
|
+
module V0
|
20
|
+
|
21
|
+
class Client
|
22
|
+
|
23
|
+
module Constants
|
24
|
+
|
25
|
+
NAMESPACE = 'io.flow.common.v0' unless defined?(Constants::NAMESPACE)
|
26
|
+
USER_AGENT = 'apidoc:0.11.17 http://www.apidoc.me/flow/common/0.0.11/ruby_client' unless defined?(Constants::USER_AGENT)
|
27
|
+
VERSION = '0.0.11' unless defined?(Constants::VERSION)
|
28
|
+
VERSION_MAJOR = 0 unless defined?(VERSION_MAJOR)
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :url
|
33
|
+
|
34
|
+
def initialize(url, opts={})
|
35
|
+
@url = HttpClient::Preconditions.assert_class('url', url, String)
|
36
|
+
@authorization = HttpClient::Preconditions.assert_class_or_nil('authorization', opts.delete(:authorization), HttpClient::Authorization)
|
37
|
+
@default_headers = HttpClient::Preconditions.assert_class('default_headers', opts.delete(:default_headers) || {}, Hash)
|
38
|
+
HttpClient::Preconditions.assert_empty_opts(opts)
|
39
|
+
HttpClient::Preconditions.check_state(url.match(/http.+/i), "URL[%s] must start with http" % url)
|
40
|
+
end
|
41
|
+
|
42
|
+
def request(path=nil)
|
43
|
+
HttpClient::Preconditions.assert_class_or_nil('path', path, String)
|
44
|
+
request = HttpClient::Request.new(URI.parse(@url + path.to_s)).with_header('User-Agent', Constants::USER_AGENT).with_header('X-Apidoc-Version', Constants::VERSION).with_header('X-Apidoc-Version-Major', Constants::VERSION_MAJOR)
|
45
|
+
|
46
|
+
@default_headers.each do |key, value|
|
47
|
+
request = request.with_header(key, value)
|
48
|
+
end
|
49
|
+
|
50
|
+
if @authorization
|
51
|
+
request = request.with_auth(@authorization)
|
52
|
+
end
|
53
|
+
|
54
|
+
request
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
module Clients
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
module Models
|
67
|
+
|
68
|
+
class ExpandableOrganization
|
69
|
+
|
70
|
+
module Types
|
71
|
+
ORGANIZATION = 'organization' unless defined?(ORGANIZATION)
|
72
|
+
ORGANIZATION_REFERENCE = 'organization_reference' unless defined?(ORGANIZATION_REFERENCE)
|
73
|
+
end
|
74
|
+
|
75
|
+
def initialize(incoming={})
|
76
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
77
|
+
HttpClient::Preconditions.require_keys(opts, [:name], 'ExpandableOrganization')
|
78
|
+
@name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String)
|
79
|
+
end
|
80
|
+
|
81
|
+
def to_hash
|
82
|
+
subtype_to_hash.merge(:discriminator => @name)
|
83
|
+
end
|
84
|
+
|
85
|
+
def ExpandableOrganization.from_json(hash)
|
86
|
+
HttpClient::Preconditions.assert_class('hash', hash, Hash)
|
87
|
+
case HttpClient::Helper.symbolize_keys(hash)[:discriminator]
|
88
|
+
when Types::ORGANIZATION; Organization.new(hash)
|
89
|
+
when Types::ORGANIZATION_REFERENCE; OrganizationReference.new(hash)
|
90
|
+
else ExpandableOrganizationUndefinedType.new(:name => union_type_name)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
class ExpandableOrganizationUndefinedType < ExpandableOrganization
|
97
|
+
|
98
|
+
attr_reader :name
|
99
|
+
|
100
|
+
def initialize(incoming={})
|
101
|
+
super(:name => 'undefined_type')
|
102
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
103
|
+
@name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String)
|
104
|
+
end
|
105
|
+
|
106
|
+
def subtype_to_hash
|
107
|
+
raise 'Unable to serialize undefined type to json'
|
108
|
+
end
|
109
|
+
|
110
|
+
def copy(incoming={})
|
111
|
+
raise 'Operation not supported for undefined type'
|
112
|
+
end
|
113
|
+
|
114
|
+
def to_hash
|
115
|
+
raise 'Operation not supported for undefined type'
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
class ExpandableUser
|
121
|
+
|
122
|
+
module Types
|
123
|
+
USER = 'user' unless defined?(USER)
|
124
|
+
USER_REFERENCE = 'user_reference' unless defined?(USER_REFERENCE)
|
125
|
+
end
|
126
|
+
|
127
|
+
def initialize(incoming={})
|
128
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
129
|
+
HttpClient::Preconditions.require_keys(opts, [:name], 'ExpandableUser')
|
130
|
+
@name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String)
|
131
|
+
end
|
132
|
+
|
133
|
+
def to_hash
|
134
|
+
subtype_to_hash.merge(:discriminator => @name)
|
135
|
+
end
|
136
|
+
|
137
|
+
def ExpandableUser.from_json(hash)
|
138
|
+
HttpClient::Preconditions.assert_class('hash', hash, Hash)
|
139
|
+
case HttpClient::Helper.symbolize_keys(hash)[:discriminator]
|
140
|
+
when Types::USER; User.new(hash)
|
141
|
+
when Types::USER_REFERENCE; UserReference.new(hash)
|
142
|
+
else ExpandableUserUndefinedType.new(:name => union_type_name)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
class ExpandableUserUndefinedType < ExpandableUser
|
149
|
+
|
150
|
+
attr_reader :name
|
151
|
+
|
152
|
+
def initialize(incoming={})
|
153
|
+
super(:name => 'undefined_type')
|
154
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
155
|
+
@name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String)
|
156
|
+
end
|
157
|
+
|
158
|
+
def subtype_to_hash
|
159
|
+
raise 'Unable to serialize undefined type to json'
|
160
|
+
end
|
161
|
+
|
162
|
+
def copy(incoming={})
|
163
|
+
raise 'Operation not supported for undefined type'
|
164
|
+
end
|
165
|
+
|
166
|
+
def to_hash
|
167
|
+
raise 'Operation not supported for undefined type'
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
class Calendar
|
173
|
+
|
174
|
+
attr_reader :value
|
175
|
+
|
176
|
+
def initialize(value)
|
177
|
+
@value = HttpClient::Preconditions.assert_class('value', value, String)
|
178
|
+
end
|
179
|
+
|
180
|
+
# Returns the instance of Calendar for this value, creating a new instance for an unknown value
|
181
|
+
def Calendar.apply(value)
|
182
|
+
if value.instance_of?(Calendar)
|
183
|
+
value
|
184
|
+
else
|
185
|
+
HttpClient::Preconditions.assert_class_or_nil('value', value, String)
|
186
|
+
value.nil? ? nil : (from_string(value) || Calendar.new(value))
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
# Returns the instance of Calendar for this value, or nil if not found
|
191
|
+
def Calendar.from_string(value)
|
192
|
+
HttpClient::Preconditions.assert_class('value', value, String)
|
193
|
+
Calendar.ALL.find { |v| v.value == value }
|
194
|
+
end
|
195
|
+
|
196
|
+
def Calendar.ALL
|
197
|
+
@@all ||= [Calendar.weekdays, Calendar.everyday, Calendar.unknown]
|
198
|
+
end
|
199
|
+
|
200
|
+
# Mon - Fri
|
201
|
+
def Calendar.weekdays
|
202
|
+
@@_weekdays ||= Calendar.new('Weekdays')
|
203
|
+
end
|
204
|
+
|
205
|
+
# 7 days per week
|
206
|
+
def Calendar.everyday
|
207
|
+
@@_everyday ||= Calendar.new('Everyday')
|
208
|
+
end
|
209
|
+
|
210
|
+
# We do not yet know the calendar
|
211
|
+
def Calendar.unknown
|
212
|
+
@@_unknown ||= Calendar.new('Unknown')
|
213
|
+
end
|
214
|
+
|
215
|
+
def to_hash
|
216
|
+
value
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
class Capability
|
222
|
+
|
223
|
+
attr_reader :value
|
224
|
+
|
225
|
+
def initialize(value)
|
226
|
+
@value = HttpClient::Preconditions.assert_class('value', value, String)
|
227
|
+
end
|
228
|
+
|
229
|
+
# Returns the instance of Capability for this value, creating a new instance for an unknown value
|
230
|
+
def Capability.apply(value)
|
231
|
+
if value.instance_of?(Capability)
|
232
|
+
value
|
233
|
+
else
|
234
|
+
HttpClient::Preconditions.assert_class_or_nil('value', value, String)
|
235
|
+
value.nil? ? nil : (from_string(value) || Capability.new(value))
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
# Returns the instance of Capability for this value, or nil if not found
|
240
|
+
def Capability.from_string(value)
|
241
|
+
HttpClient::Preconditions.assert_class('value', value, String)
|
242
|
+
Capability.ALL.find { |v| v.value == value }
|
243
|
+
end
|
244
|
+
|
245
|
+
def Capability.ALL
|
246
|
+
@@all ||= [Capability.crossdock]
|
247
|
+
end
|
248
|
+
|
249
|
+
def Capability.crossdock
|
250
|
+
@@_crossdock ||= Capability.new('crossdock')
|
251
|
+
end
|
252
|
+
|
253
|
+
def to_hash
|
254
|
+
value
|
255
|
+
end
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
class ChangeType
|
260
|
+
|
261
|
+
attr_reader :value
|
262
|
+
|
263
|
+
def initialize(value)
|
264
|
+
@value = HttpClient::Preconditions.assert_class('value', value, String)
|
265
|
+
end
|
266
|
+
|
267
|
+
# Returns the instance of ChangeType for this value, creating a new instance for an unknown value
|
268
|
+
def ChangeType.apply(value)
|
269
|
+
if value.instance_of?(ChangeType)
|
270
|
+
value
|
271
|
+
else
|
272
|
+
HttpClient::Preconditions.assert_class_or_nil('value', value, String)
|
273
|
+
value.nil? ? nil : (from_string(value) || ChangeType.new(value))
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
# Returns the instance of ChangeType for this value, or nil if not found
|
278
|
+
def ChangeType.from_string(value)
|
279
|
+
HttpClient::Preconditions.assert_class('value', value, String)
|
280
|
+
ChangeType.ALL.find { |v| v.value == value }
|
281
|
+
end
|
282
|
+
|
283
|
+
def ChangeType.ALL
|
284
|
+
@@all ||= [ChangeType.insert, ChangeType.update, ChangeType.delete]
|
285
|
+
end
|
286
|
+
|
287
|
+
def ChangeType.insert
|
288
|
+
@@_insert ||= ChangeType.new('insert')
|
289
|
+
end
|
290
|
+
|
291
|
+
def ChangeType.update
|
292
|
+
@@_update ||= ChangeType.new('update')
|
293
|
+
end
|
294
|
+
|
295
|
+
def ChangeType.delete
|
296
|
+
@@_delete ||= ChangeType.new('delete')
|
297
|
+
end
|
298
|
+
|
299
|
+
def to_hash
|
300
|
+
value
|
301
|
+
end
|
302
|
+
|
303
|
+
end
|
304
|
+
|
305
|
+
class ScheduleExceptionStatus
|
306
|
+
|
307
|
+
attr_reader :value
|
308
|
+
|
309
|
+
def initialize(value)
|
310
|
+
@value = HttpClient::Preconditions.assert_class('value', value, String)
|
311
|
+
end
|
312
|
+
|
313
|
+
# Returns the instance of ScheduleExceptionStatus for this value, creating a new instance for an unknown value
|
314
|
+
def ScheduleExceptionStatus.apply(value)
|
315
|
+
if value.instance_of?(ScheduleExceptionStatus)
|
316
|
+
value
|
317
|
+
else
|
318
|
+
HttpClient::Preconditions.assert_class_or_nil('value', value, String)
|
319
|
+
value.nil? ? nil : (from_string(value) || ScheduleExceptionStatus.new(value))
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
# Returns the instance of ScheduleExceptionStatus for this value, or nil if not found
|
324
|
+
def ScheduleExceptionStatus.from_string(value)
|
325
|
+
HttpClient::Preconditions.assert_class('value', value, String)
|
326
|
+
ScheduleExceptionStatus.ALL.find { |v| v.value == value }
|
327
|
+
end
|
328
|
+
|
329
|
+
def ScheduleExceptionStatus.ALL
|
330
|
+
@@all ||= [ScheduleExceptionStatus.open, ScheduleExceptionStatus.closed]
|
331
|
+
end
|
332
|
+
|
333
|
+
def ScheduleExceptionStatus.open
|
334
|
+
@@_open ||= ScheduleExceptionStatus.new('Open')
|
335
|
+
end
|
336
|
+
|
337
|
+
def ScheduleExceptionStatus.closed
|
338
|
+
@@_closed ||= ScheduleExceptionStatus.new('Closed')
|
339
|
+
end
|
340
|
+
|
341
|
+
def to_hash
|
342
|
+
value
|
343
|
+
end
|
344
|
+
|
345
|
+
end
|
346
|
+
|
347
|
+
class UnitOfMeasurement
|
348
|
+
|
349
|
+
attr_reader :value
|
350
|
+
|
351
|
+
def initialize(value)
|
352
|
+
@value = HttpClient::Preconditions.assert_class('value', value, String)
|
353
|
+
end
|
354
|
+
|
355
|
+
# Returns the instance of UnitOfMeasurement for this value, creating a new instance for an unknown value
|
356
|
+
def UnitOfMeasurement.apply(value)
|
357
|
+
if value.instance_of?(UnitOfMeasurement)
|
358
|
+
value
|
359
|
+
else
|
360
|
+
HttpClient::Preconditions.assert_class_or_nil('value', value, String)
|
361
|
+
value.nil? ? nil : (from_string(value) || UnitOfMeasurement.new(value))
|
362
|
+
end
|
363
|
+
end
|
364
|
+
|
365
|
+
# Returns the instance of UnitOfMeasurement for this value, or nil if not found
|
366
|
+
def UnitOfMeasurement.from_string(value)
|
367
|
+
HttpClient::Preconditions.assert_class('value', value, String)
|
368
|
+
UnitOfMeasurement.ALL.find { |v| v.value == value }
|
369
|
+
end
|
370
|
+
|
371
|
+
def UnitOfMeasurement.ALL
|
372
|
+
@@all ||= [UnitOfMeasurement.millimeter, UnitOfMeasurement.centimeter, UnitOfMeasurement.inch, UnitOfMeasurement.foot, UnitOfMeasurement.cubic_inch, UnitOfMeasurement.cubic_meter, UnitOfMeasurement.gram, UnitOfMeasurement.kilogram, UnitOfMeasurement.meter, UnitOfMeasurement.ounce, UnitOfMeasurement.pound]
|
373
|
+
end
|
374
|
+
|
375
|
+
# Equivalent to MILLI(METRE).
|
376
|
+
def UnitOfMeasurement.millimeter
|
377
|
+
@@_millimeter ||= UnitOfMeasurement.new('millimeter')
|
378
|
+
end
|
379
|
+
|
380
|
+
# Equivalent to CENTI(METRE).
|
381
|
+
def UnitOfMeasurement.centimeter
|
382
|
+
@@_centimeter ||= UnitOfMeasurement.new('centimeter')
|
383
|
+
end
|
384
|
+
|
385
|
+
# A unit of length equal to 0.01004 m (standard name in).
|
386
|
+
def UnitOfMeasurement.inch
|
387
|
+
@@_inch ||= UnitOfMeasurement.new('inch')
|
388
|
+
end
|
389
|
+
|
390
|
+
# A unit of length equal to 0.3048 m (standard name ft).
|
391
|
+
def UnitOfMeasurement.foot
|
392
|
+
@@_foot ||= UnitOfMeasurement.new('foot')
|
393
|
+
end
|
394
|
+
|
395
|
+
# A unit of volume equal to one cubic inch (in³).
|
396
|
+
def UnitOfMeasurement.cubic_inch
|
397
|
+
@@_cubic_inch ||= UnitOfMeasurement.new('cubic_inch')
|
398
|
+
end
|
399
|
+
|
400
|
+
# The metric unit for volume quantities (m³).
|
401
|
+
def UnitOfMeasurement.cubic_meter
|
402
|
+
@@_cubic_meter ||= UnitOfMeasurement.new('cubic_meter')
|
403
|
+
end
|
404
|
+
|
405
|
+
# A unit of mass equal to 1 / 1000 kilogram (standard name g).
|
406
|
+
def UnitOfMeasurement.gram
|
407
|
+
@@_gram ||= UnitOfMeasurement.new('gram')
|
408
|
+
end
|
409
|
+
|
410
|
+
# The base unit for mass quantities (kg).
|
411
|
+
def UnitOfMeasurement.kilogram
|
412
|
+
@@_kilogram ||= UnitOfMeasurement.new('kilogram')
|
413
|
+
end
|
414
|
+
|
415
|
+
# The base unit for length quantities (m).
|
416
|
+
def UnitOfMeasurement.meter
|
417
|
+
@@_meter ||= UnitOfMeasurement.new('meter')
|
418
|
+
end
|
419
|
+
|
420
|
+
# A unit of mass equal to 1 / 16 POUND (standard name oz).
|
421
|
+
def UnitOfMeasurement.ounce
|
422
|
+
@@_ounce ||= UnitOfMeasurement.new('ounce')
|
423
|
+
end
|
424
|
+
|
425
|
+
# A unit of mass equal to 453.59237 grams (avoirdupois pound, standard name lb).
|
426
|
+
def UnitOfMeasurement.pound
|
427
|
+
@@_pound ||= UnitOfMeasurement.new('pound')
|
428
|
+
end
|
429
|
+
|
430
|
+
def to_hash
|
431
|
+
value
|
432
|
+
end
|
433
|
+
|
434
|
+
end
|
435
|
+
|
436
|
+
class UnitOfTime
|
437
|
+
|
438
|
+
attr_reader :value
|
439
|
+
|
440
|
+
def initialize(value)
|
441
|
+
@value = HttpClient::Preconditions.assert_class('value', value, String)
|
442
|
+
end
|
443
|
+
|
444
|
+
# Returns the instance of UnitOfTime for this value, creating a new instance for an unknown value
|
445
|
+
def UnitOfTime.apply(value)
|
446
|
+
if value.instance_of?(UnitOfTime)
|
447
|
+
value
|
448
|
+
else
|
449
|
+
HttpClient::Preconditions.assert_class_or_nil('value', value, String)
|
450
|
+
value.nil? ? nil : (from_string(value) || UnitOfTime.new(value))
|
451
|
+
end
|
452
|
+
end
|
453
|
+
|
454
|
+
# Returns the instance of UnitOfTime for this value, or nil if not found
|
455
|
+
def UnitOfTime.from_string(value)
|
456
|
+
HttpClient::Preconditions.assert_class('value', value, String)
|
457
|
+
UnitOfTime.ALL.find { |v| v.value == value }
|
458
|
+
end
|
459
|
+
|
460
|
+
def UnitOfTime.ALL
|
461
|
+
@@all ||= [UnitOfTime.day, UnitOfTime.hour, UnitOfTime.minute]
|
462
|
+
end
|
463
|
+
|
464
|
+
def UnitOfTime.day
|
465
|
+
@@_day ||= UnitOfTime.new('day')
|
466
|
+
end
|
467
|
+
|
468
|
+
def UnitOfTime.hour
|
469
|
+
@@_hour ||= UnitOfTime.new('hour')
|
470
|
+
end
|
471
|
+
|
472
|
+
def UnitOfTime.minute
|
473
|
+
@@_minute ||= UnitOfTime.new('minute')
|
474
|
+
end
|
475
|
+
|
476
|
+
def to_hash
|
477
|
+
value
|
478
|
+
end
|
479
|
+
|
480
|
+
end
|
481
|
+
|
482
|
+
class ValueAddedService
|
483
|
+
|
484
|
+
attr_reader :value
|
485
|
+
|
486
|
+
def initialize(value)
|
487
|
+
@value = HttpClient::Preconditions.assert_class('value', value, String)
|
488
|
+
end
|
489
|
+
|
490
|
+
# Returns the instance of ValueAddedService for this value, creating a new instance for an unknown value
|
491
|
+
def ValueAddedService.apply(value)
|
492
|
+
if value.instance_of?(ValueAddedService)
|
493
|
+
value
|
494
|
+
else
|
495
|
+
HttpClient::Preconditions.assert_class_or_nil('value', value, String)
|
496
|
+
value.nil? ? nil : (from_string(value) || ValueAddedService.new(value))
|
497
|
+
end
|
498
|
+
end
|
499
|
+
|
500
|
+
# Returns the instance of ValueAddedService for this value, or nil if not found
|
501
|
+
def ValueAddedService.from_string(value)
|
502
|
+
HttpClient::Preconditions.assert_class('value', value, String)
|
503
|
+
ValueAddedService.ALL.find { |v| v.value == value }
|
504
|
+
end
|
505
|
+
|
506
|
+
def ValueAddedService.ALL
|
507
|
+
@@all ||= [ValueAddedService.hazardous_material]
|
508
|
+
end
|
509
|
+
|
510
|
+
# See https://en.wikipedia.org/wiki/ORM-D
|
511
|
+
def ValueAddedService.hazardous_material
|
512
|
+
@@_hazardous_material ||= ValueAddedService.new('Hazardous Material')
|
513
|
+
end
|
514
|
+
|
515
|
+
def to_hash
|
516
|
+
value
|
517
|
+
end
|
518
|
+
|
519
|
+
end
|
520
|
+
|
521
|
+
class Visibility
|
522
|
+
|
523
|
+
attr_reader :value
|
524
|
+
|
525
|
+
def initialize(value)
|
526
|
+
@value = HttpClient::Preconditions.assert_class('value', value, String)
|
527
|
+
end
|
528
|
+
|
529
|
+
# Returns the instance of Visibility for this value, creating a new instance for an unknown value
|
530
|
+
def Visibility.apply(value)
|
531
|
+
if value.instance_of?(Visibility)
|
532
|
+
value
|
533
|
+
else
|
534
|
+
HttpClient::Preconditions.assert_class_or_nil('value', value, String)
|
535
|
+
value.nil? ? nil : (from_string(value) || Visibility.new(value))
|
536
|
+
end
|
537
|
+
end
|
538
|
+
|
539
|
+
# Returns the instance of Visibility for this value, or nil if not found
|
540
|
+
def Visibility.from_string(value)
|
541
|
+
HttpClient::Preconditions.assert_class('value', value, String)
|
542
|
+
Visibility.ALL.find { |v| v.value == value }
|
543
|
+
end
|
544
|
+
|
545
|
+
def Visibility.ALL
|
546
|
+
@@all ||= [Visibility.public, Visibility.private]
|
547
|
+
end
|
548
|
+
|
549
|
+
def Visibility.public
|
550
|
+
@@_public ||= Visibility.new('public')
|
551
|
+
end
|
552
|
+
|
553
|
+
def Visibility.private
|
554
|
+
@@_private ||= Visibility.new('private')
|
555
|
+
end
|
556
|
+
|
557
|
+
def to_hash
|
558
|
+
value
|
559
|
+
end
|
560
|
+
|
561
|
+
end
|
562
|
+
|
563
|
+
# Defines structured fields for address to be used in user/form input. Either
|
564
|
+
# text or the structured input needs to be present.
|
565
|
+
class Address
|
566
|
+
|
567
|
+
attr_reader :text, :streets, :city, :province, :postal_code, :country
|
568
|
+
|
569
|
+
def initialize(incoming={})
|
570
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
571
|
+
@text = (x = opts.delete(:text); x.nil? ? nil : HttpClient::Preconditions.assert_class('text', x, String))
|
572
|
+
@streets = (x = opts.delete(:streets); x.nil? ? nil : HttpClient::Preconditions.assert_class('streets', x, Array).map { |v| HttpClient::Preconditions.assert_class('streets', v, String) })
|
573
|
+
@city = (x = opts.delete(:city); x.nil? ? nil : HttpClient::Preconditions.assert_class('city', x, String))
|
574
|
+
@province = (x = opts.delete(:province); x.nil? ? nil : HttpClient::Preconditions.assert_class('province', x, String))
|
575
|
+
@postal_code = (x = opts.delete(:postal_code); x.nil? ? nil : HttpClient::Preconditions.assert_class('postal_code', x, String))
|
576
|
+
@country = (x = opts.delete(:country); x.nil? ? nil : HttpClient::Preconditions.assert_class('country', x, String))
|
577
|
+
end
|
578
|
+
|
579
|
+
def to_json
|
580
|
+
JSON.dump(to_hash)
|
581
|
+
end
|
582
|
+
|
583
|
+
def copy(incoming={})
|
584
|
+
Address.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
585
|
+
end
|
586
|
+
|
587
|
+
def to_hash
|
588
|
+
{
|
589
|
+
:text => text,
|
590
|
+
:streets => streets.nil? ? nil : streets,
|
591
|
+
:city => city,
|
592
|
+
:province => province,
|
593
|
+
:postal_code => postal_code,
|
594
|
+
:country => country
|
595
|
+
}
|
596
|
+
end
|
597
|
+
|
598
|
+
end
|
599
|
+
|
600
|
+
class ChangeHeader
|
601
|
+
|
602
|
+
attr_reader :id, :timestamp, :type
|
603
|
+
|
604
|
+
def initialize(incoming={})
|
605
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
606
|
+
HttpClient::Preconditions.require_keys(opts, [:id, :timestamp, :type], 'ChangeHeader')
|
607
|
+
@id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
|
608
|
+
@timestamp = HttpClient::Preconditions.assert_class('timestamp', HttpClient::Helper.to_date_time_iso8601(opts.delete(:timestamp)), DateTime)
|
609
|
+
@type = (x = opts.delete(:type); x.is_a?(::Io::Flow::Common::V0::Models::ChangeType) ? x : ::Io::Flow::Common::V0::Models::ChangeType.apply(x))
|
610
|
+
end
|
611
|
+
|
612
|
+
def to_json
|
613
|
+
JSON.dump(to_hash)
|
614
|
+
end
|
615
|
+
|
616
|
+
def copy(incoming={})
|
617
|
+
ChangeHeader.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
618
|
+
end
|
619
|
+
|
620
|
+
def to_hash
|
621
|
+
{
|
622
|
+
:id => id,
|
623
|
+
:timestamp => timestamp,
|
624
|
+
:type => type.value
|
625
|
+
}
|
626
|
+
end
|
627
|
+
|
628
|
+
end
|
629
|
+
|
630
|
+
# Defines structured fields for a contact person. Typically used for specifying
|
631
|
+
# contact person for an account, shipment, or organization representative
|
632
|
+
class Contact
|
633
|
+
|
634
|
+
attr_reader :name, :email, :phone
|
635
|
+
|
636
|
+
def initialize(incoming={})
|
637
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
638
|
+
HttpClient::Preconditions.require_keys(opts, [:name], 'Contact')
|
639
|
+
@name = (x = opts.delete(:name); x.is_a?(::Io::Flow::Common::V0::Models::Name) ? x : ::Io::Flow::Common::V0::Models::Name.new(x))
|
640
|
+
@email = (x = opts.delete(:email); x.nil? ? nil : HttpClient::Preconditions.assert_class('email', x, String))
|
641
|
+
@phone = (x = opts.delete(:phone); x.nil? ? nil : HttpClient::Preconditions.assert_class('phone', x, String))
|
642
|
+
end
|
643
|
+
|
644
|
+
def to_json
|
645
|
+
JSON.dump(to_hash)
|
646
|
+
end
|
647
|
+
|
648
|
+
def copy(incoming={})
|
649
|
+
Contact.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
650
|
+
end
|
651
|
+
|
652
|
+
def to_hash
|
653
|
+
{
|
654
|
+
:name => name.to_hash,
|
655
|
+
:email => email,
|
656
|
+
:phone => phone
|
657
|
+
}
|
658
|
+
end
|
659
|
+
|
660
|
+
end
|
661
|
+
|
662
|
+
class DatetimeRange
|
663
|
+
|
664
|
+
attr_reader :from, :to
|
665
|
+
|
666
|
+
def initialize(incoming={})
|
667
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
668
|
+
HttpClient::Preconditions.require_keys(opts, [:from, :to], 'DatetimeRange')
|
669
|
+
@from = HttpClient::Preconditions.assert_class('from', HttpClient::Helper.to_date_time_iso8601(opts.delete(:from)), DateTime)
|
670
|
+
@to = HttpClient::Preconditions.assert_class('to', HttpClient::Helper.to_date_time_iso8601(opts.delete(:to)), DateTime)
|
671
|
+
end
|
672
|
+
|
673
|
+
def to_json
|
674
|
+
JSON.dump(to_hash)
|
675
|
+
end
|
676
|
+
|
677
|
+
def copy(incoming={})
|
678
|
+
DatetimeRange.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
679
|
+
end
|
680
|
+
|
681
|
+
def to_hash
|
682
|
+
{
|
683
|
+
:from => from,
|
684
|
+
:to => to
|
685
|
+
}
|
686
|
+
end
|
687
|
+
|
688
|
+
end
|
689
|
+
|
690
|
+
class Dimension
|
691
|
+
|
692
|
+
attr_reader :value, :units
|
693
|
+
|
694
|
+
def initialize(incoming={})
|
695
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
696
|
+
HttpClient::Preconditions.require_keys(opts, [:value, :units], 'Dimension')
|
697
|
+
@value = HttpClient::Preconditions.assert_class('value', opts.delete(:value), Float)
|
698
|
+
@units = (x = opts.delete(:units); x.is_a?(::Io::Flow::Common::V0::Models::UnitOfMeasurement) ? x : ::Io::Flow::Common::V0::Models::UnitOfMeasurement.apply(x))
|
699
|
+
end
|
700
|
+
|
701
|
+
def to_json
|
702
|
+
JSON.dump(to_hash)
|
703
|
+
end
|
704
|
+
|
705
|
+
def copy(incoming={})
|
706
|
+
Dimension.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
707
|
+
end
|
708
|
+
|
709
|
+
def to_hash
|
710
|
+
{
|
711
|
+
:value => value,
|
712
|
+
:units => units.value
|
713
|
+
}
|
714
|
+
end
|
715
|
+
|
716
|
+
end
|
717
|
+
|
718
|
+
class Error
|
719
|
+
|
720
|
+
attr_reader :code, :message
|
721
|
+
|
722
|
+
def initialize(incoming={})
|
723
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
724
|
+
HttpClient::Preconditions.require_keys(opts, [:code, :message], 'Error')
|
725
|
+
@code = HttpClient::Preconditions.assert_class('code', opts.delete(:code), String)
|
726
|
+
@message = HttpClient::Preconditions.assert_class('message', opts.delete(:message), String)
|
727
|
+
end
|
728
|
+
|
729
|
+
def to_json
|
730
|
+
JSON.dump(to_hash)
|
731
|
+
end
|
732
|
+
|
733
|
+
def copy(incoming={})
|
734
|
+
Error.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
735
|
+
end
|
736
|
+
|
737
|
+
def to_hash
|
738
|
+
{
|
739
|
+
:code => code,
|
740
|
+
:message => message
|
741
|
+
}
|
742
|
+
end
|
743
|
+
|
744
|
+
end
|
745
|
+
|
746
|
+
class Healthcheck
|
747
|
+
|
748
|
+
attr_reader :status
|
749
|
+
|
750
|
+
def initialize(incoming={})
|
751
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
752
|
+
HttpClient::Preconditions.require_keys(opts, [:status], 'Healthcheck')
|
753
|
+
@status = HttpClient::Preconditions.assert_class('status', opts.delete(:status), String)
|
754
|
+
end
|
755
|
+
|
756
|
+
def to_json
|
757
|
+
JSON.dump(to_hash)
|
758
|
+
end
|
759
|
+
|
760
|
+
def copy(incoming={})
|
761
|
+
Healthcheck.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
762
|
+
end
|
763
|
+
|
764
|
+
def to_hash
|
765
|
+
{
|
766
|
+
:status => status
|
767
|
+
}
|
768
|
+
end
|
769
|
+
|
770
|
+
end
|
771
|
+
|
772
|
+
# We capture the location as a string; over time we anticipate storing structued
|
773
|
+
# data by parsing the location (e.g. the country) to enable things like
|
774
|
+
# reporting, filtering in bulk
|
775
|
+
class Location
|
776
|
+
|
777
|
+
attr_reader :value
|
778
|
+
|
779
|
+
def initialize(incoming={})
|
780
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
781
|
+
HttpClient::Preconditions.require_keys(opts, [:value], 'Location')
|
782
|
+
@value = HttpClient::Preconditions.assert_class('value', opts.delete(:value), String)
|
783
|
+
end
|
784
|
+
|
785
|
+
def to_json
|
786
|
+
JSON.dump(to_hash)
|
787
|
+
end
|
788
|
+
|
789
|
+
def copy(incoming={})
|
790
|
+
Location.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
791
|
+
end
|
792
|
+
|
793
|
+
def to_hash
|
794
|
+
{
|
795
|
+
:value => value
|
796
|
+
}
|
797
|
+
end
|
798
|
+
|
799
|
+
end
|
800
|
+
|
801
|
+
class Name
|
802
|
+
|
803
|
+
attr_reader :first, :last
|
804
|
+
|
805
|
+
def initialize(incoming={})
|
806
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
807
|
+
@first = (x = opts.delete(:first); x.nil? ? nil : HttpClient::Preconditions.assert_class('first', x, String))
|
808
|
+
@last = (x = opts.delete(:last); x.nil? ? nil : HttpClient::Preconditions.assert_class('last', x, String))
|
809
|
+
end
|
810
|
+
|
811
|
+
def to_json
|
812
|
+
JSON.dump(to_hash)
|
813
|
+
end
|
814
|
+
|
815
|
+
def copy(incoming={})
|
816
|
+
Name.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
817
|
+
end
|
818
|
+
|
819
|
+
def to_hash
|
820
|
+
{
|
821
|
+
:first => first,
|
822
|
+
:last => last
|
823
|
+
}
|
824
|
+
end
|
825
|
+
|
826
|
+
end
|
827
|
+
|
828
|
+
# Represents a single organization in the system
|
829
|
+
class Organization < ExpandableOrganization
|
830
|
+
|
831
|
+
attr_reader :id, :name
|
832
|
+
|
833
|
+
def initialize(incoming={})
|
834
|
+
super(:name => ExpandableOrganization::Types::ORGANIZATION)
|
835
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
836
|
+
HttpClient::Preconditions.require_keys(opts, [:id, :name], 'Organization')
|
837
|
+
@id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
|
838
|
+
@name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String)
|
839
|
+
end
|
840
|
+
|
841
|
+
def to_json
|
842
|
+
JSON.dump(to_hash)
|
843
|
+
end
|
844
|
+
|
845
|
+
def copy(incoming={})
|
846
|
+
Organization.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
847
|
+
end
|
848
|
+
|
849
|
+
def subtype_to_hash
|
850
|
+
{
|
851
|
+
:id => id,
|
852
|
+
:name => name
|
853
|
+
}
|
854
|
+
end
|
855
|
+
|
856
|
+
end
|
857
|
+
|
858
|
+
class OrganizationReference < ExpandableOrganization
|
859
|
+
|
860
|
+
attr_reader :id
|
861
|
+
|
862
|
+
def initialize(incoming={})
|
863
|
+
super(:name => ExpandableOrganization::Types::ORGANIZATION_REFERENCE)
|
864
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
865
|
+
HttpClient::Preconditions.require_keys(opts, [:id], 'OrganizationReference')
|
866
|
+
@id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
|
867
|
+
end
|
868
|
+
|
869
|
+
def to_json
|
870
|
+
JSON.dump(to_hash)
|
871
|
+
end
|
872
|
+
|
873
|
+
def copy(incoming={})
|
874
|
+
OrganizationReference.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
875
|
+
end
|
876
|
+
|
877
|
+
def subtype_to_hash
|
878
|
+
{
|
879
|
+
:id => id
|
880
|
+
}
|
881
|
+
end
|
882
|
+
|
883
|
+
end
|
884
|
+
|
885
|
+
class OrganizationSummary
|
886
|
+
|
887
|
+
attr_reader :id, :name
|
888
|
+
|
889
|
+
def initialize(incoming={})
|
890
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
891
|
+
HttpClient::Preconditions.require_keys(opts, [:id, :name], 'OrganizationSummary')
|
892
|
+
@id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
|
893
|
+
@name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String)
|
894
|
+
end
|
895
|
+
|
896
|
+
def to_json
|
897
|
+
JSON.dump(to_hash)
|
898
|
+
end
|
899
|
+
|
900
|
+
def copy(incoming={})
|
901
|
+
OrganizationSummary.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
902
|
+
end
|
903
|
+
|
904
|
+
def to_hash
|
905
|
+
{
|
906
|
+
:id => id,
|
907
|
+
:name => name
|
908
|
+
}
|
909
|
+
end
|
910
|
+
|
911
|
+
end
|
912
|
+
|
913
|
+
class Price
|
914
|
+
|
915
|
+
attr_reader :amount, :currency
|
916
|
+
|
917
|
+
def initialize(incoming={})
|
918
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
919
|
+
HttpClient::Preconditions.require_keys(opts, [:amount, :currency], 'Price')
|
920
|
+
@amount = HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), String)
|
921
|
+
@currency = HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String)
|
922
|
+
end
|
923
|
+
|
924
|
+
def to_json
|
925
|
+
JSON.dump(to_hash)
|
926
|
+
end
|
927
|
+
|
928
|
+
def copy(incoming={})
|
929
|
+
Price.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
930
|
+
end
|
931
|
+
|
932
|
+
def to_hash
|
933
|
+
{
|
934
|
+
:amount => amount,
|
935
|
+
:currency => currency
|
936
|
+
}
|
937
|
+
end
|
938
|
+
|
939
|
+
end
|
940
|
+
|
941
|
+
# Represents a single user in the system
|
942
|
+
class User < ExpandableUser
|
943
|
+
|
944
|
+
attr_reader :id, :email, :name
|
945
|
+
|
946
|
+
def initialize(incoming={})
|
947
|
+
super(:name => ExpandableUser::Types::USER)
|
948
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
949
|
+
HttpClient::Preconditions.require_keys(opts, [:id, :name], 'User')
|
950
|
+
@id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
|
951
|
+
@email = (x = opts.delete(:email); x.nil? ? nil : HttpClient::Preconditions.assert_class('email', x, String))
|
952
|
+
@name = (x = opts.delete(:name); x.is_a?(::Io::Flow::Common::V0::Models::Name) ? x : ::Io::Flow::Common::V0::Models::Name.new(x))
|
953
|
+
end
|
954
|
+
|
955
|
+
def to_json
|
956
|
+
JSON.dump(to_hash)
|
957
|
+
end
|
958
|
+
|
959
|
+
def copy(incoming={})
|
960
|
+
User.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
961
|
+
end
|
962
|
+
|
963
|
+
def subtype_to_hash
|
964
|
+
{
|
965
|
+
:id => id,
|
966
|
+
:email => email,
|
967
|
+
:name => name.to_hash
|
968
|
+
}
|
969
|
+
end
|
970
|
+
|
971
|
+
end
|
972
|
+
|
973
|
+
class UserReference < ExpandableUser
|
974
|
+
|
975
|
+
attr_reader :id
|
976
|
+
|
977
|
+
def initialize(incoming={})
|
978
|
+
super(:name => ExpandableUser::Types::USER_REFERENCE)
|
979
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
980
|
+
HttpClient::Preconditions.require_keys(opts, [:id], 'UserReference')
|
981
|
+
@id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
|
982
|
+
end
|
983
|
+
|
984
|
+
def to_json
|
985
|
+
JSON.dump(to_hash)
|
986
|
+
end
|
987
|
+
|
988
|
+
def copy(incoming={})
|
989
|
+
UserReference.new(subtype_to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
990
|
+
end
|
991
|
+
|
992
|
+
def subtype_to_hash
|
993
|
+
{
|
994
|
+
:id => id
|
995
|
+
}
|
996
|
+
end
|
997
|
+
|
998
|
+
end
|
999
|
+
|
1000
|
+
class UserSummary
|
1001
|
+
|
1002
|
+
attr_reader :id, :email, :name
|
1003
|
+
|
1004
|
+
def initialize(incoming={})
|
1005
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
1006
|
+
HttpClient::Preconditions.require_keys(opts, [:id, :name], 'UserSummary')
|
1007
|
+
@id = HttpClient::Preconditions.assert_class('id', opts.delete(:id), String)
|
1008
|
+
@email = (x = opts.delete(:email); x.nil? ? nil : HttpClient::Preconditions.assert_class('email', x, String))
|
1009
|
+
@name = HttpClient::Preconditions.assert_class('name', opts.delete(:name), String)
|
1010
|
+
end
|
1011
|
+
|
1012
|
+
def to_json
|
1013
|
+
JSON.dump(to_hash)
|
1014
|
+
end
|
1015
|
+
|
1016
|
+
def copy(incoming={})
|
1017
|
+
UserSummary.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming)))
|
1018
|
+
end
|
1019
|
+
|
1020
|
+
def to_hash
|
1021
|
+
{
|
1022
|
+
:id => id,
|
1023
|
+
:email => email,
|
1024
|
+
:name => name
|
1025
|
+
}
|
1026
|
+
end
|
1027
|
+
|
1028
|
+
end
|
1029
|
+
|
1030
|
+
end
|
1031
|
+
|
1032
|
+
# ===== END OF SERVICE DEFINITION =====
|
1033
|
+
module HttpClient
|
1034
|
+
|
1035
|
+
class Request
|
1036
|
+
|
1037
|
+
def initialize(uri)
|
1038
|
+
@uri = Preconditions.assert_class('uri', uri, URI)
|
1039
|
+
@params = nil
|
1040
|
+
@body = nil
|
1041
|
+
@auth = nil
|
1042
|
+
@headers = {}
|
1043
|
+
@header_keys_lower_case = []
|
1044
|
+
end
|
1045
|
+
|
1046
|
+
def with_header(name, value)
|
1047
|
+
Preconditions.check_not_blank('name', name, "Header name is required")
|
1048
|
+
Preconditions.check_not_blank('value', value, "Header value is required")
|
1049
|
+
Preconditions.check_state(!@headers.has_key?(name),
|
1050
|
+
"Duplicate header named[%s]" % name)
|
1051
|
+
@headers[name] = value
|
1052
|
+
@header_keys_lower_case << name.downcase
|
1053
|
+
self
|
1054
|
+
end
|
1055
|
+
|
1056
|
+
def with_auth(auth)
|
1057
|
+
Preconditions.assert_class('auth', auth, HttpClient::Authorization)
|
1058
|
+
Preconditions.check_state(@auth.nil?, "auth previously set")
|
1059
|
+
|
1060
|
+
if auth.scheme.name == AuthScheme::BASIC.name
|
1061
|
+
@auth = auth
|
1062
|
+
else
|
1063
|
+
raise "Auth Scheme[#{auth.scheme.name}] not supported"
|
1064
|
+
end
|
1065
|
+
self
|
1066
|
+
end
|
1067
|
+
|
1068
|
+
def with_query(params)
|
1069
|
+
Preconditions.assert_class('params', params, Hash)
|
1070
|
+
Preconditions.check_state(@params.nil?, "Already have query parameters")
|
1071
|
+
@params = params
|
1072
|
+
self
|
1073
|
+
end
|
1074
|
+
|
1075
|
+
# Wrapper to set Content-Type header to application/json and set
|
1076
|
+
# the provided json document as the body
|
1077
|
+
def with_json(json)
|
1078
|
+
@headers['Content-Type'] ||= 'application/json; charset=UTF-8'
|
1079
|
+
with_body(json)
|
1080
|
+
end
|
1081
|
+
|
1082
|
+
def with_body(body)
|
1083
|
+
Preconditions.check_not_blank('body', body)
|
1084
|
+
@body = body
|
1085
|
+
self
|
1086
|
+
end
|
1087
|
+
|
1088
|
+
# Creates a new Net:HTTP client. The client returned should be
|
1089
|
+
# fully configured to make a request.
|
1090
|
+
def new_http_client
|
1091
|
+
client = Net::HTTP.new(@uri.host, @uri.port)
|
1092
|
+
if @uri.scheme == "https"
|
1093
|
+
configure_ssl(client)
|
1094
|
+
end
|
1095
|
+
client
|
1096
|
+
end
|
1097
|
+
|
1098
|
+
# If HTTP is required, this method accepts an HTTP Client and configures SSL
|
1099
|
+
def configure_ssl(client)
|
1100
|
+
Preconditions.assert_class('client', client, Net::HTTP)
|
1101
|
+
client.use_ssl = true
|
1102
|
+
client.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
1103
|
+
client.cert_store = OpenSSL::X509::Store.new
|
1104
|
+
client.cert_store.set_default_paths
|
1105
|
+
end
|
1106
|
+
|
1107
|
+
def get(&block)
|
1108
|
+
do_request(Net::HTTP::Get, &block)
|
1109
|
+
end
|
1110
|
+
|
1111
|
+
def delete(&block)
|
1112
|
+
do_request(Net::HTTP::Delete, &block)
|
1113
|
+
end
|
1114
|
+
|
1115
|
+
def options(&block)
|
1116
|
+
do_request(Net::HTTP::Options, &block)
|
1117
|
+
end
|
1118
|
+
|
1119
|
+
def post(&block)
|
1120
|
+
do_request(Net::HTTP::Post, &block)
|
1121
|
+
end
|
1122
|
+
|
1123
|
+
def put(&block)
|
1124
|
+
do_request(Net::HTTP::Put, &block)
|
1125
|
+
end
|
1126
|
+
|
1127
|
+
class PATCH < Net::HTTP::Put
|
1128
|
+
METHOD = "PATCH"
|
1129
|
+
end
|
1130
|
+
|
1131
|
+
def patch(&block)
|
1132
|
+
do_request(PATCH, &block)
|
1133
|
+
end
|
1134
|
+
|
1135
|
+
def do_request(klass)
|
1136
|
+
Preconditions.assert_class('klass', klass, Class)
|
1137
|
+
|
1138
|
+
uri = @uri.to_s
|
1139
|
+
if q = to_query(@params)
|
1140
|
+
uri += "?%s" % q
|
1141
|
+
end
|
1142
|
+
|
1143
|
+
request = klass.send(:new, uri)
|
1144
|
+
|
1145
|
+
curl = ['curl']
|
1146
|
+
if klass != Net::HTTP::Get
|
1147
|
+
curl << "-X%s" % klass.name.split("::").last.upcase
|
1148
|
+
end
|
1149
|
+
|
1150
|
+
if @body
|
1151
|
+
# DEBUG path = "/tmp/rest_client.tmp"
|
1152
|
+
# DEBUG File.open(path, "w") { |os| os << @body.to_s }
|
1153
|
+
# DEBUG curl << "-d@%s" % path
|
1154
|
+
request.body = @body
|
1155
|
+
end
|
1156
|
+
|
1157
|
+
if @auth
|
1158
|
+
curl << "-u \"%s:%s\"" % [@auth.username, @auth.password]
|
1159
|
+
Preconditions.check_state(!@header_keys_lower_case.include?("authorization"),
|
1160
|
+
"Cannot specify both an Authorization header and an auth instance")
|
1161
|
+
user_pass = "%s:%s" % [@auth.username, @auth.password]
|
1162
|
+
encoded = Base64.encode64(user_pass).to_s.split("\n").map(&:strip).join
|
1163
|
+
request.add_field("Authorization", "Basic %s" % encoded)
|
1164
|
+
end
|
1165
|
+
|
1166
|
+
@headers.each { |key, value|
|
1167
|
+
curl << "-H \"%s: %s\"" % [key, value]
|
1168
|
+
request.add_field(key, value)
|
1169
|
+
}
|
1170
|
+
|
1171
|
+
curl << "'%s'" % uri
|
1172
|
+
# DEBUG puts curl.join(" ")
|
1173
|
+
|
1174
|
+
raw_response = http_request(request)
|
1175
|
+
response = raw_response.to_s == "" ? nil : JSON.parse(raw_response)
|
1176
|
+
|
1177
|
+
if block_given?
|
1178
|
+
yield response
|
1179
|
+
else
|
1180
|
+
response
|
1181
|
+
end
|
1182
|
+
end
|
1183
|
+
|
1184
|
+
private
|
1185
|
+
def to_query(params={})
|
1186
|
+
parts = (params || {}).map { |k,v|
|
1187
|
+
if v.respond_to?(:each)
|
1188
|
+
v.map { |el| "%s=%s" % [k, CGI.escape(el.to_s)] }
|
1189
|
+
else
|
1190
|
+
"%s=%s" % [k, CGI.escape(v.to_s)]
|
1191
|
+
end
|
1192
|
+
}
|
1193
|
+
parts.empty? ? nil : parts.join("&")
|
1194
|
+
end
|
1195
|
+
|
1196
|
+
def http_request(request)
|
1197
|
+
response = begin
|
1198
|
+
new_http_client.request(request)
|
1199
|
+
rescue SocketError => e
|
1200
|
+
raise Exception.new("Error accessing uri[#{@uri}]: #{e}")
|
1201
|
+
end
|
1202
|
+
|
1203
|
+
case response
|
1204
|
+
when Net::HTTPSuccess
|
1205
|
+
response.body
|
1206
|
+
else
|
1207
|
+
body = response.body rescue nil
|
1208
|
+
raise HttpClient::ServerError.new(response.code.to_i, response.message, :body => body, :uri => @uri.to_s)
|
1209
|
+
end
|
1210
|
+
end
|
1211
|
+
end
|
1212
|
+
|
1213
|
+
class ServerError < StandardError
|
1214
|
+
|
1215
|
+
attr_reader :code, :details, :body, :uri
|
1216
|
+
|
1217
|
+
def initialize(code, details, incoming={})
|
1218
|
+
opts = HttpClient::Helper.symbolize_keys(incoming)
|
1219
|
+
@code = HttpClient::Preconditions.assert_class('code', code, Integer)
|
1220
|
+
@details = HttpClient::Preconditions.assert_class('details', details, String)
|
1221
|
+
@body = HttpClient::Preconditions.assert_class_or_nil('body', opts.delete(:body), String)
|
1222
|
+
@uri = HttpClient::Preconditions.assert_class_or_nil('uri', opts.delete(:uri), String)
|
1223
|
+
HttpClient::Preconditions.assert_empty_opts(opts)
|
1224
|
+
super(self.message)
|
1225
|
+
end
|
1226
|
+
|
1227
|
+
def message
|
1228
|
+
m = "%s %s" % [@code, @details]
|
1229
|
+
if @body
|
1230
|
+
m << ": %s" % @body
|
1231
|
+
end
|
1232
|
+
m
|
1233
|
+
end
|
1234
|
+
|
1235
|
+
def body_json
|
1236
|
+
JSON.parse(@body)
|
1237
|
+
end
|
1238
|
+
|
1239
|
+
end
|
1240
|
+
|
1241
|
+
class PreconditionException < Exception
|
1242
|
+
|
1243
|
+
attr_reader :message
|
1244
|
+
|
1245
|
+
def initialize(message)
|
1246
|
+
super(message)
|
1247
|
+
@message = message
|
1248
|
+
end
|
1249
|
+
|
1250
|
+
end
|
1251
|
+
|
1252
|
+
module Preconditions
|
1253
|
+
|
1254
|
+
def Preconditions.check_argument(expression, error_message=nil)
|
1255
|
+
if !expression
|
1256
|
+
raise PreconditionException.new(error_message || "check_argument failed")
|
1257
|
+
end
|
1258
|
+
nil
|
1259
|
+
end
|
1260
|
+
|
1261
|
+
def Preconditions.check_state(expression, error_message=nil)
|
1262
|
+
if !expression
|
1263
|
+
raise PreconditionException.new(error_message || "check_state failed")
|
1264
|
+
end
|
1265
|
+
nil
|
1266
|
+
end
|
1267
|
+
|
1268
|
+
def Preconditions.check_not_nil(field_name, reference, error_message=nil)
|
1269
|
+
if reference.nil?
|
1270
|
+
raise PreconditionException.new(error_message || "argument for %s cannot be nil" % field_name)
|
1271
|
+
end
|
1272
|
+
reference
|
1273
|
+
end
|
1274
|
+
|
1275
|
+
def Preconditions.check_not_blank(field_name, reference, error_message=nil)
|
1276
|
+
if reference.to_s.strip == ""
|
1277
|
+
raise PreconditionException.new(error_message || "argument for %s cannot be blank" % field_name)
|
1278
|
+
end
|
1279
|
+
reference
|
1280
|
+
end
|
1281
|
+
|
1282
|
+
# Throws an error if opts is not empty. Useful when parsing
|
1283
|
+
# arguments to a function
|
1284
|
+
def Preconditions.assert_empty_opts(opts)
|
1285
|
+
if !opts.empty?
|
1286
|
+
raise PreconditionException.new("Invalid opts: #{opts.keys.inspect}\n#{opts.inspect}")
|
1287
|
+
end
|
1288
|
+
end
|
1289
|
+
|
1290
|
+
# Requires that the provided hash has the specified keys.
|
1291
|
+
# @param fields A list of symbols
|
1292
|
+
def Preconditions.require_keys(hash, fields, error_prefix=nil)
|
1293
|
+
missing = fields.select { |f| !hash.has_key?(f) }
|
1294
|
+
if !missing.empty?
|
1295
|
+
msg = "Missing required fields: " + missing.join(", ")
|
1296
|
+
raise PreconditionException.new(error_prefix.empty? ? msg : "#{error_prefix}: #{msg}")
|
1297
|
+
end
|
1298
|
+
end
|
1299
|
+
|
1300
|
+
# Asserts that value is not nill and is_?(klass). Returns
|
1301
|
+
# value. Common use is
|
1302
|
+
#
|
1303
|
+
# amount = Preconditions.assert_class('amount', amount, BigDecimal)
|
1304
|
+
def Preconditions.assert_class(field_name, value, klass)
|
1305
|
+
Preconditions.check_not_nil('field_name', field_name)
|
1306
|
+
Preconditions.check_not_nil('klass', klass)
|
1307
|
+
Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of class %s" % [field_name, klass.name])
|
1308
|
+
Preconditions.check_state(value.is_a?(klass),
|
1309
|
+
"Value for #{field_name} is of type[#{value.class}] - class[#{klass}] is required. value[#{value.inspect.to_s}]")
|
1310
|
+
value
|
1311
|
+
end
|
1312
|
+
|
1313
|
+
def Preconditions.assert_class_or_nil(field_name, value, klass)
|
1314
|
+
if !value.nil?
|
1315
|
+
Preconditions.assert_class(field_name, value, klass)
|
1316
|
+
end
|
1317
|
+
end
|
1318
|
+
|
1319
|
+
def Preconditions.assert_boolean(field_name, value)
|
1320
|
+
Preconditions.check_not_nil('field_name', field_name)
|
1321
|
+
Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of TrueClass or FalseClass" % field_name)
|
1322
|
+
Preconditions.check_state(value.is_a?(TrueClass) || value.is_a?(FalseClass),
|
1323
|
+
"Value for #{field_name} is of type[#{value.class}] - class[TrueClass or FalseClass] is required. value[#{value.inspect.to_s}]")
|
1324
|
+
value
|
1325
|
+
end
|
1326
|
+
|
1327
|
+
def Preconditions.assert_boolean_or_nil(field_name, value)
|
1328
|
+
if !value.nil?
|
1329
|
+
Preconditions.assert_boolean(field_name, value)
|
1330
|
+
end
|
1331
|
+
end
|
1332
|
+
|
1333
|
+
def Preconditions.assert_collection_of_class(field_name, values, klass)
|
1334
|
+
Preconditions.assert_class(field_name, values, Array)
|
1335
|
+
values.each { |v| Preconditions.assert_class(field_name, v, klass) }
|
1336
|
+
end
|
1337
|
+
|
1338
|
+
def Preconditions.assert_hash_of_class(field_name, hash, klass)
|
1339
|
+
Preconditions.assert_class(field_name, hash, Hash)
|
1340
|
+
values.each { |k, v| Preconditions.assert_class(field_name, v, klass) }
|
1341
|
+
end
|
1342
|
+
|
1343
|
+
end
|
1344
|
+
|
1345
|
+
class AuthScheme
|
1346
|
+
|
1347
|
+
attr_reader :name
|
1348
|
+
|
1349
|
+
def initialize(name)
|
1350
|
+
@name = HttpClient::Preconditions.check_not_blank('name', name)
|
1351
|
+
end
|
1352
|
+
|
1353
|
+
BASIC = AuthScheme.new("basic") unless defined?(BASIC)
|
1354
|
+
|
1355
|
+
end
|
1356
|
+
|
1357
|
+
class Authorization
|
1358
|
+
|
1359
|
+
attr_reader :scheme, :username, :password
|
1360
|
+
|
1361
|
+
def initialize(scheme, username, opts={})
|
1362
|
+
@scheme = HttpClient::Preconditions.assert_class('schema', scheme, AuthScheme)
|
1363
|
+
@username = HttpClient::Preconditions.check_not_blank('username', username, "username is required")
|
1364
|
+
@password = HttpClient::Preconditions.assert_class_or_nil('password', opts.delete(:password), String)
|
1365
|
+
HttpClient::Preconditions.assert_empty_opts(opts)
|
1366
|
+
end
|
1367
|
+
|
1368
|
+
def Authorization.basic(username, password=nil)
|
1369
|
+
Authorization.new(AuthScheme::BASIC, username, :password => password)
|
1370
|
+
end
|
1371
|
+
|
1372
|
+
end
|
1373
|
+
|
1374
|
+
module Helper
|
1375
|
+
|
1376
|
+
def Helper.symbolize_keys(hash)
|
1377
|
+
Preconditions.assert_class('hash', hash, Hash)
|
1378
|
+
new_hash = {}
|
1379
|
+
hash.each { |k, v|
|
1380
|
+
new_hash[k.to_sym] = v
|
1381
|
+
}
|
1382
|
+
new_hash
|
1383
|
+
end
|
1384
|
+
|
1385
|
+
def Helper.to_big_decimal(value)
|
1386
|
+
value ? BigDecimal.new(value.to_s) : nil
|
1387
|
+
end
|
1388
|
+
|
1389
|
+
def Helper.to_object(value)
|
1390
|
+
value ? JSON.parse(value) : nil
|
1391
|
+
end
|
1392
|
+
|
1393
|
+
def Helper.to_uuid(value)
|
1394
|
+
Preconditions.check_state(value.nil? || value.match(/^\w\w\w\w\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\w\w\w\w\w\w\w\w$/),
|
1395
|
+
"Invalid guid[%s]" % value)
|
1396
|
+
value
|
1397
|
+
end
|
1398
|
+
|
1399
|
+
def Helper.to_date_iso8601(value)
|
1400
|
+
if value.is_a?(Date)
|
1401
|
+
value
|
1402
|
+
elsif value
|
1403
|
+
Date.parse(value.to_s)
|
1404
|
+
else
|
1405
|
+
nil
|
1406
|
+
end
|
1407
|
+
end
|
1408
|
+
|
1409
|
+
def Helper.to_date_time_iso8601(value)
|
1410
|
+
if value.is_a?(DateTime)
|
1411
|
+
value
|
1412
|
+
elsif value
|
1413
|
+
DateTime.parse(value.to_s)
|
1414
|
+
else
|
1415
|
+
nil
|
1416
|
+
end
|
1417
|
+
end
|
1418
|
+
|
1419
|
+
def Helper.date_iso8601_to_string(value)
|
1420
|
+
value.nil? ? nil : value.strftime('%Y-%m-%d')
|
1421
|
+
end
|
1422
|
+
|
1423
|
+
def Helper.date_time_iso8601_to_string(value)
|
1424
|
+
value.nil? ? nil : value.strftime('%Y-%m-%dT%H:%M:%S%z')
|
1425
|
+
end
|
1426
|
+
|
1427
|
+
TRUE_STRINGS = ['t', 'true', 'y', 'yes', 'on', '1', 'trueclass'] unless defined?(TRUE_STRINGS)
|
1428
|
+
FALSE_STRINGS = ['f', 'false', 'n', 'no', 'off', '0', 'falseclass'] unless defined?(FALSE_STRINGS)
|
1429
|
+
|
1430
|
+
def Helper.to_boolean(field_name, value)
|
1431
|
+
string = value.to_s.strip.downcase
|
1432
|
+
if TRUE_STRINGS.include?(string)
|
1433
|
+
true
|
1434
|
+
elsif FALSE_STRINGS.include?(string)
|
1435
|
+
false
|
1436
|
+
elsif string != ""
|
1437
|
+
raise PreconditionException.new("Unsupported boolean value[#{string}]. For true, must be one of: #{TRUE_STRINGS.inspect}. For false, must be one of: #{FALSE_STRINGS.inspect}")
|
1438
|
+
else
|
1439
|
+
nil
|
1440
|
+
end
|
1441
|
+
end
|
1442
|
+
|
1443
|
+
end
|
1444
|
+
|
1445
|
+
end
|
1446
|
+
end
|
1447
|
+
end
|
1448
|
+
end
|
1449
|
+
end
|