-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathclient.rb
More file actions
961 lines (844 loc) · 31.1 KB
/
client.rb
File metadata and controls
961 lines (844 loc) · 31.1 KB
1
2
3
4
5
6
7
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
36
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
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
173
174
175
176
177
178
179
180
181
182
183
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
266
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
require "httparty"
require "multi_json"
require "base64"
module Sift
# Represents the payload returned from a call through the track API
#
class Response
attr_reader :body,
:http_class,
:http_status_code,
:api_status,
:api_error_message,
:request,
:api_error_description,
:api_error_issues
# Constructor
#
# ==== Parameters:
#
# http_response::
# The HTTP body text returned from the API call. The body is expected to be
# a JSON object that can be decoded into status, message and request
# sections.
#
def initialize(http_response, http_response_code, http_raw_response)
@http_status_code = http_response_code
@http_raw_response = http_raw_response
# only set these variables if a message-body is expected.
if not @http_raw_response.kind_of? Net::HTTPNoContent
begin
@body = MultiJson.load(http_response) unless http_response.nil?
rescue
if @http_status_code == 200
raise TypeError.new
end
end
if not @body.nil?
@request = MultiJson.load(@body["request"].to_s) if @body["request"]
@api_status = @body["status"].to_i if @body["status"]
@api_error_message = @body["error_message"]
if @body["error"]
@api_error_message = @body["error"]
@api_error_description = @body["description"]
@api_error_issues = @body["issues"] || {}
end
end
end
end
# Helper method returns true if and only if the response from the API call was
# successful
#
# ==== Returns:
#
# true on success; false otherwise
#
def ok?
if @http_raw_response.kind_of? Net::HTTPNoContent
#if there is no content expected, use HTTP code
204 == @http_status_code
else
# otherwise use API status
@http_raw_response.kind_of? Net::HTTPOK and 0 == @api_status.to_i
end
end
# DEPRECATED
# Getter method for deprecated 'json' member variable.
def json
@body
end
# DEPRECATED
# Getter method for deprecated 'original_request' member variable.
def original_request
@request
end
end
# This class wraps accesses through the API
#
class Client
API_ENDPOINT = ENV["SIFT_RUBY_API_URL"] || 'https://api.siftscience.com'
API3_ENDPOINT = ENV["SIFT_RUBY_API3_URL"] || 'https://api3.siftscience.com'
# Maintain backward compatibility for users who may rely on HTTParty methods
include HTTParty
base_uri API_ENDPOINT
attr_reader :api_key, :account_id
class << self
def build_auth_header(api_key)
{ "Authorization" => "Basic #{Base64.encode64(api_key)}" }
end
def user_agent
"sift-ruby/#{VERSION}"
end
# Factory methods for internal API executors that inherit from the current class context.
# This ensures that subclasses of Client propagate their HTTParty configuration
# to these internal clients.
def api_client
@api_client ||= Class.new(self) do
base_uri API_ENDPOINT
end
end
def api3_client
@api3_client ||= Class.new(self) do
base_uri API3_ENDPOINT
end
end
end
# Constructor
#
# ==== Parameters:
#
# opts (optional)::
# A Hash of optional parameters for this Client --
#
# :api_key::
# The Sift Science API key associated with your account.
# Sift.api_key is used if this parameter is not set.
#
# :account_id::
# The ID of your Sift Science account. Sift.account_id is
# used if this parameter is not set.
#
# :timeout::
# The number of seconds to wait before failing a request. By
# default this is configured to 2 seconds.
#
# :version::
# The version of the Events API, Score API, and Labels API to call.
# By default, version 205.
#
# :path::
# The URL path to use for Events API path. By default, the
# official path of the specified-version of the Events API.
#
#
def initialize(opts = {})
@api_key = opts[:api_key] || Sift.api_key
@account_id = opts[:account_id] || Sift.account_id
@version = opts[:version] || API_VERSION
@verification_version = opts[:verification_version] || VERIFICATION_API_VERSION
@timeout = opts[:timeout] || 2 # 2-second timeout by default
@path = opts[:path] || Sift.rest_api_path(@version)
raise("api_key") if !@api_key.is_a?(String) || @api_key.empty?
raise("path must be a non-empty string") if !@path.is_a?(String) || @path.empty?
end
def user_agent
"SiftScience/v#{@version} sift-ruby/#{VERSION}"
end
# Sends an event to the Sift Science Events API.
#
# See https://siftscience.com/developers/docs/ruby/events-api .
#
# ==== Parameters:
#
# event::
# The name of the event to send. This can be either a reserved
# event name, like $transaction or $label or a custom event name
# (that does not start with a $). This parameter must be
# specified.
#
# properties::
# A hash of name-value pairs that specify the event-specific
# attributes to track. This parameter must be specified.
#
# opts (optional)::
# A Hash of optional parameters for the request --
#
# :return_score::
# If true, requests that the response include a score for this
# user, computed using the submitted event. See
# https://siftscience.com/developers/docs/ruby/score-api/synchronous-scores
#
# :abuse_types::
# List of abuse types, specifying for which abuse types a
# score should be returned (if scoring was requested). By
# default, a score is returned for every abuse type to which
# you are subscribed.
#
# :return_action::
# If true, requests that the response include any actions
# triggered as a result of the tracked event.
#
# :return_workflow_status::
# If true, requests that the response include the status of
# any workflow run as a result of the tracked event. See
# https://siftscience.com/developers/docs/ruby/workflows-api/workflow-decisions
#
# :timeout::
# Overrides the timeout (in seconds) for this call.
#
# :api_key::
# Overrides the API key for this call.
#
# :version::
# Overrides the version of the Events API to call.
#
# :path::
# Overrides the URI path for this API call.
#
# :include_score_percentiles::
# include_score_percentiles(optional) : Whether to add new parameter in the query parameter.
#
# :warnings::
# warnings(optional) : Whether to add list of warnings (if any) to response.
#
# ==== Returns:
#
# In the case of a network error (timeout, broken connection, etc.),
# this method propagates the exception, otherwise, a Response object is
# returned that captures the status message and status code.
#
def track(event, properties = {}, opts = {})
api_key = opts[:api_key] || @api_key
version = opts[:version] || @version
path = opts[:path] || (version && Sift.rest_api_path(version)) || @path
timeout = opts[:timeout] || @timeout
return_score = opts[:return_score]
return_action = opts[:return_action]
return_workflow_status = opts[:return_workflow_status]
return_route_info = opts[:return_route_info]
force_workflow_run = opts[:force_workflow_run]
abuse_types = opts[:abuse_types]
include_score_percentiles = opts[:include_score_percentiles]
warnings = opts[:warnings]
raise("event must be a non-empty string") if (!event.is_a? String) || event.empty?
raise("properties cannot be empty") if properties.empty?
raise("api_key cannot be empty") if api_key.empty?
query = {}
query["return_score"] = "true" if return_score
query["return_action"] = "true" if return_action
query["return_workflow_status"] = "true" if return_workflow_status
query["return_route_info"] = "true" if return_route_info
query["force_workflow_run"] = "true" if force_workflow_run
query["abuse_types"] = abuse_types.join(",") if abuse_types
if include_score_percentiles == "true" || warnings == "true"
fields = []
fields << "SCORE_PERCENTILES" if include_score_percentiles == "true"
fields << "WARNINGS" if warnings == "true"
query["fields"] = fields.join(",")
end
options = {
:body => MultiJson.dump(delete_nils(properties).merge({"$type" => event,
"$api_key" => api_key})),
:headers => {"User-Agent" => user_agent},
:query => query
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.post(path, options)
Response.new(response.body, response.code, response.response)
end
# Retrieves a user's fraud score from the Sift Science API.
#
# See https://siftscience.com/developers/docs/ruby/score-api/score-api .
#
# ==== Parameters:
#
# user_id::
# A user's id. This id should be the same as the user_id used in
# event calls.
#
# opts (optional)::
# A Hash of optional parameters for the request --
#
# :abuse_types::
# List of abuse types, specifying for which abuse types a
# score should be returned. By default, a score is returned
# for every abuse type to which you are subscribed.
#
# :api_key::
# Overrides the API key for this call.
#
# :timeout::
# Overrides the timeout (in seconds) for this call.
#
# :version::
# Overrides the version of the Events API to call.
#
# :include_score_percentiles::
# include_score_percentiles(optional) : Whether to add new parameter in the query parameter.
#
# ==== Returns:
#
# A Response object containing a status code, status message, and,
# if successful, the user's score(s).
#
def score(user_id, opts = {})
abuse_types = opts[:abuse_types]
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
version = opts[:version] || @version
include_score_percentiles = opts[:include_score_percentiles]
raise("user_id must be a non-empty string") if (!user_id.is_a? String) || user_id.to_s.empty?
raise("Bad api_key parameter") if api_key.empty?
query = {}
query["api_key"] = api_key
query["abuse_types"] = abuse_types.join(",") if abuse_types
if include_score_percentiles == "true"
query["fields"] = "SCORE_PERCENTILES"
end
options = {
:headers => {"User-Agent" => user_agent},
:query => query
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.get(Sift.score_api_path(user_id, version), options)
Response.new(response.body, response.code, response.response)
end
# Fetches the latest score(s) computed for the specified user and abuse types.
#
# As opposed to client.score() and client.rescore_user(), this *does not* compute
# a new score for the user; it simply fetches the latest score(s) which have computed.
# These scores may be arbitrarily old.
#
# See https://siftscience.com/developers/docs/ruby/score-api/get-score for more details.
#
# ==== Parameters:
#
# user_id::
# A user's id. This id should be the same as the user_id used in
# event calls.
#
# opts (optional)::
# A Hash of optional parameters for the request --
#
# :abuse_types::
# List of abuse types, specifying for which abuse types a
# score should be returned. By default, a score is returned
# for every abuse type to which you are subscribed.
#
# :api_key::
# Overrides the API key for this call.
#
# :timeout::
# Overrides the timeout (in seconds) for this call.
#
# :include_score_percentiles::
# include_score_percentiles(optional) : Whether to add new parameter in the query parameter.
#
# ==== Returns:
#
# A Response object containing a status code, status message, and,
# if successful, the user's score(s).
#
def get_user_score(user_id, opts = {})
abuse_types = opts[:abuse_types]
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
include_score_percentiles = opts[:include_score_percentiles]
raise("user_id must be a non-empty string") if (!user_id.is_a? String) || user_id.to_s.empty?
raise("Bad api_key parameter") if api_key.empty?
query = {}
query["api_key"] = api_key
query["abuse_types"] = abuse_types.join(",") if abuse_types
if include_score_percentiles == "true"
query["fields"] = "SCORE_PERCENTILES"
end
options = {
:headers => {"User-Agent" => user_agent},
:query => query
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.get(Sift.user_score_api_path(user_id, @version), options)
Response.new(response.body, response.code, response.response)
end
# Rescores the specified user for the specified abuse types and returns the resulting score(s).
#
# See https://siftscience.com/developers/docs/ruby/score-api/rescore for more details.
#
# ==== Parameters:
#
# user_id::
# A user's id. This id should be the same as the user_id used in
# event calls.
#
# opts (optional)::
# A Hash of optional parameters for the request --
#
# :abuse_types::
# List of abuse types, specifying for which abuse types a
# score should be returned. By default, a score is returned
# for every abuse type to which you are subscribed.
#
# :api_key::
# Overrides the API key for this call.
#
# :timeout::
# Overrides the timeout (in seconds) for this call.
#
# ==== Returns:
#
# A Response object containing a status code, status message, and,
# if successful, the user's score(s).
#
def rescore_user(user_id, opts = {})
abuse_types = opts[:abuse_types]
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
raise("user_id must be a non-empty string") if (!user_id.is_a? String) || user_id.to_s.empty?
raise("Bad api_key parameter") if api_key.empty?
query = {}
query["api_key"] = api_key
query["abuse_types"] = abuse_types.join(",") if abuse_types
options = {
:headers => {"User-Agent" => user_agent},
:query => query
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.post(Sift.user_score_api_path(user_id, @version), options)
Response.new(response.body, response.code, response.response)
end
# Labels a user.
#
# See https://siftscience.com/developers/docs/ruby/labels-api/label-user .
#
# ==== Parameters:
#
# user_id::
# A user's id. This id should be the same as the user_id used in
# event calls.
#
# properties::
# A hash of name-value pairs that specify the label attributes.
# This parameter must be specified.
#
# opts (optional)::
# A Hash of optional parameters for the request --
#
# :api_key::
# Overrides the API key for this call.
#
# :timeout::
# Overrides the timeout (in seconds) for this call.
#
# :version::
# Overrides the version of the Events API to call.
#
# ==== Returns:
#
# In the case of a connection error (timeout, broken connection,
# etc.), this method returns nil; otherwise, a Response object is
# returned that captures the status message and status code.
#
def label(user_id, properties = {}, opts = {})
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
version = opts[:version] || @version
path = Sift.users_label_api_path(user_id, version)
raise("user_id must be a non-empty string") if (!user_id.is_a? String) || user_id.to_s.empty?
track("$label", delete_nils(properties),
:path => path, :api_key => api_key, :timeout => timeout)
end
# Unlabels a user.
#
# See https://siftscience.com/developers/docs/ruby/labels-api/unlabel-user .
#
# ==== Parameters:
#
# user_id::
# A user's id. This id should be the same as the user_id used in
# event calls.
#
# opts (optional)::
# A Hash of optional parameters for this request --
#
# :abuse_type::
# The abuse type for which the user should be unlabeled. If
# omitted, the user is unlabeled for all abuse types.
#
# :api_key::
# Overrides the API key for this call.
#
# :timeout::
# Overrides the timeout (in seconds) for this call.
#
# :version::
# Overrides the version of the Events API to call.
#
# ==== Returns:
#
# A Response object is returned with only an http code of 204.
#
def unlabel(user_id, opts = {})
abuse_type = opts[:abuse_type]
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
version = opts[:version] || @version
raise("user_id must be a non-empty string") if (!user_id.is_a? String) || user_id.to_s.empty?
query = {}
query[:api_key] = api_key
query[:abuse_type] = abuse_type if abuse_type
options = {
:headers => {},
:query => query
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.delete(Sift.users_label_api_path(user_id, version), options)
Response.new(response.body, response.code, response.response)
end
# Gets the status of a workflow run.
#
# See https://siftscience.com/developers/docs/ruby/workflows-api/workflow-status .
#
# ==== Parameters
#
# run_id::
# The ID of a workflow run.
#
# opts (optional)::
# A Hash of optional parameters for this request --
#
# :account_id::
# Overrides the API key for this call.
#
# :api_key::
# Overrides the API key for this call.
#
# :timeout::
# Overrides the timeout (in seconds) for this call.
#
def get_workflow_status(run_id, opts = {})
account_id = opts[:account_id] || @account_id
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
options = {
:headers => { "User-Agent" => user_agent },
:basic_auth => { :username => api_key, :password => "" }
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api3_client.get(Sift.workflow_status_path(account_id, run_id), options)
Response.new(response.body, response.code, response.response)
end
# Gets the decision status of a user.
#
# See https://siftscience.com/developers/docs/ruby/decisions-api/decision-status .
#
# ==== Parameters
#
# user_id::
# The ID of user.
#
# opts (optional)::
# A Hash of optional parameters for this request --
#
# :account_id::
# Overrides the API key for this call.
#
# :api_key::
# Overrides the API key for this call.
#
# :timeout::
# Overrides the timeout (in seconds) for this call.
#
def get_user_decisions(user_id, opts = {})
account_id = opts[:account_id] || @account_id
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
options = {
:headers => { "User-Agent" => user_agent },
:basic_auth => { :username => api_key, :password => "" }
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api3_client.get(Sift.user_decisions_api_path(account_id, user_id), options)
Response.new(response.body, response.code, response.response)
end
# Gets the decision status of an order.
#
# See https://siftscience.com/developers/docs/ruby/decisions-api/decision-status .
#
# ==== Parameters
#
# order_id::
# The ID of an order.
#
# opts (optional)::
# A Hash of optional parameters for this request --
#
# :account_id::
# Overrides the API key for this call.
#
# :api_key::
# Overrides the API key for this call.
#
# :timeout::
# Overrides the timeout (in seconds) for this call.
#
def get_order_decisions(order_id, opts = {})
account_id = opts[:account_id] || @account_id
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
options = {
:headers => { "User-Agent" => user_agent },
:basic_auth => { :username => api_key, :password => "" }
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api3_client.get(Sift.order_decisions_api_path(account_id, order_id), options)
Response.new(response.body, response.code, response.response)
end
# Gets the decision status of a session.
#
# See https://siftscience.com/developers/docs/ruby/decisions-api/decision-status .
#
# ==== Parameters
#
# user_id::
# The ID of the user in the session.
#
# session_id::
# The ID of a session.
#
# opts (optional)::
# A Hash of optional parameters for this request --
#
# :account_id::
# Overrides the account id for this call.
#
# :api_key::
# Overrides the API key for this call.
#
# :timeout::
# Overrides the timeout (in seconds) for this call.
#
def get_session_decisions(user_id, session_id, opts = {})
account_id = opts[:account_id] || @account_id
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
options = {
:headers => { "User-Agent" => user_agent },
:basic_auth => { :username => api_key, :password => "" }
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api3_client.get(Sift.session_decisions_api_path(account_id, user_id, session_id), options)
Response.new(response.body, response.code, response.response)
end
# Gets the decision status of a piece of content.
#
# See https://siftscience.com/developers/docs/ruby/decisions-api/decision-status .
#
# ==== Parameters
#
# user_id::
# The ID of the owner of the content.
#
# content_id::
# The ID of a piece of content.
#
# opts (optional)::
# A Hash of optional parameters for this request --
#
# :account_id::
# Overrides the API key for this call.
#
# :api_key::
# Overrides the API key for this call.
#
# :timeout::
# Overrides the timeout (in seconds) for this call.
#
def get_content_decisions(user_id, content_id, opts = {})
account_id = opts[:account_id] || @account_id
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
options = {
:headers => { "User-Agent" => user_agent },
:basic_auth => { :username => api_key, :password => "" }
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api3_client.get(Sift.content_decisions_api_path(account_id, user_id, content_id), options)
Response.new(response.body, response.code, response.response)
end
def decisions(opts = {})
decision_instance.list(opts)
end
def decisions!(opts = {})
handle_response(decisions(opts))
end
def apply_decision(configs = {})
decision_instance.apply_to(configs)
end
def apply_decision!(configs = {})
handle_response(apply_decision(configs))
end
def build_default_headers_post(api_key)
{
"Authorization" => "Basic #{Base64.encode64(api_key+":")}",
"User-Agent" => "SiftScience/v#{@version} sift-ruby/#{VERSION}",
"Content-Type" => "application/json"
}
end
def verification_send(properties = {}, opts = {})
api_key = opts[:api_key] || @api_key
version = opts[:verification_version] || @verification_version
timeout = opts[:timeout] || @timeout
raise("properties cannot be empty") if properties.empty?
raise("api_key cannot be empty") if api_key.empty?
options = {
:body => MultiJson.dump(delete_nils(properties)),
:headers => build_default_headers_post(api_key)
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.post(Sift.verification_api_send_path(version), options)
Response.new(response.body, response.code, response.response)
end
def verification_resend(properties = {}, opts = {})
api_key = opts[:api_key] || @api_key
version = opts[:verification_version] || @verification_version
timeout = opts[:timeout] || @timeout
raise("properties cannot be empty") if properties.empty?
raise("api_key cannot be empty") if api_key.empty?
options = {
:body => MultiJson.dump(delete_nils(properties)),
:headers => build_default_headers_post(api_key)
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.post(Sift.verification_api_resend_path(version), options)
Response.new(response.body, response.code, response.response)
end
def verification_check(properties = {}, opts = {})
api_key = opts[:api_key] || @api_key
version = opts[:verification_version] || @verification_version
timeout = opts[:timeout] || @timeout
raise("properties cannot be empty") if properties.empty?
raise("api_key cannot be empty") if api_key.empty?
options = {
:body => MultiJson.dump(delete_nils(properties)),
:headers => build_default_headers_post(api_key)
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.post(Sift.verification_api_check_path(version), options)
Response.new(response.body, response.code, response.response)
end
def create_psp_merchant_profile(properties = {}, opts = {})
# Create a new PSP Merchant profile
# Args:
# properties: A dict of merchant profile data.
# Returns
# A sift.client.Response object if the call succeeded, else raises an ApiException
account_id = opts[:account_id] || @account_id
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
raise("api_key cannot be empty") if api_key.empty?
raise("account_id cannot be empty") if account_id.empty?
raise("properties cannot be empty") if properties.empty?
options = {
:body => MultiJson.dump(delete_nils(properties)),
:headers => { "User-Agent" => user_agent, "Content-Type" => "application/json" },
:basic_auth => { :username => api_key, :password => "" }
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.post(Sift.psp_merchant_api_path(account_id), options)
Response.new(response.body, response.code, response.response)
end
def update_psp_merchant_profile(merchant_id, properties = {}, opts = {})
# Update an existing PSP Merchant profile
# Args:
# merchant_id: id of merchant
# properties: A dict of merchant profile data.
# Returns
# A sift.client.Response object if the call succeeded, else raises an ApiException
account_id = opts[:account_id] || @account_id
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
raise("api_key cannot be empty") if api_key.empty?
raise("account_id cannot be empty") if account_id.empty?
raise("merchant_id cannot be empty") if merchant_id.empty?
raise("properties cannot be empty") if properties.empty?
options = {
:body => MultiJson.dump(delete_nils(properties)),
:headers => { "User-Agent" => user_agent, "Content-Type" => "application/json" },
:basic_auth => { :username => api_key, :password => "" }
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.put(Sift.psp_merchant_id_api_path(account_id, merchant_id), options)
Response.new(response.body, response.code, response.response)
end
def get_a_psp_merchant_profile(merchant_id, opts = {})
# Gets a PSP merchant profile using merchant id.
# Args:
# merchant_id: id of merchant
# Returns
# A sift.client.Response object if the call succeeded, else raises an ApiException
account_id = opts[:account_id] || @account_id
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
raise("api_key cannot be empty") if api_key.empty?
raise("account_id cannot be empty") if account_id.empty?
raise("merchant_id cannot be empty") if merchant_id.empty?
options = {
:headers => { "User-Agent" => user_agent, "Content-Type" => "application/json" },
:basic_auth => { :username => api_key, :password => "" }
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.get(Sift.psp_merchant_id_api_path(account_id, merchant_id), options)
Response.new(response.body, response.code, response.response)
end
def get_psp_merchant_profiles(batch_size = nil, batch_token = nil, opts = {})
# Get all PSP merchant profiles.
# Args:
# batch_size : Batch or page size of the paginated sequence.
# batch_token : Batch or page position of the paginated sequence.
# Returns
# A sift.client.Response object if the call succeeded, else raises an ApiException
account_id = opts[:account_id] || @account_id
api_key = opts[:api_key] || @api_key
timeout = opts[:timeout] || @timeout
raise("api_key cannot be empty") if api_key.empty?
raise("account_id cannot be empty") if account_id.empty?
query = {}
query["batch_size"] = batch_size if batch_size
query["batch_token"] = batch_token if batch_token
options = {
:headers => { "User-Agent" => user_agent, "Content-Type" => "application/json" },
:basic_auth => { :username => api_key, :password => "" },
:query => query
}
options.merge!(:timeout => timeout) unless timeout.nil?
response = self.class.api_client.get(Sift.psp_merchant_api_path(account_id), options)
Response.new(response.body, response.code, response.response)
end
private
def handle_response(response)
if response.ok?
response.body
else
raise ApiError.new(response.api_error_message, response)
end
end
def decision_instance
@decision_instance ||= Decision.new(api_key, account_id, self.class)
end
def delete_nils(properties)
properties.delete_if do |k, v|
case v
when nil
true
when Hash
delete_nils(v)
false
else
false
end
end
end
end
require_relative "./client/decision"
require_relative "./error"
end