ztunnel
ztunnel copied to clipboard
outbound: add request builder
Fix #846
Introduce typed-builder to reduce builder codes and make all struct easy to build.
@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.
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);
My concern is not with the crate it's with the concept in general
Sure, if the issue is not planed, feel free to close all.
ping @hzxuzhonghu as the issue requester.
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.