Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions redbot/cogs/audio/apis/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async def get_country_code(self, ctx: Context = None) -> str:
else "US"
)

async def request_access_token(self) -> MutableMapping:
async def request_access_token(self) -> Optional[MutableMapping]:
"""Make a spotify call to get the auth token."""
await self.get_token()
payload = {"grant_type": "client_credentials"}
Expand All @@ -141,6 +141,7 @@ async def get_access_token(self) -> Optional[str]:
token = await self.request_access_token()
if token is None:
log.debug("Requested a token from Spotify, did not end up getting one.")
return None
try:
token["expires_at"] = int(time.time()) + int(token["expires_in"])
except KeyError:
Expand All @@ -151,12 +152,18 @@ async def get_access_token(self) -> Optional[str]:

async def post(
self, url: str, payload: MutableMapping, headers: MutableMapping = None
) -> MutableMapping:
) -> Optional[MutableMapping]:
"""Make a POST call to spotify."""
async with self.session.post(url, data=payload, headers=headers) as r:
data = await r.json(loads=json.loads)
if r.status != 200:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still should be checking for status code as some (if not most) non-200 responses contain valid JSON. Yes, with current usage this doesn't technically matter but we should be doing it nonetheless.
If we do this, we could probably even simplify get_access_token() and remove the try-except with KeyError from it and just directly assign instead.

log.debug("Issue making POST request to %r: [%d] %r", url, r.status, data)

# If the content-type is not JSON, we can safely assume that the request failed and return None irrespective of the status code.
if r.headers["Content-Type"] == "application/json":
data = await r.json(loads=json.loads)
else:
log.debug(
"Issue making POST request to %r: [%d] %r", url, r.status, await r.text()
)
return None
return data

async def make_get_call(self, url: str, params: MutableMapping) -> MutableMapping:
Expand Down