11# Sift Python Bindings
22
33Bindings for Sift's APIs -- including the
4- [ Events] ( https://sift.com/resources/references /events-api.html ) ,
5- [ Labels] ( https://sift.com/resources/references /labels-api.html ) ,
4+ [ Events] (https://developers. sift.com/docs/python /events-api/ ,
5+ [ Labels] ( https://developers. sift.com/docs/python /labels-api/ ) ,
66and
7- [ Score] ( https://sift.com/resources/references /score-api.html )
7+ [ Score] ( https://developers. sift.com/docs/python /score-api/ )
88APIs.
99
10-
1110## Installation
1211
13- Set up a virtual environment with virtualenv (otherwise you will need
14- to make the pip calls as sudo):
15-
16- virtualenv venv
17- source venv/bin/activate
18-
19- Get the latest released package from pip:
20-
21- Python 2:
22-
23- pip install Sift
24-
25- Python 3:
26-
27- pip3 install Sift
28-
29- or install newest source directly from GitHub:
30-
31- Python 2:
32-
33- pip install git+https://github.com/SiftScience/sift-python
34-
35- Python 3:
36-
37- pip3 install git+https://github.com/SiftScience/sift-python
38-
12+ ``` sh
13+ # install from PyPi
14+ pip install Sift
15+ ```
3916
4017## Documentation
4118
42- Please see [ here] ( https://sift.com/developers/ docs/python/events-api/ overview ) for the
19+ Please see [ here] ( https://developers. sift.com/docs/python/apis- overview ) for the
4320most up-to-date documentation.
4421
4522## Changelog
@@ -48,19 +25,13 @@ Please see
4825[ the CHANGELOG] ( https://github.com/SiftScience/sift-python/blob/master/CHANGES.md )
4926for a history of all changes.
5027
51- Note, that in v2.0.0, the API semantics were changed to raise an
52- exception in the case of error to be more pythonic. Client code will
53- need to be updated to catch ` sift.client.ApiException ` exceptions.
54-
55-
5628## Usage
5729
5830Here's an example:
5931
6032``` python
6133
62- import json
63- import sift.client
34+ import sift
6435
6536client = sift.Client(api_key = ' <your API key here>' , account_id = ' <your account ID here>' )
6637
@@ -85,12 +56,17 @@ properties = {
8556}
8657
8758try :
88- response = client.track(" $transaction" , properties)
89- if response.is_ok():
90- print " Successfully tracked event"
59+ response = client.track(
60+ " $transaction" ,
61+ properties,
62+ )
9163except sift.client.ApiException:
9264 # request failed
9365 pass
66+ else :
67+ if response.is_ok():
68+ print (" Successfully tracked event" )
69+
9470
9571# Track a transaсtion event and receive a score with percentiles in response (sync flow).
9672# Note: `return_score` or `return_workflow_status` must be set `True`.
@@ -111,15 +87,24 @@ properties = {
11187}
11288
11389try :
114- response = client.track(" $transaction" , properties, return_score = True , include_score_percentiles = True , abuse_types = [" promotion_abuse" , " content_abuse" , " payment_abuse" ])
115- if response.is_ok():
116- score_response = response.body[" score_response" ]
117- print (score_response)
90+ response = client.track(
91+ " $transaction" ,
92+ properties,
93+ return_score = True ,
94+ include_score_percentiles = True ,
95+ abuse_types = (" promotion_abuse" , " content_abuse" , " payment_abuse" ),
96+ )
11897except sift.client.ApiException:
11998 # request failed
12099 pass
100+ else :
101+ if response.is_ok():
102+ score_response = response.body[" score_response" ]
103+ print (score_response)
104+
121105
122- # To include `warnings` field to Events API response via calling `track()` method, set it by the `include_warnings` param:
106+ # In order to include `warnings` field to Events API response via calling
107+ # `track()` method, set it by the `include_warnings` param:
123108try :
124109 response = client.track(" $transaction" , properties, include_warnings = True )
125110 # ...
@@ -130,12 +115,12 @@ except sift.client.ApiException:
130115# Request a score for the user with user_id 23056
131116try :
132117 response = client.score(user_id)
133- s = json.dumps(response.body)
134- print s
135-
136118except sift.client.ApiException:
137119 # request failed
138120 pass
121+ else :
122+ print (response.body)
123+
139124
140125try :
141126 # Label the user with user_id 23056 as Bad with all optional fields
@@ -211,6 +196,7 @@ send_properties = {
211196 }
212197 }
213198}
199+
214200try :
215201 response = client.verification_send(send_properties)
216202except sift.client.ApiException:
@@ -241,35 +227,4 @@ try:
241227except sift.client.ApiException:
242228 # request failed
243229 pass
244-
245- ```
246-
247- ## Testing
248-
249- Before submitting a change, make sure the following commands run without
250- errors from the root dir of the repository:
251-
252- python -m unittest discover
253- python3 -m unittest discover
254-
255- ## Integration testing app
256-
257- For testing the app with real calls it is possible to run the integration testing app,
258- it makes calls to almost all our public endpoints to make sure the library integrates
259- well. At the moment, the app is run on every merge to master
260-
261- #### How to run it locally
262-
263- 1 . Add env variable ` ACCOUNT_ID ` with the valid account id
264- 2 . Add env variable ` API_KEY ` with the valid Api Key associated from the account
265- 3 . Run the following under the project root folder
266- ```
267- # uninstall the lib from the local env (if it was installed)
268- pip uninstall sift
269-
270- # install the lib from the local source code
271- pip install ../sift-python
272-
273- # run the app
274- python test_integration_app/main.py
275230```
0 commit comments