-
Notifications
You must be signed in to change notification settings - Fork 7
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
Feat/generics request #107
Conversation
I would like to implement a new To avoid this: if resp is None:
raise NoContentError() I was thinking of having something like this: @staticmethod
def perform_with_content(
path: str,
params: Dict[Any, Any],
verb: RequestVerb,
) -> T:
resp = Request[T](
path=path,
params=params,
verb=verb,
).perform()
if resp is None:
raise NoContentError()
return resp Because most of our resources should return content like With that, we can make sure the response will be of the type expected. As an example the Emails.send(...) will be: @classmethod
def send(cls, params: SendParams) -> Email:
"""
Send an email through the Resend Email API.
see more: https://resend.com/docs/api-reference/emails/send-email
Args:
params (SendParams): The email parameters
Returns:
Email: The email object that was sent
"""
path = "/emails"
resp = request.Request[Email].perform_with_content(
path=path,
params=cast(Dict[Any, Any], params),
verb="post",
)
return resp |
a7bf9e3
to
5968df8
Compare
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.
this lgtm, thanks @pedroimpulcetto
Implementing Generics type hint to avoid using "Any".
The main purpose of this PR is to avoid using "Any" inside
Request.perform()
Now Request class is a Generic class
Request(Generic[T])
, with that we can mark theperform
method to return a specific type.Example:
For this example
So Request[Email] will return an Email type
And we can make sure the
resp
variable is an EmailBecause
perform_with_content
is returning a generic typeT
perform_with_content
is a method the wrapperform
method to check if the return is not None and we can make sure the return type is a genericT
So after that we can work to include right types to return and fix this issue: #105