22from dataclasses import dataclass
33from typing import Dict , Mapping , Optional
44
5- import requests
5+ from aiohttp import ClientSession
66
77from ytmusicapi .constants import (
88 OAUTH_CLIENT_ID ,
@@ -47,7 +47,7 @@ def __init__(
4747 self ,
4848 client_id : Optional [str ] = None ,
4949 client_secret : Optional [str ] = None ,
50- session : Optional [requests . Session ] = None ,
50+ session : Optional [ClientSession ] = None ,
5151 proxies : Optional [Dict ] = None ,
5252 ):
5353 """
@@ -67,22 +67,22 @@ def __init__(
6767 self .client_id = client_id if client_id else OAUTH_CLIENT_ID
6868 self .client_secret = client_secret if client_secret else OAUTH_CLIENT_SECRET
6969
70- self ._session = session if session else requests . Session () # for auth requests
70+ self ._session = session if session else ClientSession () # for auth requests
7171 if proxies :
7272 self ._session .proxies .update (proxies )
7373
74- def get_code (self ) -> AuthCodeDict :
74+ async def get_code (self ) -> AuthCodeDict :
7575 """Method for obtaining a new user auth code. First step of token creation."""
76- code_response = self ._send_request (OAUTH_CODE_URL , data = {"scope" : OAUTH_SCOPE })
77- return code_response .json ()
76+ code_response = await self ._send_request (OAUTH_CODE_URL , data = {"scope" : OAUTH_SCOPE })
77+ return await code_response .json ()
7878
79- def _send_request (self , url , data ):
79+ async def _send_request (self , url , data ):
8080 """Method for sending post requests with required client_id and User-Agent modifications"""
8181
8282 data .update ({"client_id" : self .client_id })
83- response = self ._session .post (url , data , headers = {"User-Agent" : OAUTH_USER_AGENT })
84- if response .status_code == 401 :
85- data = response .json ()
83+ response = await self ._session .post (url , data = data , headers = {"User-Agent" : OAUTH_USER_AGENT })
84+ if response .status == 401 :
85+ data = await response .json ()
8686 issue = data .get ("error" )
8787 if issue == "unauthorized_client" :
8888 raise UnauthorizedOAuthClient ("Token refresh error. Most likely client/token mismatch." )
@@ -94,31 +94,31 @@ def _send_request(self, url, data):
9494 )
9595 else :
9696 raise Exception (
97- f"OAuth request error. status_code: { response .status_code } , url: { url } , content: { data } "
97+ f"OAuth request error. status_code: { response .status } , url: { url } , content: { data } "
9898 )
9999 return response
100100
101- def token_from_code (self , device_code : str ) -> RefreshableTokenDict :
101+ async def token_from_code (self , device_code : str ) -> RefreshableTokenDict :
102102 """Method for verifying user auth code and conversion into a FullTokenDict."""
103- response = self ._send_request (
103+ response = await self ._send_request (
104104 OAUTH_TOKEN_URL ,
105105 data = {
106106 "client_secret" : self .client_secret ,
107107 "grant_type" : "http://oauth.net/grant_type/device/1.0" ,
108108 "code" : device_code ,
109109 },
110110 )
111- return response .json ()
111+ return await response .json ()
112112
113- def refresh_token (self , refresh_token : str ) -> BaseTokenDict :
113+ async def refresh_token (self , refresh_token : str ) -> BaseTokenDict :
114114 """
115115 Method for requesting a new access token for a given refresh_token.
116116 Token must have been created by the same OAuth client.
117117
118118 :param refresh_token: Corresponding refresh_token for a matching access_token.
119119 Obtained via
120120 """
121- response = self ._send_request (
121+ response = await self ._send_request (
122122 OAUTH_TOKEN_URL ,
123123 data = {
124124 "client_secret" : self .client_secret ,
@@ -127,4 +127,4 @@ def refresh_token(self, refresh_token: str) -> BaseTokenDict:
127127 },
128128 )
129129
130- return response .json ()
130+ return await response .json ()
0 commit comments