nifi_rust_client/
builder.rs1use std::time::Duration;
2
3use snafu::ResultExt as _;
4use url::Url;
5
6use crate::NifiClient;
7use crate::NifiError;
8use crate::error::{HttpSnafu, InvalidBaseUrlSnafu, InvalidCertificateSnafu};
9
10#[derive(Debug)]
32pub struct NifiClientBuilder {
33 base_url: Url,
34 timeout: Option<Duration>,
35 connect_timeout: Option<Duration>,
36 proxy_all: Option<Url>,
37 proxy_http: Option<Url>,
38 proxy_https: Option<Url>,
39 danger_accept_invalid_certs: bool,
40 root_certificates: Vec<Vec<u8>>,
41}
42
43impl NifiClientBuilder {
44 pub fn new(base_url: &str) -> Result<Self, NifiError> {
48 let base_url = Url::parse(base_url).context(InvalidBaseUrlSnafu)?;
49 Ok(Self {
50 base_url,
51 timeout: None,
52 connect_timeout: None,
53 proxy_all: None,
54 proxy_http: None,
55 proxy_https: None,
56 danger_accept_invalid_certs: false,
57 root_certificates: Vec::new(),
58 })
59 }
60
61 pub fn timeout(mut self, duration: Duration) -> Self {
66 self.timeout = Some(duration);
67 self
68 }
69
70 pub fn connect_timeout(mut self, duration: Duration) -> Self {
72 self.connect_timeout = Some(duration);
73 self
74 }
75
76 pub fn proxy(mut self, url: Url) -> Self {
78 self.proxy_all = Some(url);
79 self
80 }
81
82 pub fn http_proxy(mut self, url: Url) -> Self {
84 self.proxy_http = Some(url);
85 self
86 }
87
88 pub fn https_proxy(mut self, url: Url) -> Self {
90 self.proxy_https = Some(url);
91 self
92 }
93
94 pub fn danger_accept_invalid_certs(mut self, accept: bool) -> Self {
98 self.danger_accept_invalid_certs = accept;
99 self
100 }
101
102 pub fn add_root_certificate(mut self, pem: &[u8]) -> Self {
106 self.root_certificates.push(pem.to_vec());
107 self
108 }
109
110 pub fn build(self) -> Result<NifiClient, NifiError> {
112 let mut builder = reqwest::Client::builder()
113 .danger_accept_invalid_certs(self.danger_accept_invalid_certs);
114
115 if let Some(d) = self.timeout {
116 builder = builder.timeout(d);
117 }
118 if let Some(d) = self.connect_timeout {
119 builder = builder.connect_timeout(d);
120 }
121
122 for pem in &self.root_certificates {
123 let cert = reqwest::Certificate::from_pem(pem).context(InvalidCertificateSnafu)?;
124 builder = builder.add_root_certificate(cert);
125 }
126
127 if let Some(url) = self.proxy_all {
128 let proxy = reqwest::Proxy::all(url.as_str()).context(HttpSnafu)?;
129 builder = builder.proxy(proxy);
130 }
131 if let Some(url) = self.proxy_http {
132 let proxy = reqwest::Proxy::http(url.as_str()).context(HttpSnafu)?;
133 builder = builder.proxy(proxy);
134 }
135 if let Some(url) = self.proxy_https {
136 let proxy = reqwest::Proxy::https(url.as_str()).context(HttpSnafu)?;
137 builder = builder.proxy(proxy);
138 }
139
140 let http = builder.build().context(HttpSnafu)?;
141 Ok(NifiClient::from_parts(self.base_url, http))
142 }
143
144 #[cfg(feature = "dynamic")]
166 pub async fn build_dynamic(self) -> Result<crate::dynamic::DynamicClient, NifiError> {
167 let client = self.build()?;
168 crate::dynamic::DynamicClient::from_client(client).await
169 }
170}