1 # -*- coding: utf-8 -*-
7 from apiclient.models import OAuthConnection
8 from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET, WL_API_URL
9 from django.conf import settings
12 if WL_CONSUMER_KEY and WL_CONSUMER_SECRET:
13 wl_consumer = oauth2.Consumer(WL_CONSUMER_KEY, WL_CONSUMER_SECRET)
18 class ApiError(BaseException):
22 class NotAuthorizedError(BaseException):
26 def api_call(user, path, data=None):
27 conn = OAuthConnection.get(user)
29 raise NotAuthorizedError("No WL authorization for user %s." % user)
30 token = oauth2.Token(conn.token, conn.token_secret)
31 client = oauth2.Client(wl_consumer, token)
33 data = json.dumps(data)
34 data = urllib.urlencode({"data": data})
35 resp, content = client.request(
36 "%s%s" % (WL_API_URL, path),
40 resp, content = client.request("%s%s" % (WL_API_URL, path))
41 status = resp['status']
44 return json.loads(content)
45 elif status.startswith('2'):
48 raise ApiError(content)
50 raise ApiError('User not authorized for publishing.')
52 raise ApiError("WL API call error [code %s]" % status)