RFC: use builders for HTTP client methods #1298
Replies: 4 comments 4 replies
-
why not validate it when building and use the same built struct for every api call instead? this way it won't have to be validated every time there's an api request |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Mabye make the builder error to require |
Beta Was this translation helpful? Give feedback.
-
We already had a discussion about builders that went a while, with the result being here: #1056 (comment) The short-term plan, however, does not consider validation. There is still discussion to be had about what to do regarding validation. Our HTTP client will always perform validation to ensure it causes the least amount of errors on Discord's side. However, the validation methods could be exposed for other uses, if one is making their own HTTP requests without our client, but still wishes to use our models. My thinking is that builders should not do validation at all, and users may validate built structs if they choose. Again, our HTTP client will always validate. But it's important to have one source of truth regarding validation, one correct set of constants and functions that validate structs. |
Beta Was this translation helpful? Give feedback.
-
Actually, methods of the http client are very similar to builders, and some conflicts with existing builders (e.g. #1165). I think it would be better to transform all these methods to independent builders. This will allow more flexibility, such as sending built types over network or storing them in database.
Suggested implementation
Two new traits are introduced. The
Validate
trait will be implemented by models to perform validation of their data (e.g. checking command description length). TheBuilder
trait will be implemented by builders. Builders must validate type using theirValidate
implementation before building them.(I am not sure if the syntax of the following code is correct, it is an example)
A default implementation of
Builder
will be provided for each type that implementValidate
(the implementation will only validate the type and return it).Example usage
Therefore, most existing http methods could take an
impl Builder<T>
parameter :It will not be mandatory to use a builder, you can also construct the type manually, and it will be validated using its
Validate
impl.What do you think about that ?
Beta Was this translation helpful? Give feedback.
All reactions