6 from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET, WL_API_URL, BETA_API_URL
9 if WL_CONSUMER_KEY and WL_CONSUMER_SECRET:
10 wl_consumer = oauth2.Consumer(WL_CONSUMER_KEY, WL_CONSUMER_SECRET)
15 class ApiError(BaseException):
19 class NotAuthorizedError(BaseException):
23 def api_call(user, path, data=None, beta=False):
24 from .models import OAuthConnection
25 api_url = BETA_API_URL if beta else WL_API_URL
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 data = json.dumps(data)
33 data = urllib.urlencode({"data": data})
34 resp, content = client.request(
35 "%s%s" % (api_url, path),
39 resp, content = client.request(
40 "%s%s" % (api_url, path))
41 status = resp['status']
44 return json.loads(content)
45 elif status.startswith('2'):
48 raise ApiError('User not authorized for publishing.')
50 raise ApiError("WL API call error %s, path: %s" % (status, path))