From: Josh Roesslein Date: Fri, 7 Aug 2009 20:25:13 +0000 (-0500) Subject: Fix some bugs with new model validation code. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=19433225f82073b682543f6666999041cf2cd4ff;p=tweepy.git Fix some bugs with new model validation code. --- diff --git a/tweepy/api.py b/tweepy/api.py index 341dcc2..e49eed5 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -11,7 +11,8 @@ from error import TweepError class API(object): def __init__(self, auth_handler=None, username=None, host='twitter.com', cache=None, - secure=False, api_root='', classes={'user': User, 'status': Status, + secure=False, api_root='', validate=True, + classes={'user': User, 'status': Status, 'direct_message': DirectMessage, 'friendship': Friendship, 'saved_search': SavedSearch, 'search_result': SearchResult}): self.auth_handler = auth_handler @@ -20,6 +21,7 @@ class API(object): self.api_root = api_root self.cache = cache self.secure = secure + self.validate = validate self.classes = classes """Get public timeline""" diff --git a/tweepy/binder.py b/tweepy/binder.py index 7752972..850036a 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -79,13 +79,24 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False, # Pass returned body into parser and return parser output out = parser(resp.read(), api) + conn.close() + + # validate result + if api.validate: + # list of results + if isinstance(out, list) and len(out) > 0: + if hasattr(out[0], 'validate'): + for result in out: + result.validate() + # single result + else: + if hasattr(out, 'validate'): + out.validate() # store result in cache if api.cache and method == 'GET': api.cache.store(url, out) - # close connection and return data - conn.close() return out return _call diff --git a/tweepy/models.py b/tweepy/models.py index b1b28e3..9dc8cf3 100644 --- a/tweepy/models.py +++ b/tweepy/models.py @@ -27,7 +27,7 @@ class Status(Model): @staticmethod def _validate(status): - Model.validate(status, [ + Model._validate(status, [ 'created_at', 'id', 'text', 'source', 'truncated', 'in_reply_to_status_id', 'in_reply_to_user_id', 'favorited', 'in_reply_to_screen_name' ]) @@ -43,7 +43,7 @@ class User(Model): @staticmethod def _validate(user): - Model.validate(user, [ + Model._validate(user, [ 'id', 'name', 'screen_name', 'location', 'description', 'profile_image_url', 'url', 'protected', 'followers_count', 'profile_background_color', 'profile_text_color', 'profile_sidebar_fill_color', 'profile_sidebar_border_color',