feat(client): update for current endpoints#4
feat(client): update for current endpoints#4drisc wants to merge 13 commits intoRetroAchievements:mainfrom
Conversation
There was a problem hiding this comment.
How can we improve type safety? It looks like every method returns dict.
There was a problem hiding this comment.
I used dict because that is Pythons mapping type, since it's taking returned json as the result I figured using the native mapping type would be a good fit. Is there something else you would recommend for this?
There was a problem hiding this comment.
Looking into this more I can define the return type to increase safety e.g using Dict[str, Any]
There was a problem hiding this comment.
Dict[str, Any] is not type-safe. It's the equivalent of Record<string, any> in TypeScript. In other words, it's like saying "this can be any object".
Everything in api-js and api-kotlin is type-safe to provide a good developer experience. TypedDict should let us get similar DX benefits with Python, and it's already part of Python's standard library (Python 3.8+).
With TypedDict, IDEs can provide autocomplete and catch type errors:
response = client.get_user_points("username")
points = response["points"] # good. IDE knows this exists
typo = response["ponts"] # the IDE will show a type error, helping the dev catch their bugThere was a problem hiding this comment.
I see, so I could do something like:
from typing import TypedDict
class UserProfileResponse(TypedDict):
User: str
UserPic: str
MemberSince: str
RichPresenceMsg: str | None
TotalPoints: int
# etcthen use it like so: def get_user_profile(self, user: str) -> UserProfileResponse:
There was a problem hiding this comment.
There are some formatting issues going on in this file. Would now be a good time to set up https://github.com/psf/black?
There was a problem hiding this comment.
I thought that I had formatted the file correctly, I used black within VS Code with Format on Save enabled. We can add black as an action on the repo with a pre-commit to make sure things are good.
This PR adds all the current endpoints along with warnings for legacy and "heavy" endpoints as defined by the API docs pages.
Most of the endpoints have checks to ensure that they either provide the expected default values or they have a valid value that won't return an error from the API.
All defs are now snake_case and the file has been formatted with black and linted with flake8.