ztunnel icon indicating copy to clipboard operation
ztunnel copied to clipboard

outbound: add request builder

Open daixiang0 opened this issue 1 year ago • 6 comments

Fix #846

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

daixiang0 avatar Apr 10 '24 03:04 daixiang0

@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 fd824902a57885a4539cf092af29b61785157715 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.

istio-testing avatar Apr 10 '24 03:04 istio-testing

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);

daixiang0 avatar Apr 10 '24 03:04 daixiang0

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

howardjohn avatar Apr 10 '24 04:04 howardjohn

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

daixiang0 avatar Apr 10 '24 04:04 daixiang0

ping @hzxuzhonghu as the issue requester.

daixiang0 avatar Apr 10 '24 05:04 daixiang0

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 avatar Apr 12 '24 22:04 istio-testing