1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from urllib.parse import urlencode
7 from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET, WL_API_URL, BETA_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, beta=False):
25 from .models import OAuthConnection
26 api_url = BETA_API_URL if beta else WL_API_URL
27 conn = OAuthConnection.get(user=user, beta=beta)
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 = urlencode({"data": data})
35 resp, content = client.request(
36 "%s%s" % (api_url, path),
40 resp, content = client.request(
41 "%s%s" % (api_url, path))
42 status = resp['status']
45 return json.loads(content)
46 elif status.startswith('2'):
49 raise ApiError('User not authorized for publishing.')
51 raise ApiError("WL API call error %s, path: %s" % (status, path))