"""Python client for Sift Science's API.
See: https://siftscience.com/docs/references/events-api
"""

import json
import logging
import requests
import traceback

from sift import version

API_URL = 'https://api.siftscience.com/v%s/events' % version.API_VERSION
sift_logger = logging.getLogger('sift_client')

class Client(object):
    def __init__(self, api_key, api_url=API_URL, timeout=2.0):
        """Initialize the client.

        Args:
            api_key: Your Sift Science API key associated with your customer
                account. You can obtain this from
                https://siftscience.com/quickstart
            api_url: The URL to send events to.
            timeout: Number of seconds to wait before failing request. Defaults
                to 2 seconds.
        """
        self.api_key = api_key
        self.url = api_url
        self.timeout = timeout

    def user_agent(self):
        return 'SiftScience/v%s sift-python/%s' % (version.API_VERSION, version.VERSION)

    def track(self, event, properties, return_score=False):
        """Track an event and associated properties to the Sift Science client.
        This call is blocking.

        Args:
            event: The name of the event to send. This can either be a reserved
                event name such as "$transaction" or "$create_order" or a custom event
                name (that does not start with a $).
            properties: A dict of additional event-specific attributes to track
            return_score: Whether the API response should include a score for this 
                 user (the score will be calculated using this event)
        Returns:
            A requests.Response object if the track call succeeded, otherwise
            a subclass of requests.exceptions.RequestException indicating the
            exception that occurred.
        """
        headers = { 'Content-type' : 'application/json',
                    'Accept' : '*/*',
                    'User-Agent' : self.user_agent() }

        properties.update({ '$api_key': self.api_key, '$type': event })
        if return_score:
          params = { 'return_score' : return_score }
        try:
            response = requests.post(self.url, data=json.dumps(properties),
                    headers=headers, timeout=self.timeout, params=params)
            # TODO(david): Wrap the response object in a class
            return response
        except requests.exceptions.RequestException as e:
            sift_logger.warn('Failed to track event: %s' % properties)
            sift_logger.warn(traceback.format_exception_only(type(e), e))

            return e

@@ -224,42 +224,9 @@
 .com
-/v%25s/events' %25 version.API_VERSION
+'
 %0Asif
@@ -809,16 +809,47 @@
  api_url
+ + '/v%25s' %25 version.API_VERSION
 %0A       
@@ -989,16 +989,168 @@
 RSION)%0A%0A
+    def event_url(self):%0A        return self.url + '/events'%0A%0A    def label_url(self, user_id):%0A        return self.url + '/users/%25s/labels' %25 user_id%0A%0A
     def 
@@ -1179,16 +1179,27 @@
 perties,
+ path=None,
  return_
@@ -2165,16 +2165,75 @@
 nt() %7D%0A%0A
+        if path is None:%0A          path = self.event_url()%0A
         
@@ -2424,24 +2424,20 @@
 ts.post(
-self.url
+path
 , data=j
@@ -2828,16 +2828,16 @@
 ), e))%0A%0A
-
         
@@ -2849,8 +2849,793 @@
 eturn e%0A
+%0A    def label(self, user_id, properties):%0A        %22%22%22Labels a user as either good or bad through the Sift Science API.%0A        This call is blocking.%0A%0A        Args:%0A            user_id:  A user's id. This id should be the same as the user_id used in%0A                event calls.%0A            properties: A dict of additional event-specific attributes to track%0A        Returns:%0A            A requests.Response object if the label call succeeded, otherwise%0A            a subclass of requests.exceptions.RequestException indicating the%0A            exception that occurred.%0A        %22%22%22%0A        if not isinstance(user_id, str) or len(user_id.strip()) == 0:%0A            raise RuntimeError(%22user_id must be a string%22)%0A%0A        return self.track('$label', properties, self.label_url(user_id))%0A

Add label API call.