Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert all Spotify response objects to Pydantic models #975

Open
MrinalJain17 opened this issue May 13, 2023 · 4 comments
Open

Convert all Spotify response objects to Pydantic models #975

MrinalJain17 opened this issue May 13, 2023 · 4 comments

Comments

@MrinalJain17
Copy link

Is your feature request related to a problem? Please describe.

  • Working with raw JSON response objects/dictionaries is tedious.
  • No concrete information about the type and structure of any particular response. One has to refer to the official Spotify API documentation.

Describe the solution you'd like

All the responses can be represented as Pydantic models. https://docs.pydantic.dev/latest/

  • Enables better developer experience (integrates well with IDEs)
  • Strong type checking (and validation!)

Describe alternatives you've considered

I've used Pydantic before, so biased towards the tool. There might be other options, but the core feature request stays the same -- well-structured and validated python objects in favour of raw JSON responses.

Additional context

This can be thought of as a more rigorous version of #695.

@stefanondisponibile
Copy link
Contributor

This idea is nice, but I can see how creating (and maintaining) such schemas could be a little bit tedious.
I was trying to figure out if we could get the json schemas somewhere, and create the type hints out of them. This way we'd just have to keep in "sync" with the schemas.

However, the only thing I found were these. The repository seems archived though, does anyone know if there are any fresher ones?

@mase-git
Copy link
Contributor

mase-git commented Jun 3, 2023

Problem here is the dynamic structure response that changes over time according to the Spotify API changelog. You need to adapt manually every schema and mantains the latest versions with updated schema. This identifies a notable effort spent to contributors.

Unfortunately, Spotify didn't update response schema very well and it's a bit tedious mantains a good schema without occurs in some issues.

@dieser-niko
Copy link
Member

Even though pydantic and similar approaches probably won't be implemented in spotipy, I just want to leave this OpenAPI specification here: https://developer.spotify.com/reference/web-api/open-api-schema.yaml
Apparently it also has some wrong parts, but I didn't look into it yet.

Found in this community post: https://community.spotify.com/t5/Spotify-for-Developers/OpenApi-Swagger-description-for-the-Web-API/m-p/5329753#messageview_1

@stefanondisponibile
Copy link
Contributor

yep, @dieser-niko is right. If that can be of help for @MrinalJain17 or anyone in the future one option is converting that openapi schema to pydantic classes (pydantic docs, datamodel-code-generator). So ideally, you could have a thin layer to convert spotipy's responses to Pydantic objects. IMO this remains overkill to implement and remain in sync, but just in case someone needs it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants