<commit_before>Basic tests on the API, authentication, etc.
============================================

    >>> from assistly import AssistlyAPI
    >>> from assistly.models import User, Case, Topic, Interaction, Customer, CustomerEmail

Cache engine class
------------------

    >>> import os
    >>> import hashlib
    >>> import urllib
    >>> class CacheEngine(object):
    ...     cache_temp_dir = '/tmp/assistly-cache/'
    ...     enabled = True
    ... 
    ...     def make_file_path(self, key):
    ...         if not os.path.exists(self.cache_temp_dir):
    ...             os.makedirs(self.cache_temp_dir)
    ...         return os.path.join(self.cache_temp_dir, key)
    ... 
    ...     def make_key(self, method, url, query_params, post_params):
    ...         query_params = urllib.urlencode(query_params) if query_params else ''
    ...         post_params = urllib.urlencode(post_params) if post_params else ''
    ...         key = hashlib.sha1('.'.join([method, url, query_params, post_params])).hexdigest()
    ...         return key
    ...     
    ...     def get(self, key):
    ...         path = self.make_file_path(key)
    ...         if self.enabled and os.path.exists(path):
    ...             fp = file(path)
    ...             data = fp.read()
    ...             fp.close()
    ...             return data
    ...     
    ...     def set(self, key, data):
    ...         path = self.make_file_path(key)
    ...         if self.enabled:
    ...             fp = file(path, 'w')
    ...             fp.write(data)
    ...             fp.close()
    >>> cache_engine = CacheEngine()

Basic stuff
-----------

    >>> import simplejson
    >>> fp = file(os.path.join(os.path.dirname(__file__), 'keys.json'))
    >>> json = simplejson.loads(fp.read())
    >>> fp.close()

Starting on the API

    >>> api = AssistlyAPI(cache_engine=cache_engine, base_url='marinho', key=json['key'],
    ...         secret=json['secret'])
    >>> api.base_url
    'https://marinho.assistly.com/api/v1/'

Setting a token manually

    >>> api.set_token(token_key=json['token_key'], token_secret=json['token_secret'])
    >>> api.token_key == json['token_key']
    True

    >>> api.token_secret == json['token_secret']
    True

Requesting an access token # TODO

    >>> #api.request_token()
    >>> #api.token_key
    >>> #api.token_secret

Verifying credentials

    >>> isinstance(api.verify_credentials().user, User)
    True

Users
-----

Getting users

    >>> users = api.users(count=5)
    >>> for user in users[:1]:
    ...     isinstance(user, User)
    True

Topics
------

Getting topics

    >>> topics = api.topics(count=5)
    >>> for topic in topics[:1]:
    ...     isinstance(topic, Topic)
    True

Interactions
------------

Creating an interaction

    >>> response = api.interaction_create(subject='New interaction from the API', customer_email='<EMAIL>')
    >>> response.success
    True
    >>> isinstance(response.customer, Customer)
    True
    >>> isinstance(response.case, Case)
    True
    >>> isinstance(response.interaction, Interaction)
    True

Getting interactions

    >>> interactions = api.interactions(case_id=response.interaction.case_id, count=1)
    >>> for interaction in interactions[:1]:
    ...     interaction.id == response.interaction.id
    True

Cases
-----

Getting cases

    >>> cases = api.cases(count=5)
    >>> for case in cases[:1]:
    ...     isinstance(case, Case)
    True

Case show

    >>> case = api.case_show(response.interaction.case_id)
    >>> case.id == response.interaction.case_id
    True
    >>> case.case_status_type
    'new'

Case update

    >>> resp = api.case_update(response.interaction.case_id, case_status_type='open', subject='New subject for this.')
    >>> case = api.case_show(response.interaction.case_id)
    >>> case.case_status_type
    'open'

Customers
---------

Creating a customer

    >>> import datetime
    >>> email = datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S') + '@raminel.com.br'
    >>> response = api.customer_create(name='<NAME>', company='Raminel Sites', email=email)
    >>> response.success
    True
    >>> isinstance(response.customer, Customer)
    True

Getting customers

    >>> customers = api.customers(count=1)
    >>> for customer in customers[:1]:
    ...     isinstance(customer, Customer)
    True

Customer show

    >>> customer = api.customer_show(response.customer.id)
    >>> isinstance(customer, Customer)
    True

Customer update

    >>> response = api.customer_update(response.customer.id, name='<NAME>')
    >>> customer = api.customer_show(response.customer.id)
    >>> print customer.first_name, customer.last_name
    <NAME>

Customer email create

    >>> email = datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S') + '@raminel2.com.br'
    >>> response = api.customer_email_create(response.customer.id, email=email)
    >>> response.success
    True
    >>> isinstance(response.email, CustomerEmail)
    True
    >>> response.email.email == email
    True

Customer email update

    >>> customer = response.customer
    >>> new_email = datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S') + '@raminel3.com.br'
    >>> response = api.customer_email_update(customer.id, response.email.id, new_email)
    >>> isinstance(response.email, CustomerEmail)
    True
    >>> response.email.email == new_email
    True
<commit_msg>Response to customer_email_create don't include the customer ID<commit_after>Basic tests on the API, authentication, etc.
============================================

    >>> from assistly import AssistlyAPI
    >>> from assistly.models import User, Case, Topic, Interaction, Customer, CustomerEmail

Cache engine class
------------------

    >>> import os
    >>> import hashlib
    >>> import urllib
    >>> class CacheEngine(object):
    ...     cache_temp_dir = '/tmp/assistly-cache/'
    ...     enabled = True
    ... 
    ...     def make_file_path(self, key):
    ...         if not os.path.exists(self.cache_temp_dir):
    ...             os.makedirs(self.cache_temp_dir)
    ...         return os.path.join(self.cache_temp_dir, key)
    ... 
    ...     def make_key(self, method, url, query_params, post_params):
    ...         query_params = urllib.urlencode(query_params) if query_params else ''
    ...         post_params = urllib.urlencode(post_params) if post_params else ''
    ...         key = hashlib.sha1('.'.join([method, url, query_params, post_params])).hexdigest()
    ...         return key
    ...     
    ...     def get(self, key):
    ...         path = self.make_file_path(key)
    ...         if self.enabled and os.path.exists(path):
    ...             fp = file(path)
    ...             data = fp.read()
    ...             fp.close()
    ...             return data
    ...     
    ...     def set(self, key, data):
    ...         path = self.make_file_path(key)
    ...         if self.enabled:
    ...             fp = file(path, 'w')
    ...             fp.write(data)
    ...             fp.close()
    >>> cache_engine = CacheEngine()

Basic stuff
-----------

    >>> import simplejson
    >>> fp = file(os.path.join(os.path.dirname(__file__), 'keys.json'))
    >>> json = simplejson.loads(fp.read())
    >>> fp.close()

Starting on the API

    >>> api = AssistlyAPI(cache_engine=cache_engine, base_url='marinho', key=json['key'],
    ...         secret=json['secret'])
    >>> api.base_url
    'https://marinho.assistly.com/api/v1/'

Setting a token manually

    >>> api.set_token(token_key=json['token_key'], token_secret=json['token_secret'])
    >>> api.token_key == json['token_key']
    True

    >>> api.token_secret == json['token_secret']
    True

Requesting an access token # TODO

    >>> #api.request_token()
    >>> #api.token_key
    >>> #api.token_secret

Verifying credentials

    >>> isinstance(api.verify_credentials().user, User)
    True

Users
-----

Getting users

    >>> users = api.users(count=5)
    >>> for user in users[:1]:
    ...     isinstance(user, User)
    True

Topics
------

Getting topics

    >>> topics = api.topics(count=5)
    >>> for topic in topics[:1]:
    ...     isinstance(topic, Topic)
    True

Interactions
------------

Creating an interaction

    >>> response = api.interaction_create(subject='New interaction from the API', customer_email='<EMAIL>')
    >>> response.success
    True
    >>> isinstance(response.customer, Customer)
    True
    >>> isinstance(response.case, Case)
    True
    >>> isinstance(response.interaction, Interaction)
    True

Getting interactions

    >>> interactions = api.interactions(case_id=response.interaction.case_id, count=1)
    >>> for interaction in interactions[:1]:
    ...     interaction.id == response.interaction.id
    True

Cases
-----

Getting cases

    >>> cases = api.cases(count=5)
    >>> for case in cases[:1]:
    ...     isinstance(case, Case)
    True

Case show

    >>> case = api.case_show(response.interaction.case_id)
    >>> case.id == response.interaction.case_id
    True
    >>> case.case_status_type
    'new'

Case update

    >>> resp = api.case_update(response.interaction.case_id, case_status_type='open', subject='New subject for this.')
    >>> case = api.case_show(response.interaction.case_id)
    >>> case.case_status_type
    'open'

Customers
---------

Creating a customer

    >>> import datetime
    >>> email = datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S') + '@ram<EMAIL>'
    >>> response = api.customer_create(name='<NAME>', company='Raminel Sites', email=email)
    >>> response.success
    True
    >>> isinstance(response.customer, Customer)
    True

Getting customers

    >>> customers = api.customers(count=1)
    >>> for customer in customers[:1]:
    ...     isinstance(customer, Customer)
    True

Customer show

    >>> customer = api.customer_show(response.customer.id)
    >>> isinstance(customer, Customer)
    True

Customer update

    >>> response = api.customer_update(response.customer.id, name='<NAME>')
    >>> customer = api.customer_show(response.customer.id)
    >>> print customer.first_name, customer.last_name
    <NAME>

Customer email create

    >>> email = datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S') + '@raminel2.com.br'
    >>> response = api.customer_email_create(response.customer.id, email=email)
    >>> response.success
    True
    >>> isinstance(response.email, CustomerEmail)
    True
    >>> response.email.email == email
    True

Customer email update

    >>> new_email = datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S') + '@raminel3.com.br'
    >>> response = api.customer_email_update(customer.id, response.email.id, new_email)
    >>> isinstance(response.email, CustomerEmail)
    True
    >>> response.email.email == new_email
    True

{'fraction_non_alphanumeric': 0.16983405951608418, 'fraction_numerical': 0.0025957170668397143, 'mean_word_length': 2.510575984380085, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 375, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2440791', 'n_tokens_mistral': 3305, 'n_tokens_neox': 3234, 'n_words': 645}