Optimizing Some Python API Request Code

August 16, 2023

I almost fucked up… Big Time.

Okay, I’m being a bit overdramatic. Nothing terrible was gonna happen yesterday, but I almost wasted an hour of my time debugging code – code that needed two minutes to fix.

Let me take you on the journey of how I debugged my code faster than Sherlock Holmes could solve a case…

Right now, I’m working on a data engineering/analytics project to build a dataset that other people can use for their data portfolios. And part of my process involves retrieving data from the Spotify API.

So, I created a little script with the following chunk of code.

unoptimized code with a broken while loop

The code’s simple…

The while loop runs until the get_playlist function returns a valid response from the API.

But before checking the function, we first check if the access token is valid (Spotify’s access token only lasts one hour).

If it’s valid, we run the function.

If it’s invalid, I run a function that creates a new access token for me.

This code looks sexy to me. It’s short. It’s clean. It’s efficient…

Well, almost sexy…

It turns out the code sucks.

It doesn’t work correctly.

See, the while loop depends on the access token JSON file I have. The if statement is supposed to check for a valid/invalid access token, but it always returns a “valid” token.

That’s because the JSON file doesn’t specify if the token is valid or not. It only stores the data from the last token I created, no matter how long ago that was.

So even when the token is invalid, the if statement runs the get_playlist functions.

Then when I check the newly created JSON file, I see nothing but a bunch of 401 errors.

A whole lot of 401 error messages from my while loop code

The old me (who was more of a Python Noobie than a Data Dork) would have continued trying to improve this while loop to make it work.

But the new me knows better.

Sometimes, when we’re coding, the problem isn’t that we don’t know the answer. It’s that we don’t know how to apply the answer to our code.

In my situation, a while loop wasn’t efficient.

It would have taken me hours to create something that worked and was optimized for performance.

That’s why I changed my strategy.

I simplified my code and did what needed to be done to make the code less prone to bugs in the future.

Check it out…

optimized code after removing while loop