Skip to content

Commit 77d20d6

Browse files
committed
Adding events support.
1 parent 56b134b commit 77d20d6

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

intercom/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ def wrapper(instance, value):
4747
from .note import Note
4848
from .user import User
4949
from .tag import Tag
50+
from .events import Event
5051

5152
__all__ = (
5253
AuthenticationError, BadGatewayError, Intercom, ResourceNotFound,
5354
ServerError, ServiceUnavailableError, Impression, MessageThread,
54-
Note, User, Tag
55+
Note, User, Tag, Event
5556
)

intercom/events.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# coding=utf-8
2+
#
3+
# Copyright 2014 martin@mekkaoui.fr
4+
#
5+
# License: MIT
6+
#
7+
""" Intercom API wrapper. """
8+
9+
from . import Intercom
10+
from .user import UserId
11+
12+
13+
class Event(UserId):
14+
15+
@classmethod
16+
def create(cls, event_name=None, user_id=None, email=None, metadata=None):
17+
resp = Intercom.create_event(event_name=event_name, user_id=user_id, email=email, metadata=metadata)
18+
return Event(resp)
19+
20+
def save(self):
21+
""" Create an Event from this objects properties:
22+
23+
>>> event = Event()
24+
>>> event.event_name = "shared-item"
25+
>>> event.email = "joe@example.com"
26+
>>> event.save()
27+
28+
"""
29+
resp = Intercom.create_event(**self)
30+
self.update(resp)
31+
32+
@property
33+
def event_name(self):
34+
""" The name of the Event. """
35+
return dict.get(self, 'event_name', None)
36+
37+
@event_name.setter
38+
def event_name(self, event_name):
39+
""" Set the event name. """
40+
self['event_name'] = event_name

intercom/intercom.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import functools
2020
import json
2121
import requests
22+
import time
2223

2324
DEFAULT_TIMEOUT = 10 # seconds
2425

@@ -63,6 +64,8 @@ def wrapper(*args, **kwargs):
6364
""" Decorator closure. """
6465
response = func_to_decorate(*args, **kwargs)
6566
raise_errors_on_failure(response)
67+
if not response.content.strip():
68+
return ''
6669
result = json.loads(response.content)
6770
return result
6871
return wrapper
@@ -121,19 +124,24 @@ def _create_or_update_user(cls, method, **kwargs):
121124

122125
@classmethod
123126
def get_users(cls, **kwargs):
124-
""" Returns a paginated list of all users in your application on Intercom.
127+
""" Returns a paginated list of all users in your application on
128+
Intercom.
125129
126130
**Arguments**
127131
128132
* ``page``: optional (defaults to 1)
129133
* ``per_page``: optional (defaults to 500, max value of 500)
130-
* ``tag_id``: optional — query for users that are tagged with a specific tag.
131-
* ``tag_name``: optional — query for users that are tagged with a specific tag.
134+
* ``tag_id``: optional — query for users that are tagged with a
135+
specific tag.
136+
* ``tag_name``: optional — query for users that are tagged with a
137+
specific tag.
132138
133139
**Response**
134140
135-
* ``users``: an array of User objects (same as returned by getting a single User)
136-
* ``total_count``: the total number of Users tracked in your Intercom application
141+
* ``users``: an array of User objects (same as returned by getting a
142+
single User)
143+
* ``total_count``: the total number of Users tracked in your Intercom
144+
application
137145
* ``page``: the current requested page
138146
* ``next_page``: the next page number, if any
139147
* ``previous_page``: the previous page number, if any
@@ -146,7 +154,8 @@ def get_users(cls, **kwargs):
146154
3
147155
148156
"""
149-
return Intercom._call('GET', Intercom.api_endpoint + 'users', params=kwargs)
157+
return Intercom._call(
158+
'GET', Intercom.api_endpoint + 'users', params=kwargs)
150159

151160
@classmethod
152161
def get_user(cls, email=None, user_id=None):
@@ -194,7 +203,8 @@ def create_user(cls, **kwargs):
194203
unsubscribed status.
195204
196205
197-
>>> user = Intercom.create_user(user_id='7902', email='ben@intercom.io',
206+
>>> user = Intercom.create_user(user_id='7902',
207+
... email='ben@intercom.io',
198208
... name='Somebody', created_at=1270000000, last_seen_ip='1.2.3.4',
199209
... custom_data={ 'app_name': 'Genesis'}, last_request_at=1300000000)
200210
>>> user['name']
@@ -433,3 +443,22 @@ def get_tag(cls, name=None):
433443
tag_dict = Intercom._call(
434444
'GET', Intercom.api_endpoint + 'tags', params=params)
435445
return tag_dict
446+
447+
@classmethod
448+
def create_event(cls, event_name=None, user_id=None, email=None, metadata=None):
449+
"""
450+
Create an event
451+
"""
452+
params = {
453+
'event_name': event_name,
454+
'user_id': user_id,
455+
'email': email,
456+
'created': int(time.time())
457+
}
458+
459+
if isinstance(metadata, dict):
460+
params['metadata'] = metadata
461+
462+
call = Intercom._call(
463+
'POST', Intercom.api_endpoint + 'events', params=params)
464+
return call

0 commit comments

Comments
 (0)