X-Git-Url: https://git.mdrn.pl/audio.git/blobdiff_plain/c84a8c9ad465608d565821914169f19f30b3b2c3..e1b56c9335e1b315fcce885d95a9d9d7a86fe566:/src/apiclient/models.py?ds=sidebyside diff --git a/src/apiclient/models.py b/src/apiclient/models.py index 83979e8..07f804f 100644 --- a/src/apiclient/models.py +++ b/src/apiclient/models.py @@ -1,5 +1,8 @@ +import json from django.db import models from django.contrib.auth.models import User +from requests_oauthlib import OAuth2Session +from .settings import YOUTUBE_CLIENT_ID, YOUTUBE_CLIENT_SECRET, YOUTUBE_TOKEN_URL class OAuthConnection(models.Model): @@ -18,3 +21,43 @@ class OAuthConnection(models.Model): return o +class YouTubeToken(models.Model): + token = models.TextField() + + def token_updater(self, token): + self.token = json.dumps(token) + self.save() + + def get_session(self): + return OAuth2Session( + client_id=YOUTUBE_CLIENT_ID, + auto_refresh_url=YOUTUBE_TOKEN_URL, + token=json.loads(self.token), + auto_refresh_kwargs={'client_id':YOUTUBE_CLIENT_ID,'client_secret':YOUTUBE_CLIENT_SECRET}, + token_updater=self.token_updater + ) + + def call(self, method, url, params=None, json=None, data=None, resumable_data=None): + params = params or {} + if resumable_data: + params['uploadType'] = 'resumable' + + session = self.get_session() + response = session.request( + method=method, + url=url, + json=json, + data=data, + params=params, + headers={ + 'X-Upload-Content-Length': str(len(resumable_data)), + 'x-upload-content-type': 'application/octet-stream', + } if resumable_data else {} + ) + if resumable_data: + location = response.headers['Location'] + return session.put( + url=location, + data=resumable_data, + headers={"Content-Type": "application/octet-stream"}, + )