Added direct message endpoints.
authorJosh Roesslein <jroesslein@gmail.com>
Mon, 6 Jul 2009 02:37:57 +0000 (21:37 -0500)
committerJosh Roesslein <jroesslein@gmail.com>
Mon, 6 Jul 2009 02:37:57 +0000 (21:37 -0500)
api.py
models.py
parsers.py

diff --git a/api.py b/api.py
index 5ff79e11dd81b73d9738fdc3ba83fc8c4e43dd8b..79091cb4c2813e206068f95d3bf04f877d840701 100644 (file)
--- a/api.py
+++ b/api.py
@@ -2,13 +2,14 @@ import base64
 
 from binder import bind_api
 from parsers import *
-from models import User, Status
+from models import User, Status, DirectMessage
 
 """Twitter API"""
 class API(object):
 
   def __init__(self, username=None, password=None, host='twitter.com', secure=False,
-                classes={'user': User, 'status': Status}):
+                classes={'user': User, 'status': Status,
+                'direct_message': DirectMessage}):
     if username and password:
       self._b64up = base64.b64encode('%s:%s' % (username, password))
     else:
@@ -103,4 +104,38 @@ class API(object):
       require_auth = True
   )
 
+  """Get direct messages"""
+  direct_messages = bind_api(
+      path = '/direct_messages.json',
+      parser = parse_directmessages,
+      allowed_param = ['since_id', 'max_id', 'count', 'page'],
+      require_auth = True
+  )
+
+  """Sent direct messages"""
+  sent_direct_messages = bind_api(
+      path = '/direct_messages/sent.json',
+      parser = parse_directmessages,
+      allowed_param = ['since_id', 'max_id', 'count', 'page'],
+      require_auth = True
+  )
+
+  """Send direct message"""
+  send_direct_message = bind_api(
+      path = '/direct_messages/new.json',
+      method = 'POST',
+      parser = parse_dm,
+      allowed_param = ['user', 'text'],
+      require_auth = True
+  )
+
+  """Destroy direct message"""
+  destroy_direct_message = bind_api(
+      path = '/direct_messages/destroy.json',
+      method = 'DELETE',
+      parser = parse_dm,
+      allowed_param = ['id'],
+      require_auth = True
+  )
+
 api = API('jitterapp', 'josh1987')
index 4504e50b80c9b4bdb300c2096d3bcd7a90ae3493..5061ab1754fd39b5756169b9774dc46f80a92dc7 100644 (file)
--- a/models.py
+++ b/models.py
@@ -13,3 +13,8 @@ class User(object):
     return self._api.friends(id=self.id, **kargs)
   def followers(self, **kargs):
     return self._api.followers(id=self.id, **kargs)
+
+class DirectMessage(object):
+
+  def destroy(self):
+    return self._api.destroy_direct_message(id=self.id)
index f6037cb0cc49064055ae273a23cacbbef649d62e..8d79e99f8cc8b12b84396916a12b008abf3295e4 100644 (file)
@@ -60,3 +60,27 @@ def parse_statuses(data, api):
   for obj in json.loads(data):
     statuses.append(_parse_status(obj, api))
   return statuses
+
+def _parse_dm(obj, api):
+
+  dm = api.classes['direct_message']()
+  dm._api = api
+  for k,v in obj.items():
+    if k == 'sender':
+      setattr(dm, k, _parse_user(v, api))
+    elif k == 'created_at':
+      setattr(dm, k, _parse_datetime(v))
+    else:
+      setattr(dm, k, v)
+  return dm
+
+def parse_dm(data, api):
+
+  return _parse_dm(json.loads(data), api)
+
+def parse_directmessages(data, api):
+
+  directmessages = []
+  for obj in json.loads(data):
+    directmessages.append(_parse_dm(obj, api))
+  return directmessages