3 from django.utils import simplejson
6 from apiclient.models import OAuthConnection
7 from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET, WL_API_URL
10 if WL_CONSUMER_KEY and WL_CONSUMER_SECRET:
11 wl_consumer = oauth2.Consumer(WL_CONSUMER_KEY, WL_CONSUMER_SECRET)
16 class ApiError(BaseException):
20 class NotAuthorizedError(BaseException):
24 def api_call(user, path, data=None):
25 # what if not verified?
26 conn = OAuthConnection.get(user)
28 raise NotAuthorizedError("No WL authorization for user %s." % user)
29 token = oauth2.Token(conn.token, conn.token_secret)
30 client = oauth2.Client(wl_consumer, token)
32 resp, content = client.request(
33 "%s%s.json" % (WL_API_URL, path),
35 body=urllib.urlencode(data))
37 resp, content = client.request(
38 "%s%s.json" % (WL_API_URL, path))
39 status = resp['status']
41 return simplejson.loads(content)
42 elif status.startswith('2'):
45 raise ApiError("WL API call error")