3 from urllib.parse import urlencode
5 from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET, WL_API_URL, BETA_API_URL
8 if WL_CONSUMER_KEY and WL_CONSUMER_SECRET:
9 wl_consumer = oauth2.Consumer(WL_CONSUMER_KEY, WL_CONSUMER_SECRET)
14 class ApiError(BaseException):
18 class NotAuthorizedError(BaseException):
22 def api_call(user, path, data=None, beta=False):
23 from .models import OAuthConnection
24 api_url = BETA_API_URL if beta else WL_API_URL
25 conn = OAuthConnection.get(user=user, beta=beta)
27 raise NotAuthorizedError("No WL authorization for user %s." % user)
28 token = oauth2.Token(conn.token, conn.token_secret)
29 client = oauth2.Client(wl_consumer, token)
31 data = json.dumps(data)
32 data = urlencode({"data": data})
33 resp, content = client.request(
34 "%s%s" % (api_url, path),
38 resp, content = client.request(
39 "%s%s" % (api_url, path))
40 status = resp['status']
43 return json.loads(content)
44 elif status.startswith('2'):
47 raise ApiError('User not authorized for publishing.')
49 raise ApiError("WL API call error %s, path: %s" % (status, path))