2 from requests_oauthlib import OAuth1
3 from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET, WL_API_URL
6 class ApiError(BaseException):
10 class NotAuthorizedError(BaseException):
14 def api_call(user, path, method='POST', data=None, files=None):
15 from .models import OAuthConnection
16 conn = OAuthConnection.get(user=user)
18 raise NotAuthorizedError("No WL authorization for user %s." % user)
20 auth = OAuth1(WL_CONSUMER_KEY, WL_CONSUMER_SECRET, conn.token, conn.token_secret)
22 url = WL_API_URL + path
24 r = requests.request(method=method, url=url, data=data, files=files, auth=auth)
26 if r.status_code == 200:
28 elif 201 <= r.status_code < 300:
30 elif r.status_code == 401:
31 raise ApiError('User not authorized for publishing.')
33 raise ApiError("WL API call error %s, path: %s" % (r.status_code, path))
36 def youtube_call(method, url, params=None, json=None, data=None, resumable_file_path=None):
37 from .models import YouTubeToken
38 yt = YouTubeToken.objects.first()
39 return yt.call(method, url, params=params, json=json, data=data, resumable_file_path=resumable_file_path)