Implemented friendship endpoints.
authorJosh Roesslein <jroesslein@gmail.com>
Mon, 6 Jul 2009 03:12:16 +0000 (22:12 -0500)
committerJosh Roesslein <jroesslein@gmail.com>
Mon, 6 Jul 2009 03:12:16 +0000 (22:12 -0500)
api.py
models.py
parsers.py

diff --git a/api.py b/api.py
index 79091cb4c2813e206068f95d3bf04f877d840701..a0c8a9371104bb57bcc632b07a823b29ec036c8e 100644 (file)
--- a/api.py
+++ b/api.py
@@ -2,14 +2,14 @@ import base64
 
 from binder import bind_api
 from parsers import *
-from models import User, Status, DirectMessage
+from models import User, Status, DirectMessage, Friendship
 
 """Twitter API"""
 class API(object):
 
   def __init__(self, username=None, password=None, host='twitter.com', secure=False,
                 classes={'user': User, 'status': Status,
-                'direct_message': DirectMessage}):
+                'direct_message': DirectMessage, 'friendship': Friendship}):
     if username and password:
       self._b64up = base64.b64encode('%s:%s' % (username, password))
     else:
@@ -138,4 +138,37 @@ class API(object):
       require_auth = True
   )
 
+  """Create friendship"""
+  create_friendship = bind_api(
+      path = '/friendships/create.json',
+      method = 'POST',
+      parser = parse_user,
+      allowed_param = ['id', 'user_id', 'screen_name', 'follow'],
+      require_auth = True
+  )
+
+  """Destroy friendship"""
+  destroy_friendship = bind_api(
+      path = '/friendships/destroy.json',
+      method = 'DELETE',
+      parser = parse_user,
+      allowed_param = ['id', 'user_id', 'screen_name'],
+      require_auth = True
+  )
+
+  """Check if friendship exists"""
+  exists_friendship = bind_api(
+      path = '/friendships/exists.json',
+      parser = parse_bool,
+      allowed_param = ['user_a', 'user_b']
+  )
+
+  """Show friendship details"""
+  show_friendship = bind_api(
+      path = '/friendships/show.json',
+      parser = parse_friendship,
+      allowed_param = ['source_id', 'source_screen_name',
+                        'target_id', 'target_screen_name']
+  )
+
 api = API('jitterapp', 'josh1987')
index 5061ab1754fd39b5756169b9774dc46f80a92dc7..fda94e73b4e23a3e2545287239f37a2cb0715797 100644 (file)
--- a/models.py
+++ b/models.py
@@ -18,3 +18,7 @@ class DirectMessage(object):
 
   def destroy(self):
     return self._api.destroy_direct_message(id=self.id)
+
+class Friendship(object):
+
+  pass
index 8d79e99f8cc8b12b84396916a12b008abf3295e4..14d67c3eba5ad192bd233e2e97ceacbbd273a8fd 100644 (file)
@@ -84,3 +84,23 @@ def parse_directmessages(data, api):
   for obj in json.loads(data):
     directmessages.append(_parse_dm(obj, api))
   return directmessages
+
+def parse_friendship(data, api):
+
+  relationship = json.loads(data)['relationship']
+
+  # parse source
+  source = api.classes['friendship']()
+  for k,v in relationship['source'].items():
+    setattr(source, k, v)
+
+  # parse target
+  target = api.classes['friendship']()
+  for k,v in relationship['target'].items():
+    setattr(target, k, v)
+
+  return source, target
+
+def parse_bool(data, api):
+
+  return json.loads(data)