Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions tsp_sdk/src/http_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#[cfg(feature = "resolve")]
use once_cell::sync::OnceCell;

#[cfg(feature = "resolve")]
#[derive(Debug)]
pub(crate) struct ReqwestClientError {
pub(crate) context: &'static str,
pub(crate) source: reqwest::Error,
}

#[cfg(feature = "resolve")]
pub(crate) fn reqwest_client() -> Result<&'static reqwest::Client, ReqwestClientError> {
static CLIENT: OnceCell<reqwest::Client> = OnceCell::new();

CLIENT.get_or_try_init(|| {
let client = reqwest::Client::builder();

#[cfg(feature = "use_local_certificate")]
let client = {
#[cfg(feature = "async")]
tracing::warn!("Using local root CA! (should only be used for local testing)");

let cert = reqwest::Certificate::from_pem(include_bytes!(
"../../examples/test/root-ca.pem"
))
.map_err(|e| ReqwestClientError {
context: "Local root CA",
source: e,
})?;

client.add_root_certificate(cert)
};

client.build().map_err(|e| ReqwestClientError {
context: "Client build error",
source: e,
})
})
}

2 changes: 2 additions & 0 deletions tsp_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub mod crypto;
/// Defines several common data structures, traits and error types that are used throughout the project.
pub mod definitions;
mod error;
#[cfg(feature = "resolve")]
mod http_client;
mod store;

/// Contains code for handling *verified identifiers* and identities.
Expand Down
16 changes: 2 additions & 14 deletions tsp_sdk/src/transport/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,11 @@ pub(crate) const SCHEME_WS: &str = "ws";
pub(crate) const SCHEME_WSS: &str = "wss";

pub(crate) async fn send_message(tsp_message: &[u8], url: &Url) -> Result<(), TransportError> {
let client = reqwest::Client::builder();

#[cfg(feature = "use_local_certificate")]
let cert = {
warn!("Using local root CA! (should only be used for local testing)");
let cert = include_bytes!("../../../examples/test/root-ca.pem");
reqwest::Certificate::from_pem(cert).unwrap()
};

#[cfg(feature = "use_local_certificate")]
let client = client.add_root_certificate(cert);

let url = url.clone();
let client = crate::http_client::reqwest_client()
.map_err(|e| TransportError::Http(e.context.to_string(), e.source))?;

let response = client
.build()
.map_err(|e| TransportError::Http("Client build error".to_string(), e))?
.post(url.clone())
.body(tsp_message.to_vec())
.send()
Expand Down
15 changes: 2 additions & 13 deletions tsp_sdk/src/vid/did/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,10 @@ pub async fn resolve(id: &str, parts: Vec<&str>) -> Result<Vid, VidError> {
{
let url = resolve_url(&parts)?;

let client = reqwest::Client::builder();

#[cfg(feature = "use_local_certificate")]
let cert = {
tracing::warn!("Using local root CA! (should only be used for local testing)");
reqwest::Certificate::from_pem(include_bytes!("../../../../examples/test/root-ca.pem"))
.unwrap()
};

#[cfg(feature = "use_local_certificate")]
let client = client.add_root_certificate(cert);
let client = crate::http_client::reqwest_client()
.map_err(|e| VidError::Http(e.context.to_string(), e.source))?;

let response = client
.build()
.map_err(|e| VidError::Http("Client build error".to_string(), e))?
.get(url.as_ref())
.send()
.await
Expand Down
Loading