Skip to content

Commit c2ab116

Browse files
authored
Merge pull request #59 from SiftScience/get_session_decisions
(For Gary or Jintae) Add get session decisions
2 parents e20dbdc + 3445b7b commit c2ab116

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

CHANGES.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
4.1.0.0 2018-06-01
2+
==================
3+
4+
- Add get session level decisions in Get Decisions APIs.
5+
16
4.0.1 2018-04-06
27
==================
38

@@ -86,4 +91,3 @@ INCOMPATIBLE CHANGES INTRODUCED IN API V205:
8691
==================
8792

8893
- Just the Python REST client itself.
89-

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ except sift.client.ApiException:
144144
# request failed
145145
pass
146146

147+
# Get the latest decisions for a session
148+
try:
149+
response = client.get_session_decisions('example_user', 'example_session')
150+
except sift.client.ApiException:
151+
# request failed
152+
pass
153+
147154
# Get the latest decisions for a piece of content
148155
try:
149156
response = client.get_content_decisions('example_user', 'example_content')

sift/client.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,35 @@ def get_content_decisions(self, user_id, content_id, timeout=None):
533533
except requests.exceptions.RequestException as e:
534534
raise ApiException(str(e))
535535

536+
def get_session_decisions(self, user_id, session_id, timeout=None):
537+
"""Gets the decisions for a user's session.
538+
539+
Args:
540+
user_id: The ID of a user.
541+
session_id: The ID of a session.
542+
543+
Returns:
544+
A sift.client.Response object if the call succeeded.
545+
Otherwise, raises an ApiException.
546+
547+
"""
548+
if not isinstance(user_id, self.UNICODE_STRING) or len(user_id.strip()) == 0:
549+
raise ApiException("user_id must be a string")
550+
if not isinstance(session_id, self.UNICODE_STRING) or len(session_id.strip()) == 0:
551+
raise ApiException("session_id must be a string")
552+
553+
if timeout is None:
554+
timeout = self.timeout
555+
556+
try:
557+
return Response(requests.get(
558+
self._session_decisions_url(self.account_id, user_id, session_id),
559+
auth=requests.auth.HTTPBasicAuth(self.api_key, ''),
560+
headers={'User-Agent': self._user_agent()},
561+
timeout=timeout))
562+
563+
except requests.exceptions.RequestException as e:
564+
raise ApiException(str(e))
536565

537566
def apply_session_decision(self, user_id, session_id, properties, timeout=None):
538567
"""Apply decision to session
@@ -638,6 +667,9 @@ def _user_decisions_url(self, account_id, user_id):
638667
def _order_decisions_url(self, account_id, order_id):
639668
return API3_URL + '/v3/accounts/%s/orders/%s/decisions' % (account_id, order_id)
640669

670+
def _session_decisions_url(self, account_id, user_id, session_id):
671+
return API3_URL + '/v3/accounts/%s/users/%s/sessions/%s/decisions' % (account_id, user_id, session_id)
672+
641673
def _content_decisions_url(self, account_id, user_id, content_id):
642674
return API3_URL + '/v3/accounts/%s/users/%s/content/%s/decisions' % (account_id, user_id, content_id)
643675

sift/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
VERSION = '4.0.1'
1+
VERSION = '4.1.0'
22
API_VERSION = '205'

tests/test_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,12 @@ def test_apply_decision_to_session_fails_with_no_session_id(self):
452452
except Exception as e:
453453
assert(isinstance(e, sift.client.ApiException))
454454

455+
def test_get_session_decisions_fails_with_no_session_id(self):
456+
try:
457+
self.sift_client.get_session_decisions("user_id", None)
458+
except Exception as e:
459+
assert(isinstance(e, sift.client.ApiException))
460+
455461
def test_apply_decision_to_content_fails_with_no_content_id(self):
456462
try:
457463
self.sift_client.apply_content_decision("user_id", None, {})
@@ -937,6 +943,25 @@ def test_get_order_decisions(self):
937943
assert(response.body['decisions']['payment_abuse']['decision']['id'] == 'decision7')
938944
assert(response.body['decisions']['promotion_abuse']['decision']['id'] == 'good_order')
939945

946+
def test_get_session_decisions(self):
947+
mock_response = mock.Mock()
948+
mock_response.content = '{"decisions":{"account_takeover": {"decision": {"id": "session_decision"},"time": 1461963839151,"webhook_succeeded": true}}}'
949+
mock_response.json.return_value = json.loads(mock_response.content)
950+
mock_response.status_code = 200
951+
mock_response.headers = response_with_data_header()
952+
953+
with mock.patch('requests.get') as mock_get:
954+
mock_get.return_value = mock_response
955+
956+
response = self.sift_client.get_session_decisions('example_user','example_session')
957+
mock_get.assert_called_with(
958+
'https://api3.siftscience.com/v3/accounts/ACCT/users/example_user/sessions/example_session/decisions',
959+
headers=mock.ANY, auth=mock.ANY, timeout=mock.ANY)
960+
961+
assert(isinstance(response, sift.client.Response))
962+
assert(response.is_ok())
963+
assert(response.body['decisions']['account_takeover']['decision']['id'] == 'session_decision')
964+
940965
def test_get_content_decisions(self):
941966
mock_response = mock.Mock()
942967
mock_response.content = '{"decisions":{"content_abuse":{"decision":{"id":"content_looks_bad_content_abuse"},"time":1468517407135,"webhook_succeeded":true}}}'

0 commit comments

Comments
 (0)