From 20da4ac81ce413a2b1e76f2571c6317d1ee1e8c8 Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Sun, 5 Jul 2009 22:12:16 -0500 Subject: [PATCH] Implemented friendship endpoints. --- api.py | 37 +++++++++++++++++++++++++++++++++++-- models.py | 4 ++++ parsers.py | 20 ++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/api.py b/api.py index 79091cb..a0c8a93 100644 --- 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') diff --git a/models.py b/models.py index 5061ab1..fda94e7 100644 --- 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 diff --git a/parsers.py b/parsers.py index 8d79e99..14d67c3 100644 --- a/parsers.py +++ b/parsers.py @@ -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) -- 2.25.1