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

Feat/new client with headers #13

Merged
merged 7 commits into from
Oct 14, 2024
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,14 +659,7 @@ impl Client {
let uri = Uri::from_parts(parts).map_err(Error::InvalidRpcUrlFromUriParts)?;
let base_url = Arc::from(uri.to_string());
tracing::trace!(?uri);
let mut headers = HeaderMap::new();
headers.insert("X-Client-Name", unsafe {
"soroban-cli".parse().unwrap_unchecked()
});
let version = VERSION.unwrap_or("devel");
headers.insert("X-Client-Version", unsafe {
version.parse().unwrap_unchecked()
});
let headers = Self::default_http_headers();
let http_client = Arc::new(
HttpClientBuilder::default()
.set_headers(headers)
Expand All @@ -679,11 +672,6 @@ impl Client {
})
}

#[must_use]
pub fn base_url(&self) -> &str {
&self.base_url
}

/// Create a new client with a timeout in seconds
/// # Errors
pub fn new_with_timeout(base_url: &str, timeout: u64) -> Result<Self, Error> {
Expand All @@ -692,6 +680,45 @@ impl Client {
Ok(client)
}

/// Create a new client with additional headers
/// # Errors
pub fn new_with_headers(base_url: &str, additional_headers: HeaderMap) -> Result<Self, Error> {
let mut client = Self::new(base_url)?;
let mut headers = Self::default_http_headers();

for (key, value) in additional_headers {
match key {
None => return Err(Error::InvalidResponse),
Some(k) => headers.insert(k, value),
};
}
let http_client = Arc::new(
HttpClientBuilder::default()
.set_headers(headers)
.build(base_url)?,
);

client.http_client = http_client;
Ok(client)
}

fn default_http_headers() -> HeaderMap {
let mut headers = HeaderMap::new();
headers.insert("X-Client-Name", unsafe {
"rs-stellar-rpc-client".parse().unwrap_unchecked()
});
let version = VERSION.unwrap_or("devel");
headers.insert("X-Client-Version", unsafe {
version.parse().unwrap_unchecked()
});
headers
}

#[must_use]
pub fn base_url(&self) -> &str {
&self.base_url
}

#[must_use]
pub fn client(&self) -> &HttpClient {
&self.http_client
Expand Down
Loading