Skip to content

Commit

Permalink
Simplify again and create a default_http_headers fn
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Sep 27, 2024
1 parent 3d9e4f4 commit 01ff185
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,14 +665,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 @@ -685,11 +678,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 @@ -698,17 +686,12 @@ 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();

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()
});
for (key, value) in additional_headers {
headers.insert(key.unwrap(), value);
}
Expand All @@ -722,11 +705,29 @@ impl Client {
Ok(client)
}

fn default_http_headers() -> HeaderMap {
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()
});
headers
}

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

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


///
/// # Errors
pub async fn friendbot_url(&self) -> Result<String, Error> {
Expand Down

0 comments on commit 01ff185

Please sign in to comment.