Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,16 +393,19 @@ Note that models generated from webhook notifications might differ slightly from
### Errors
You do not need to deal with the HTTP response from an API call directly. If there is an unsuccessful response then an error that is a subclass of `intercom.Error` will be raised. If desired, you can get at the http_code of an `Error` via it's `http_code` method.

The list of different error subclasses are listed below. As they all inherit off `Error` you can choose to except `Error` or the more specific error subclass:
The list of different error subclasses are listed below. As they all inherit off `IntercomError` you can choose to except `IntercomError` or the more specific error subclass:

```python
AuthenticationError
ServerError
ServiceUnavailableError
ResourceNotFound
BadGatewayError
BadRequestError
RateLimitExceeded
AttributeNotSetError # Raised when you try to call a getter that does not exist on an object
MultipleMatchingUsersError
HttpError
UnexpectedError
```


Expand Down
4 changes: 4 additions & 0 deletions intercom/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
class Company(Resource, Delete, Count, Find, All, Save, Load, Users):
update_verb = 'post'
identity_vars = ['id', 'company_id']

@property
def flat_store_attributes(self):
return ['custom_attributes']
16 changes: 8 additions & 8 deletions intercom/errors.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
# -*- coding: utf-8 -*-


class ArgumentError(ValueError):
pass


class HttpError(Exception):
pass


class IntercomError(Exception):

def __init__(self, message=None, context=None):
Expand All @@ -17,6 +9,14 @@ def __init__(self, message=None, context=None):
self.context = context


class ArgumentError(ValueError, IntercomError):
pass


class HttpError(IntercomError):
pass


class ResourceNotFound(IntercomError):
pass

Expand Down
25 changes: 17 additions & 8 deletions intercom/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@ def untag_companies(cls, name, companies):
return cls._tag_collection(name, 'companies', companies, untag=True)

@classmethod
def find_all_for_user(cls, id=None, email=None, user_id=None):
params = {}
if id:
params['id'] = id
if email:
params['email'] = email
if user_id:
params['user_id'] = user_id
def find_all_for_user(cls, **kwargs):
return cls.find_all_for('user', **kwargs)

@classmethod
def find_all_for_company(cls, **kwargs):
return cls.find_all_for('company', **kwargs)

@classmethod
def find_all_for(cls, taggable_type, **kwargs):
params = {
'taggable_type': taggable_type
}
res_id = kwargs.pop('id', None)
if res_id:
params['taggable_id'] = res_id
params.update(kwargs)

return Tag.find_all(**params)
5 changes: 5 additions & 0 deletions tests/integration/test_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def test_add_user_custom_attributes(self):
self.assertEqual(len(user.companies), 2)
self.assertEqual(user.companies[0].company_id, "9")

# check the custom attributes
company = Company.find(company_id=6)
self.assertEqual(
company.custom_attributes['referral_source'], "Google")

def test_find_by_company_id(self):
# Find a company by company_id
company = Company.find(company_id=self.company.company_id)
Expand Down
71 changes: 47 additions & 24 deletions tests/integration/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import unittest
from intercom import Intercom
from intercom import Tag
from intercom import User
from intercom import Company
from . import delete
from . import get_or_create_company
from . import get_or_create_user
Expand Down Expand Up @@ -32,47 +34,68 @@ def teardown_class(cls):

def test_tag_users(self):
# Tag users
tag = Tag.tag_users("blue", [self.user.id])
self.assertEqual(tag.name, "blue")
tag = Tag.tag_users('blue', [self.user.id])
self.assertEqual(tag.name, 'blue')
user = User.find(email=self.user.email)
self.assertEqual(1, len(user.tags))

def test_untag_users(self):
# Untag users
tag = Tag.untag_users("blue", [self.user.id])
self.assertEqual(tag.name, "blue")
tag = Tag.untag_users('blue', [self.user.id])
self.assertEqual(tag.name, 'blue')
user = User.find(email=self.user.email)
self.assertEqual(0, len(user.tags))

def test_all(self):
# Iterate over all tags
for tag in Tag.all():
self.assertIsNotNone(tag.id)

# def test_all_for_user_by_id(self):
# # Iterate over all tags for user
# tags = Tag.find_all_for_user(id=self.user.id)
# for tag in tags:
# self.assertIsNotNone(tag.id)
def test_all_for_user_by_id(self):
# Iterate over all tags for user
tags = Tag.find_all_for_user(id=self.user.id)
for tag in tags:
self.assertIsNotNone(tag.id)

# def test_all_for_user_by_email(self):
# # Iterate over all tags for user
# tags = Tag.find_all_for_user(email=self.user.email)
# for tag in tags:
# self.assertIsNotNone(tag.id)
def test_all_for_user_by_email(self):
# Iterate over all tags for user
tags = Tag.find_all_for_user(email=self.user.email)
for tag in tags:
self.assertIsNotNone(tag.id)

# def test_all_for_user_by_user_id(self):
# # Iterate over all tags for user
# tags = Tag.find_all_for_user(user_id=self.user.user_id)
# for tag in tags:
# self.assertIsNotNone(tag.id)
def test_all_for_user_by_user_id(self):
# Iterate over all tags for user
tags = Tag.find_all_for_user(user_id=self.user.user_id)
for tag in tags:
self.assertIsNotNone(tag.id)

def test_tag_companies(self):
# Tag companies
tag = Tag.tag_companies("red", [self.user.companies[0].id])
self.assertEqual(tag.name, "red")
company = Company.find(id=self.user.companies[0].id)
self.assertEqual(1, len(company.tags))

def test_untag_companies(self):
# Untag companies
tag = Tag.untag_companies("blue", [self.user.companies[0].id])
self.assertEqual(tag.name, "blue")
tag = Tag.untag_companies("red", [self.user.companies[0].id])
self.assertEqual(tag.name, "red")
company = Company.find(id=self.user.companies[0].id)
self.assertEqual(0, len(company.tags))

# Iterate over all tags for company
def test_all_for_company_by_id(self):
# Iterate over all tags for user
red_tag = Tag.tag_companies("red", [self.company.id])
tags = Tag.find_all_for_company(id=self.company.id)
for tag in tags:
self.assertEqual(red_tag.id, tag.id)
Tag.untag_companies("red", [self.company.id])

# # Iterate over all tags for company
# Tag.find_all_for_company(id='43357e2c3c77661e25000026')
# Tag.find_all_for_company(company_id='6')
def test_all_for_company_by_company_id(self):
# Iterate over all tags for user
red_tag = Tag.tag_companies("red", [self.company.id])
tags = Tag.find_all_for_company(company_id=self.company.id)
for tag in tags:
self.assertEqual(red_tag.id, tag.id)
Tag.untag_companies("red", [self.company.id])