Off by one error.
[audio.git] / src / apiclient / __init__.py
1 import requests
2 from requests_oauthlib import OAuth1
3 from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET, WL_API_URL
4
5
6 class ApiError(BaseException):
7     pass
8
9
10 class NotAuthorizedError(BaseException):
11     pass
12
13
14 def api_call(user, path, method='POST', data=None, files=None):
15     from .models import OAuthConnection
16     conn = OAuthConnection.get(user=user)
17     if not conn.access:
18         raise NotAuthorizedError("No WL authorization for user %s." % user)
19
20     auth = OAuth1(WL_CONSUMER_KEY, WL_CONSUMER_SECRET, conn.token, conn.token_secret)
21
22     url = WL_API_URL + path
23
24     r = requests.request(method=method, url=url, data=data, files=files, auth=auth)
25
26     if r.status_code == 200:
27         return r.content
28     elif 201 <= r.status_code < 300:
29         return
30     elif r.status_code == 401:
31         raise ApiError('User not authorized for publishing.')
32     else:
33         raise ApiError("WL API call error %s, path: %s" % (r.status_code, path))
34
35
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)