Skip to content

Commit

Permalink
Feat/new client with headers (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman authored Oct 14, 2024
1 parent b6b4ef0 commit 7554d4c
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 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,42 @@ 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 {
headers.insert(key.ok_or(Error::InvalidResponse)?, 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

0 comments on commit 7554d4c

Please sign in to comment.