-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add contacts from v2.0 #223
Open
umarovt
wants to merge
9
commits into
intercom:master
Choose a base branch
from
umarovt:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0f33ec8
Add constants from v2.0
umarovt a715628
Finish cursor based pagination fix
umarovt ee17727
Implememnt search over contacts
umarovt 3ed3f3c
Fix issue with passing params to collection proxy
umarovt b5dce26
Fix issue with end statement for old pagination
umarovt 4876349
Fix continue iterating test
umarovt cfe7294
Remove console log
umarovt ccd57c4
Remove console log
umarovt 9b9875a
Drop python 3.4 because of yaml
umarovt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
dist | ||
venv | ||
env | ||
.venv | ||
.coverage | ||
.env | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ sudo: false | |
language: python | ||
python: | ||
- 2.7 | ||
- 3.4 | ||
- 3.5 | ||
- 3.6 | ||
install: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Operation to search through contacts.""" | ||
|
||
from intercom import utils | ||
|
||
|
||
class Search(object): | ||
"""A mixin that provides `search` functionality.""" | ||
|
||
def search(self, query, **params): | ||
"""Find all instances of the resource based on the supplied parameters.""" | ||
collection_name = utils.resource_class_to_collection_name( | ||
self.collection_class) | ||
finder_url = "/{}/scroll".format(collection_name) | ||
|
||
response = self.client.post("/{}/search".format(collection_name), query) | ||
|
||
collection_data = response['data'] | ||
|
||
return map(lambda item: self.collection_class(**item), collection_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from intercom.traits.api_resource import Resource | ||
from intercom.traits.incrementable_attributes import IncrementableAttributes | ||
|
||
|
||
class Contact(Resource, IncrementableAttributes): | ||
|
||
update_verb = 'post' | ||
identity_vars = ['id', 'email', 'role'] | ||
|
||
@property | ||
def flat_store_attributes(self): | ||
return ['custom_attributes'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from intercom import contact | ||
from intercom.api_operations.all import All | ||
from intercom.api_operations.bulk import Submit | ||
from intercom.api_operations.find import Find | ||
from intercom.api_operations.find_all import FindAll | ||
from intercom.api_operations.search import Search | ||
from intercom.api_operations.delete import Delete | ||
from intercom.api_operations.save import Save | ||
from intercom.api_operations.load import Load | ||
from intercom.extended_api_operations.tags import Tags | ||
from intercom.service.base_service import BaseService | ||
|
||
|
||
class Contact(BaseService, All, Find, FindAll, Delete, Save, Load, Submit, Tags, Search): | ||
|
||
@property | ||
def collection_class(self): | ||
return contact.Contact |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,20 @@ def get_timestamp(): | |
now = datetime.utcnow() | ||
return int(time.mktime(now.timetuple())) | ||
|
||
def get_or_create_contact(client, timestamp): | ||
# get user | ||
email = '%[email protected]' % (timestamp) | ||
try: | ||
user = client.contacts.find(email=email) | ||
except ResourceNotFound: | ||
# Create a user | ||
contact = client.contacts.create( | ||
email=email, | ||
user_id=timestamp, | ||
name="Ada %s" % (timestamp)) | ||
time.sleep(5) | ||
return contact | ||
|
||
|
||
def get_or_create_user(client, timestamp): | ||
# get user | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import unittest | ||
from intercom.client import Client | ||
from . import get_timestamp | ||
from . import get_or_create_contact | ||
|
||
intercom = Client( | ||
os.environ.get('INTERCOM_PERSONAL_ACCESS_TOKEN')) | ||
|
||
|
||
class ContactsTest(unittest.TestCase): | ||
|
||
@classmethod | ||
def setup_class(cls): | ||
nowstamp = get_timestamp() | ||
cls.user = get_or_create_contact(intercom, nowstamp) | ||
cls.email = cls.user.email | ||
|
||
@classmethod | ||
def teardown_class(cls): | ||
delete_user(intercom, cls.user) | ||
|
||
def test_find_by_email(self): | ||
# Find user by email | ||
contact = intercom.contacts.find(email=self.email) | ||
self.assertEqual(self.email, contact.email) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,128 @@ def get_user(email="[email protected]", name="Joe Schmoe"): | |
} | ||
} | ||
|
||
def get_contact(email="[email protected]", name="Joe Schmoe"): | ||
return { | ||
"type": "contact", | ||
"id": "id-from-customers-app", | ||
"workspace_id": "ecahpwf5", | ||
"external_id": "the-app-id", | ||
"role": "user", | ||
"email": email, | ||
"phone": "+1123456789", | ||
"name": name, | ||
"avatar": "https://example.org/128Wash.jpg", | ||
"owner_id": 127, | ||
"social_profiles": { | ||
"type": "list", | ||
"data": [ | ||
{ | ||
"type": "social_profile", | ||
"name": "Twitter", | ||
"url": "http://twitter.com/th1sland" | ||
} | ||
] | ||
}, | ||
"has_hard_bounced": False, | ||
"marked_email_as_spam": False, | ||
"unsubscribed_from_emails": False, | ||
"created_at": 1571672154, | ||
"updated_at": 1571672158, | ||
"signed_up_at": 1571069751, | ||
"last_seen_at": 1571069751, | ||
"last_replied_at": 1571672158, | ||
"last_contacted_at": 1571672158, | ||
"last_email_opened_at": 1571673478, | ||
"last_email_clicked_at": 1571676789, | ||
"language_override": None, | ||
"browser": "chrome", | ||
"browser_version": "77.0.3865.90", | ||
"browser_language": "en", | ||
"os": "OS X 10.14.6", | ||
"location": { | ||
"type": "location", | ||
"country": "Ireland", | ||
"region": "Dublin", | ||
"city": "Dublin" | ||
}, | ||
"android_app_name": None, | ||
"android_app_version": None, | ||
"android_device": None, | ||
"android_os_version": None, | ||
"android_sdk_version": None, | ||
"android_last_seen_at": None, | ||
"ios_app_name": None, | ||
"ios_app_version": None, | ||
"ios_device": None, | ||
"ios_os_version": None, | ||
"ios_sdk_version": None, | ||
"ios_last_seen_at": None, | ||
"custom_attributes": { | ||
"paid_subscriber": True, | ||
"monthly_spend": 155.5, | ||
"team_mates": 1 | ||
}, | ||
"tags": { | ||
"type": "list", | ||
"data": [ | ||
{ | ||
"type": "tag", | ||
"id": "2", | ||
"url": "/tags/2" | ||
}, | ||
{ | ||
"type": "tag", | ||
"id": "4", | ||
"url": "/tags/4" | ||
}, | ||
{ | ||
"type": "tag", | ||
"id": "5", | ||
"url": "/tags/5" | ||
} | ||
], | ||
"url": "/contacts/5ba682d23d7cf92bef87bfd4/tags", | ||
"total_count": 3, | ||
"has_more": False | ||
}, | ||
"notes": { | ||
"type": "list", | ||
"data": [ | ||
{ | ||
"type": "note", | ||
"id": "20114858", | ||
"url": "/notes/20114858" | ||
} | ||
], | ||
"url": "/contacts/5ba682d23d7cf92bef87bfd4/notes", | ||
"total_count": 1, | ||
"has_more": False | ||
}, | ||
"companies": { | ||
"type": "list", | ||
"data": [ | ||
{ | ||
"type": "company", | ||
"id": "5ba686093d7cf93552a3dc99", | ||
"url": "/companies/5ba686093d7cf93552a3dc99" | ||
|
||
}, | ||
{ | ||
"type": "company", | ||
"id": "5cee64a03d7cf90c51b36f19", | ||
"url": "/companies/5cee64a03d7cf90c51b36f19" | ||
}, | ||
{ | ||
"type": "company", | ||
"id": "5d7668883d7cf944dbc5c791", | ||
"url": "/companies/5d7668883d7cf944dbc5c791" | ||
} | ||
], | ||
"url": "/contacts/5ba682d23d7cf92bef87bfd4/companies", | ||
"total_count": 3, | ||
"has_more": False | ||
} | ||
} | ||
|
||
def get_company(name): | ||
return { | ||
|
@@ -160,7 +282,6 @@ def get_company(name): | |
} | ||
} | ||
|
||
|
||
def get_event(name="the-event-name"): | ||
return { | ||
"type": "event", | ||
|
@@ -195,6 +316,26 @@ def page_of_users(include_next_link=False): | |
page["pages"]["next"] = "https://api.intercom.io/users?per_page=50&page=2" | ||
return page | ||
|
||
def page_of_contacts(include_next_link=False): | ||
page = { | ||
"type": "contacts.list", | ||
"pages": { | ||
"type": "pages", | ||
"page": 1, | ||
"next": None, | ||
"per_page": 50, | ||
"total_pages": 7 | ||
}, | ||
"users": [ | ||
get_contact("[email protected]"), | ||
get_contact("[email protected]"), | ||
get_contact("[email protected]")], | ||
"total_count": 314 | ||
} | ||
if include_next_link: | ||
page["pages"]["next"] = "https://api.intercom.io/contacts?per_page=50&page=2" | ||
return page | ||
|
||
|
||
def users_scroll(include_users=False): # noqa | ||
# a "page" of results from the Scroll API | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1