Skip to content
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

outbound: add request builder #912

Closed
wants to merge 1 commit into from
Closed

Conversation

daixiang0
Copy link
Member

Fix #846

Introduce typed-builder to reduce builder codes and make all struct easy to build.

Signed-off-by: Loong <loong.dai@intel.com>
@daixiang0 daixiang0 requested a review from a team as a code owner April 10, 2024 03:17
@istio-testing istio-testing added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Apr 10, 2024
Copy link
Member

@howardjohn howardjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a regression in safety and behavior. It bloats the code and adds indirection via macro, and makes it possible to forget to set fields - less safe than today. The only real benefit is we can omit the None fields, which I view as a downside - being explicit is important here

@istio-testing
Copy link
Contributor

@daixiang0: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
test_ztunnel fd82490 link true /test test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@daixiang0
Copy link
Member Author

daixiang0 commented Apr 10, 2024

I want to use existed crate to reduce the builder codes, here is an example without any crate:

struct RequestBuilder {
    method: String,
    url: String,
    headers: Vec<(String, String)>,
    body: Option<String>,
}

impl RequestBuilder {
    pub fn new(method: &str, url: &str) -> Self {
        Self {
            method: method.to_string(),
            url: url.to_string(),
            headers: Vec::new(),
            body: None,
        }
    }

    pub fn add_header(mut self, key: &str, value: &str) -> Self {
        self.headers.push((key.to_string(), value.to_string()));
        self
    }

    pub fn set_body(mut self, body: &str) -> Self {
        self.body = Some(body.to_string());
        self
    }

    pub fn build(self) -> Result<Request, &'static str> {
        let request = Request {
            method: self.method,
            url: self.url,
            headers: self.headers,
            body: self.body,
        };

        if request_is_valid(&request) {
            Ok(request)
        } else {
            Err("Invalid request")
        }
    }
}

let request = RequestBuilder::new("GET", "https://api.example.com")
    .add_header("Authorization", "Bearer token")
    .set_body("Hello, world!")
    .build()?;

println!("Request: {:?}", request);

@howardjohn
Copy link
Member

My concern is not with the crate it's with the concept in general

@daixiang0
Copy link
Member Author

Sure, if the issue is not planed, feel free to close all.

@daixiang0
Copy link
Member Author

daixiang0 commented Apr 10, 2024

ping @hzxuzhonghu as the issue requester.

@istio-testing
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@istio-testing istio-testing added the needs-rebase Indicates a PR needs to be rebased before being merged label Apr 12, 2024
@daixiang0 daixiang0 closed this May 13, 2024
@daixiang0 daixiang0 deleted the request branch May 13, 2024 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-rebase Indicates a PR needs to be rebased before being merged size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

finer grained request builder
3 participants