-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
208 lines (159 loc) · 6.83 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import base64
import json
import logging
import requests
import time
import webbrowser
class SpotifyClient:
def __init__(self, client_id, client_secret):
self._client_id = client_id
self._client_secret = client_secret
self._logger = logging.getLogger("gpm2spotify")
@property
def client_id(self):
return self._client_id
def authorization_header(self):
"""Header required to make unscoped requests
:returns: Dictionary, containing authorization information
"""
client_credentials = f"{self._client_id}:{self._client_secret}"
encoded_client_credentials = base64.standard_b64encode(bytearray(client_credentials, 'utf-8')).decode("utf-8")
headers = {
"Authorization": f"Basic {encoded_client_credentials}",
}
return headers
def make_request(self, method, endpoint, headers=None, data=None, json_data=None):
"""Helper to abstract making HTTP request and error handling
:param method: String, "GET" or "PUT" or "POST" or "PATCH"
:param endpoint: String, endpoint to make the request
:param headers: Dictionary, headers passed with the request
:param data: Dictionary, data to be appeneded to the request
:returns: Dictoniary, response; None if non 2XX status or if making request failed
:note: Use `response is not None` to check for success in case on empty response
"""
method_to_function = {
"GET": requests.get,
"PUT": requests.put,
"POST": requests.post,
"PATCH": requests.patch,
}
try:
resp = method_to_function[method](endpoint, headers=headers, data=data, json=json_data)
if not resp.ok:
if resp.status_code == 429:
time.sleep(int(resp.headers["Retry-After"]))
return self.make_request(method, endpoint, headers, data, json_data)
self._logger.error(
f"{method} {endpoint} Failed"
f" data={data}"
f" errcode={resp.status_code}"
f" errmsg={resp.content}"
)
return None
content = resp.content.decode()
if not content:
return dict()
else:
return json.loads(content)
except Exception:
self._logger.exception(f"{method} {endpoint} Failed data={data}")
class SpotifyUser:
def __init__(self, spotify_client, flask_server):
self._client = spotify_client
self._flask_server = flask_server
self._access_token = ""
self._logger = logging.getLogger("gpm2spotify")
def make_request(self, method, endpoint, data=None, json=None):
"""Makes a request with user authorisation
:param method: String, "GET" or "PUT" or "POST" or "PATCH"
:param endpoint: String, endpoint to make the request
:param data: Dictionary, data to me appeneded to the request
"""
if not self._access_token:
raise RuntimeError("User access token not found")
headers = {
"Authorization": f"Bearer {self._access_token}"
}
return self._client.make_request(method, endpoint, headers, data, json)
def get_access_token_url(self):
"""Returns the endpoint used to Get a new access token from spotify for accessing user data
"""
scopes = "playlist-modify-private playlist-modify-public user-library-modify user-read-private"
auth_endpoint = "https://accounts.spotify.com/authorize"
query = f"client_id={self._client.client_id}&response_type=code&redirect_uri={self._flask_server}/on_auth&scope={scopes}"
return f"{auth_endpoint}?{query}"
def get_user_id(self):
"""Request spotify for the user's id
:return: Bool, True if successfully read user's id
"""
endpoint = "https://api.spotify.com/v1/me"
resp = self.make_request("GET", endpoint)
if not resp:
self._logger.error("Failed to read user id")
return False
self._user_id = resp["id"]
return True
@property
def user_id(self):
return self._user_id
def on_auth_request_return(self, code=None, error=None):
"""Handles response from spotify authorization request
:return: Bool, True if the user was comepletely authenticated
"""
if not code:
self._logger.error(f"User authorization request failed: {error}")
return False
self._auth_code = code
success = self._get_access_token_from_auth_code()
return success
def _get_access_token_from_auth_code(self):
"""Gets access token for the authorized user
:return: Bool, True if successful
"""
spotify_access_token_endpoint = "https://accounts.spotify.com/api/token"
data = {
"grant_type": "authorization_code",
"code": self._auth_code,
"redirect_uri": f"{self._flask_server}/on_auth"
}
resp = self._client.make_request(
"POST",
spotify_access_token_endpoint,
self._client.authorization_header(),
data
)
if not resp:
self._logger.error("Failed to retrive access token for the user")
return False
self._access_token = resp["access_token"]
return True
class SpotifApplication:
def __init__(self, spotify_client):
self._client = spotify_client
self._logger = logging.getLogger("gpm2spotify")
def make_request(self, method, endpoint, data=None, json=None):
"""Makes a request to unscoped data
:param method: String, "GET" or "PUT" or "POST" or "PATCH"
:param endpoint: String, endpoint to make the request
:param data: Dictionary, data to me appeneded to the request
"""
if not self._access_token:
raise RuntimeError("User access token not found")
headers = {
"Authorization": f"Bearer {self._access_token}"
}
return self._client.make_request(method, endpoint, headers, data, json)
def get_access_token(self):
"""Gets a new access token from spotify for the unscoped data
:returns: Bool, True if the access token is successfully retrieved
"""
spotify_access_token_endpoint = "https://accounts.spotify.com/api/token"
data = {
"grant_type": "client_credentials",
}
resp = self._client.make_request("POST", spotify_access_token_endpoint, self._client.authorization_header(), data)
if not resp:
self._logger.error("Failed to get app access token")
return False
self._access_token = resp["access_token"]
return True