-
Notifications
You must be signed in to change notification settings - Fork 6
feat(client): update for current endpoints #4
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can we improve type safety? It looks like every method returns dict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.