-
Notifications
You must be signed in to change notification settings - Fork 90
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
refactor v2 client to be more idiomatic #3318
Conversation
- options are the functions that modify a config.
This change allows finer control over each HTTP request by accepting a context.Context in methods like GET, PUT, POST, and DELETE, aligning with Go's idiomatic use of context for request-scoped settings, such as timeouts and cancellation. This approach offers greater flexibility and is consistent with the standard library's design.
- Enforce base URL as mandatory in constructor; removes the illusion of it being an 'optional' setting when it's fundamentally required for operation
the context is passed into each method
We already have finer control to set context for each request inside
I don't think this is a valid reason. This is an anti-pattern if we keep adding required fields to the constructor and optional ones as configs. Besides, this allows us to provide different ways to set the address, including passing the address as a single field (current behaviour) or pass host and port separately. Just to understand the bigger picture, what is the pain the current client is causing and why are we prioritizing fixing it? |
well, if nothing else, the client calls are broken idiomatically (e.g. many functions don't have ctx as the first parameter, which is strongly guided to - https://pkg.go.dev/context#:~:text=The%20Context%20should%20be%20the,func%20DoSomething(ctx%20context.). This solves for that. However, I do agree that using an "Option" structure is more flexible and allows for cleaner dependency injection. |
As David already alluded to, the client calls are broken idiomatically - my main source of pain. As we move from the V1 client to the V2 client I want to pay this debt down now, while it's still easy.
Providing required fields as 'options' is the anti-pattern being addressed here. Previously we passed
I prefer we enforce this type of check via the compiler. |
Yeah that makes sense. I just wanted to clarify the current implementation does support overriding the context per call, but I understand it wasn't the golang way
I like it as well
Good read. Thanks for sharing. Though it doesn't say required fields should go under the constructor. Regardless of the blog post, the set of required fields can change or grow in the future, and a field might be required or optional depending on other values. We need to make a tradeoff between compile time validation and maintainability of our APIs where we don't have to break the signatures very often when features are introduced. This is a standard tradeoff we had to make even with builder pattern commonly used in java. Still, I'll leave this decision to you whether to define the address at the constructor, and whether we want to give users the option to define the host and port separately as well. I don't feel strongly towards any, as long as we don't keep adding more fields to the constructor in the future, even required fields.
localhost as in the local host. Obviously we need to define the schema and port :) To be more specific
Don't forget that the client is our GO SDK to access Bacalhau and that developers can embed it in their client applications. There won't be bacalhau config in that setup
As I mentioned above, there will be scenarios where optional fields will be required depending on others. Runtime validation is going to be needed regardless as it provides more coverage, better error messages, and more validations, such as validating the address is not blank. Though I understand the overall value of compile time checks when possible |
Nice. Thanks for picking this up! |
@wdbaruni is there anything blocking approving this change? |
Walid touched on something critical - we need to be validating at nearly every step. We are a platform that people will SURELY misuse |
Please review each commit, they are distinct changes
At a high level this PR: