From 70f628a64c1e59f183a1faa27fcc34d5a0578f2d Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sat, 19 Feb 2011 16:36:01 -0500 Subject: [PATCH] extracted _safe_request() new api.post function --- convore/api.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/convore/api.py b/convore/api.py index 1a71399..aa016ad 100644 --- a/convore/api.py +++ b/convore/api.py @@ -12,15 +12,7 @@ API_URL = 'https://convore.com/api/' # Helpers # ======= -def get(*path): - """ - api.get('groups') - api.get('groups', 'id') - api.get('accounts', 'verify') - """ - - url = '%s%s%s' % (API_URL, '/'.join(map(str, path)), '.json') - r = requests.get(url) +def _safe_response(r): try: r.raise_for_status() return r @@ -30,8 +22,23 @@ def get(*path): else: raise APIError +def get(*path): + """ + api.get('groups') + api.get('groups', 'id') + api.get('accounts', 'verify') + """ + + url = '%s%s%s' % (API_URL, '/'.join(map(str, path)), '.json') + r = requests.get(url) + return _safe_response(r) +def post(params, *path): + url = '%s%s%s' % (API_URL, '/'.join(map(str, path)), '.json') + r = requests.post(url, params=params) + return _safe_response(r) + # ========== # Exceptions # ========== @@ -141,8 +148,7 @@ class Groups(UserList): return group try: - r = requests.get(API_URL + 'groups/%s.json' % key) - r.raise_for_status() + r = get('groups', key) group = Group() group.import_from_api(json.loads(r.content)['group'])