Skip to content

Commit 31d3307

Browse files
committed
Implement the delete user functionality.
Fixes intercomgh-6
1 parent 7b3ddff commit 31d3307

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

intercom/intercom.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ class Intercom(object):
7373
@api_call
7474
def _call(cls, method, url, params=None):
7575
""" Construct an API request, send it to the API, and parse the response. """
76+
7677
req_params = {}
7778
headers = { 'User-Agent': 'python-intercom/0.2.5', 'Accept': 'application/json' }
78-
if method in ('POST', 'PUT'):
79+
if method in ('POST', 'PUT', 'DELETE'):
7980
headers['content-type'] = 'application/json'
8081
req_params['data'] = json.dumps(params)
8182
elif method == 'GET':
@@ -160,6 +161,19 @@ def update_user(cls, user_id=None, email=None, name=None, created_at=None,
160161
name=name, created_at=created_at, custom_data=custom_data,
161162
last_seen_ip=last_seen_ip, last_seen_user_agent=last_seen_user_agent)
162163

164+
@classmethod
165+
def delete_user(cls, user_id=None, email=None):
166+
""" Delete a user.
167+
168+
>>> user = Intercom.get_user(user_id='7902')
169+
>>> user['email']
170+
u'bob@example.com'
171+
172+
"""
173+
params = { 'email': email, 'user_id': user_id }
174+
user_dict = Intercom._call('DELETE', Intercom.api_endpoint + 'users', params)
175+
return user_dict
176+
163177
@classmethod
164178
def create_impression(cls, user_id=None, email=None, user_ip=None,
165179
user_agent=None, location=None):

intercom/user.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ def create(cls, user_id=None, email=None, name=None, created_at=None,
109109
last_seen_ip=last_seen_ip, last_seen_user_agent=last_seen_user_agent)
110110
return cls(resp)
111111

112+
@classmethod
113+
def delete(cls, user_id=None, email=None):
114+
""" Deletes a user.
115+
116+
>>> user = User.delete(email="somebody@example.com")
117+
>>> user.user_id
118+
u'123'
119+
>>> user = User.delete(user_id="123")
120+
>>> user.email
121+
u'bob@example.com'
122+
123+
"""
124+
resp = Intercom.delete_user(user_id=user_id, email=email)
125+
return cls(resp)
126+
112127
@classmethod
113128
def all(cls):
114129
""" Return all of the Users.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"email": "ben@intercom.io",
3+
"user_id": "7902",
4+
"name": "Ben McRedmond",
5+
"created_at": 1257553080,
6+
"last_impression_at": 1300000000,
7+
"custom_data": {
8+
"plan": "pro"
9+
},
10+
"social_profiles": [
11+
{
12+
"type": "twitter",
13+
"url": "http://twitter.com/abc",
14+
"username": "abc"
15+
},
16+
{
17+
"type": "facebook",
18+
"url": "http://facebook.com/vanity",
19+
"username": "vanity",
20+
"id": "13241141441141413"
21+
}
22+
],
23+
"location_data": {
24+
"city_name": "Santiago",
25+
"continent_code": "SA",
26+
"country_name": "Chile",
27+
"latitude": -33.44999999999999,
28+
"longitude": -70.6667,
29+
"postal_code": "",
30+
"region_name": "12",
31+
"timezone": "Chile/Continental",
32+
"country_code": "CHL"
33+
},
34+
"session_count": 0,
35+
"last_seen_ip": "1.2.3.4",
36+
"last_seen_user_agent": "ie6",
37+
"relationship_score": 90,
38+
"unsubscribed_from_emails": false
39+
}

tests/unit/test_intercom.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def test_create_valid(self):
3232
self.assertEqual(None, resp['user_id'])
3333
self.assertEqual('xxx@example.com', resp['email'])
3434

35+
@patch('requests.request', create_response(200, 'delete_user_valid.json'))
36+
def test_delete_valid(self):
37+
resp = Intercom.delete_user(email='ben@intercom.io')
38+
self.assertEqual('ben@intercom.io', resp['email'])
39+
3540
@raises(AuthenticationError)
3641
@patch('requests.request', create_response(401))
3742
def test_get_user_identifiers(self):

0 commit comments

Comments
 (0)