Skip to content

Commit a2ba885

Browse files
authored
Merge pull request #114 from SiftScience/API-7827
[API-7827]: Stop using url parameters to authenticate Score API requests
2 parents 1cb9ac8 + 6100337 commit a2ba885

File tree

3 files changed

+58
-36
lines changed

3 files changed

+58
-36
lines changed

sift/client.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def score(self, user_id, timeout=None, abuse_types=None, version=None, include_s
234234
version = self.version
235235

236236
headers = {'User-Agent': self._user_agent()}
237-
params = {'api_key': self.api_key}
237+
params = {}
238238
if abuse_types:
239239
params['abuse_types'] = ','.join(abuse_types)
240240

@@ -248,7 +248,8 @@ def score(self, user_id, timeout=None, abuse_types=None, version=None, include_s
248248
url,
249249
headers=headers,
250250
timeout=timeout,
251-
params=params)
251+
params=params,
252+
auth=requests.auth.HTTPBasicAuth(self.api_key, ''))
252253
return Response(response)
253254
except requests.exceptions.RequestException as e:
254255
raise ApiException(str(e), url)
@@ -284,7 +285,7 @@ def get_user_score(self, user_id, timeout=None, abuse_types=None, include_score_
284285

285286
url = self._user_score_url(user_id, self.version)
286287
headers = {'User-Agent': self._user_agent()}
287-
params = {'api_key': self.api_key}
288+
params = {}
288289
if abuse_types:
289290
params['abuse_types'] = ','.join(abuse_types)
290291

@@ -296,7 +297,8 @@ def get_user_score(self, user_id, timeout=None, abuse_types=None, include_score_
296297
url,
297298
headers=headers,
298299
timeout=timeout,
299-
params=params)
300+
params=params,
301+
auth=requests.auth.HTTPBasicAuth(self.api_key, ''))
300302
return Response(response)
301303
except requests.exceptions.RequestException as e:
302304
raise ApiException(str(e), url)
@@ -326,7 +328,7 @@ def rescore_user(self, user_id, timeout=None, abuse_types=None):
326328

327329
url = self._user_score_url(user_id, self.version)
328330
headers = {'User-Agent': self._user_agent()}
329-
params = {'api_key': self.api_key}
331+
params = {}
330332
if abuse_types:
331333
params['abuse_types'] = ','.join(abuse_types)
332334

@@ -335,7 +337,8 @@ def rescore_user(self, user_id, timeout=None, abuse_types=None):
335337
url,
336338
headers=headers,
337339
timeout=timeout,
338-
params=params)
340+
params=params,
341+
auth=requests.auth.HTTPBasicAuth(self.api_key, ''))
339342
return Response(response)
340343
except requests.exceptions.RequestException as e:
341344
raise ApiException(str(e), url)
@@ -401,7 +404,7 @@ def unlabel(self, user_id, timeout=None, abuse_type=None, version=None):
401404

402405
url = self._label_url(user_id, version)
403406
headers = {'User-Agent': self._user_agent()}
404-
params = {'api_key': self.api_key}
407+
params = {}
405408
if abuse_type:
406409
params['abuse_type'] = abuse_type
407410

@@ -410,7 +413,8 @@ def unlabel(self, user_id, timeout=None, abuse_type=None, version=None):
410413
url,
411414
headers=headers,
412415
timeout=timeout,
413-
params=params)
416+
params=params,
417+
auth=requests.auth.HTTPBasicAuth(self.api_key, ''))
414418
return Response(response)
415419

416420
except requests.exceptions.RequestException as e:

tests/test_client.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import unittest
55
import warnings
66
from decimal import Decimal
7+
from requests.auth import HTTPBasicAuth
78

89
import mock
910
import requests.exceptions
@@ -342,9 +343,10 @@ def test_score_ok(self):
342343
response = self.sift_client.score('12345')
343344
mock_get.assert_called_with(
344345
'https://api.siftscience.com/v205/score/12345',
345-
params={'api_key': self.test_key},
346+
params={},
346347
headers=mock.ANY,
347-
timeout=mock.ANY)
348+
timeout=mock.ANY,
349+
auth=HTTPBasicAuth(self.test_key, ''))
348350
self.assertIsInstance(response, sift.client.Response)
349351
assert (response.is_ok())
350352
assert (response.api_error_message == "OK")
@@ -364,9 +366,10 @@ def test_score_with_timeout_param_ok(self):
364366
response = self.sift_client.score('12345', test_timeout)
365367
mock_get.assert_called_with(
366368
'https://api.siftscience.com/v205/score/12345',
367-
params={'api_key': self.test_key},
369+
params={},
368370
headers=mock.ANY,
369-
timeout=test_timeout)
371+
timeout=test_timeout,
372+
auth=HTTPBasicAuth(self.test_key, ''))
370373
self.assertIsInstance(response, sift.client.Response)
371374
assert (response.is_ok())
372375
assert (response.api_error_message == "OK")
@@ -388,9 +391,10 @@ def test_get_user_score_ok(self):
388391
response = self.sift_client.get_user_score('12345', test_timeout)
389392
mock_get.assert_called_with(
390393
'https://api.siftscience.com/v205/users/12345/score',
391-
params={'api_key': self.test_key},
394+
params={},
392395
headers=mock.ANY,
393-
timeout=test_timeout)
396+
timeout=test_timeout,
397+
auth=HTTPBasicAuth(self.test_key, ''))
394398
self.assertIsInstance(response, sift.client.Response)
395399
assert (response.is_ok())
396400
assert (response.api_error_message == "OK")
@@ -415,9 +419,10 @@ def test_get_user_score_with_abuse_types_ok(self):
415419
timeout=test_timeout)
416420
mock_get.assert_called_with(
417421
'https://api.siftscience.com/v205/users/12345/score',
418-
params={'api_key': self.test_key, 'abuse_types': 'payment_abuse,content_abuse'},
422+
params={'abuse_types': 'payment_abuse,content_abuse'},
419423
headers=mock.ANY,
420-
timeout=test_timeout)
424+
timeout=test_timeout,
425+
auth=HTTPBasicAuth(self.test_key, ''))
421426
self.assertIsInstance(response, sift.client.Response)
422427
assert (response.is_ok())
423428
assert (response.api_error_message == "OK")
@@ -440,9 +445,10 @@ def test_rescore_user_ok(self):
440445
response = self.sift_client.rescore_user('12345', test_timeout)
441446
mock_post.assert_called_with(
442447
'https://api.siftscience.com/v205/users/12345/score',
443-
params={'api_key': self.test_key},
448+
params={},
444449
headers=mock.ANY,
445-
timeout=test_timeout)
450+
timeout=test_timeout,
451+
auth=HTTPBasicAuth(self.test_key, ''))
446452
self.assertIsInstance(response, sift.client.Response)
447453
assert (response.is_ok())
448454
assert (response.api_error_message == "OK")
@@ -467,9 +473,10 @@ def test_rescore_user_with_abuse_types_ok(self):
467473
timeout=test_timeout)
468474
mock_post.assert_called_with(
469475
'https://api.siftscience.com/v205/users/12345/score',
470-
params={'api_key': self.test_key, 'abuse_types': 'payment_abuse,content_abuse'},
476+
params={'abuse_types': 'payment_abuse,content_abuse'},
471477
headers=mock.ANY,
472-
timeout=test_timeout)
478+
timeout=test_timeout,
479+
auth=HTTPBasicAuth(self.test_key, ''))
473480
self.assertIsInstance(response, sift.client.Response)
474481
assert (response.is_ok())
475482
assert (response.api_error_message == "OK")
@@ -968,7 +975,8 @@ def test_unlabel_user_ok(self):
968975
'https://api.siftscience.com/v205/users/%s/labels' % user_id,
969976
headers=mock.ANY,
970977
timeout=mock.ANY,
971-
params={'api_key': self.test_key, 'abuse_type': 'account_abuse'})
978+
params={'abuse_type': 'account_abuse'},
979+
auth=HTTPBasicAuth(self.test_key, ''))
972980
self.assertIsInstance(response, sift.client.Response)
973981
assert (response.is_ok())
974982

@@ -1008,7 +1016,8 @@ def test_unlabel_user_with_special_chars_ok(self):
10081016
'https://api.siftscience.com/v205/users/%s/labels' % urllib.parse.quote(user_id),
10091017
headers=mock.ANY,
10101018
timeout=mock.ANY,
1011-
params={'api_key': self.test_key})
1019+
params={},
1020+
auth=HTTPBasicAuth(self.test_key, ''))
10121021
self.assertIsInstance(response, sift.client.Response)
10131022
assert (response.is_ok())
10141023

@@ -1055,9 +1064,10 @@ def test_score__with_special_user_id_chars_ok(self):
10551064
response = self.sift_client.score(user_id, abuse_types=['legacy'])
10561065
mock_get.assert_called_with(
10571066
'https://api.siftscience.com/v205/score/%s' % urllib.parse.quote(user_id),
1058-
params={'api_key': self.test_key, 'abuse_types': 'legacy'},
1067+
params={'abuse_types': 'legacy'},
10591068
headers=mock.ANY,
1060-
timeout=mock.ANY)
1069+
timeout=mock.ANY,
1070+
auth=HTTPBasicAuth(self.test_key, ''))
10611071
self.assertIsInstance(response, sift.client.Response)
10621072
assert (response.is_ok())
10631073
assert (response.api_error_message == "OK")
@@ -1464,9 +1474,10 @@ def test_score_api_include_score_percentiles_ok(self):
14641474
response = self.sift_client.score(user_id='12345', include_score_percentiles=True)
14651475
mock_get.assert_called_with(
14661476
'https://api.siftscience.com/v205/score/12345',
1467-
params={'api_key': self.test_key, 'fields': 'SCORE_PERCENTILES'},
1477+
params={'fields': 'SCORE_PERCENTILES'},
14681478
headers=mock.ANY,
1469-
timeout=mock.ANY)
1479+
timeout=mock.ANY,
1480+
auth=HTTPBasicAuth(self.test_key, ''))
14701481
self.assertIsInstance(response, sift.client.Response)
14711482
assert (response.is_ok())
14721483
assert (response.api_error_message == "OK")
@@ -1488,9 +1499,10 @@ def test_get_user_score_include_score_percentiles_ok(self):
14881499
response = self.sift_client.get_user_score(user_id='12345', timeout=test_timeout, include_score_percentiles=True)
14891500
mock_get.assert_called_with(
14901501
'https://api.siftscience.com/v205/users/12345/score',
1491-
params={'api_key': self.test_key, 'fields': 'SCORE_PERCENTILES'},
1502+
params={'fields': 'SCORE_PERCENTILES'},
14921503
headers=mock.ANY,
1493-
timeout=test_timeout)
1504+
timeout=test_timeout,
1505+
auth=HTTPBasicAuth(self.test_key, ''))
14941506
self.assertIsInstance(response, sift.client.Response)
14951507
assert (response.is_ok())
14961508
assert (response.api_error_message == "OK")

tests/test_client_v203.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import unittest
88
import sys
99
import requests.exceptions
10+
from requests.auth import HTTPBasicAuth
1011
if sys.version_info[0] < 3:
1112
import six.moves.urllib as urllib
1213
else:
@@ -166,9 +167,10 @@ def test_score_ok(self):
166167
response = self.sift_client_v204.score('12345', version='203')
167168
mock_get.assert_called_with(
168169
'https://api.siftscience.com/v203/score/12345',
169-
params={'api_key': self.test_key},
170+
params={},
170171
headers=mock.ANY,
171-
timeout=mock.ANY)
172+
timeout=mock.ANY,
173+
auth=HTTPBasicAuth(self.test_key, ''))
172174
self.assertIsInstance(response, sift.client.Response)
173175
assert(response.is_ok())
174176
assert(response.api_error_message == "OK")
@@ -186,9 +188,10 @@ def test_score_with_timeout_param_ok(self):
186188
response = self.sift_client.score('12345', test_timeout)
187189
mock_get.assert_called_with(
188190
'https://api.siftscience.com/v203/score/12345',
189-
params={'api_key': self.test_key},
191+
params={},
190192
headers=mock.ANY,
191-
timeout=test_timeout)
193+
timeout=test_timeout,
194+
auth=HTTPBasicAuth(self.test_key, ''))
192195
self.assertIsInstance(response, sift.client.Response)
193196
assert(response.is_ok())
194197
assert(response.api_error_message == "OK")
@@ -285,7 +288,8 @@ def test_unlabel_user_ok(self):
285288
'https://api.siftscience.com/v203/users/%s/labels' % user_id,
286289
headers=mock.ANY,
287290
timeout=mock.ANY,
288-
params={'api_key': self.test_key})
291+
params={},
292+
auth=HTTPBasicAuth(self.test_key, ''))
289293
self.assertIsInstance(response, sift.client.Response)
290294
assert(response.is_ok())
291295

@@ -326,7 +330,8 @@ def test_unlabel_user_with_special_chars_ok(self):
326330
'https://api.siftscience.com/v203/users/%s/labels' % urllib.parse.quote(user_id),
327331
headers=mock.ANY,
328332
timeout=mock.ANY,
329-
params={'api_key': self.test_key})
333+
params={},
334+
auth=HTTPBasicAuth(self.test_key, ''))
330335
self.assertIsInstance(response, sift.client.Response)
331336
assert(response.is_ok())
332337

@@ -373,9 +378,10 @@ def test_score__with_special_user_id_chars_ok(self):
373378
response = self.sift_client.score(user_id)
374379
mock_get.assert_called_with(
375380
'https://api.siftscience.com/v203/score/%s' % urllib.parse.quote(user_id),
376-
params={'api_key': self.test_key},
381+
params={},
377382
headers=mock.ANY,
378-
timeout=mock.ANY)
383+
timeout=mock.ANY,
384+
auth=HTTPBasicAuth(self.test_key, ''))
379385
self.assertIsInstance(response, sift.client.Response)
380386
assert(response.is_ok())
381387
assert(response.api_error_message == "OK")

0 commit comments

Comments
 (0)