diff --git a/.changes/added/3183.md b/.changes/added/3183.md new file mode 100644 index 00000000000..e81d4f3dec0 --- /dev/null +++ b/.changes/added/3183.md @@ -0,0 +1 @@ +Add unchecked constructor for FuelClient \ No newline at end of file diff --git a/bin/e2e-test-client/src/test_context.rs b/bin/e2e-test-client/src/test_context.rs index 24c5a879f09..c38fa6441a7 100644 --- a/bin/e2e-test-client/src/test_context.rs +++ b/bin/e2e-test-client/src/test_context.rs @@ -7,6 +7,7 @@ use anyhow::{ use fuel_core_chain_config::ContractConfig; use fuel_core_client::client::{ FuelClient, + normalize_url, types::{ CoinType, TransactionStatus, @@ -72,7 +73,9 @@ impl TestContext { } fn new_client(default_endpoint: String, wallet: &ClientConfig) -> FuelClient { - FuelClient::new(wallet.endpoint.clone().unwrap_or(default_endpoint)).unwrap() + let endpoint = wallet.endpoint.clone().unwrap_or(default_endpoint); + let url = normalize_url(&endpoint).unwrap(); + FuelClient::with_urls(&[url.as_str()]).unwrap() } } diff --git a/bin/e2e-test-client/tests/integration_tests.rs b/bin/e2e-test-client/tests/integration_tests.rs index d150a490b19..81b31403b2f 100644 --- a/bin/e2e-test-client/tests/integration_tests.rs +++ b/bin/e2e-test-client/tests/integration_tests.rs @@ -19,7 +19,7 @@ use std::{ str::FromStr, time::Duration, }; -use tempfile::TempDir; // Used for writing assertions // Run programs +use tempfile::TempDir; // Use Jemalloc #[global_allocator] @@ -81,7 +81,7 @@ async fn works_in_multinode_local_env() { let producer_bound_addr = producer.node.bound_address.to_string(); let validator_bound_addr = validator.node.bound_address.to_string(); - config.wallet_a.endpoint = Some(producer_bound_addr.clone()); + config.wallet_a.endpoint = Some(producer_bound_addr); config.wallet_b.endpoint = Some(validator_bound_addr); // save config file diff --git a/bin/fuel-core-client/src/main.rs b/bin/fuel-core-client/src/main.rs index b115c13954b..31348d2692f 100644 --- a/bin/fuel-core-client/src/main.rs +++ b/bin/fuel-core-client/src/main.rs @@ -42,8 +42,8 @@ struct CliArgs { impl CliArgs { async fn exec(&self) { - let client = - FuelClient::new(self.endpoint.as_str()).expect("expected valid endpoint"); + let client = FuelClient::with_urls(&[self.endpoint.as_str()]) + .expect("expected valid endpoint"); match &self.command { Command::Transaction(sub_cmd) => match sub_cmd { diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index c0bd3fec16d..366baa961aa 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -325,7 +325,7 @@ impl Default for AWSClientManager { } /// Normalizes a URL string by ensuring it has an http(s) scheme and the `/v1/graphql` path. -fn normalize_url(url_str: &str) -> anyhow::Result { +pub fn normalize_url(url_str: &str) -> anyhow::Result { let mut raw_url = url_str.to_string(); if !raw_url.starts_with("http") { raw_url = format!("http://{raw_url}"); @@ -383,6 +383,7 @@ pub fn from_strings_errors_to_std_error(errors: Vec) -> io::Error { } impl FuelClient { + #[deprecated(since = "0.47.1", note = "Use `new_unchecked` instead")] pub fn new(url: impl AsRef) -> anyhow::Result { Self::from_str(url.as_ref()) } @@ -393,7 +394,7 @@ impl FuelClient { rpc_url: R, ) -> anyhow::Result { let urls: Vec<_> = graph_ql_urls - .map(|str| normalize_url(str.as_ref())) + .map(|str| Url::parse(str.as_ref())) .try_collect()?; let mut client = Self::with_urls(&urls)?; let mut raw_rpc_url = >::as_ref(&rpc_url).to_string(); @@ -412,7 +413,14 @@ impl FuelClient { } let urls = urls .iter() - .map(|url| normalize_url(url.as_ref())) + .map(|url| { + let reference = url.as_ref(); + if !reference.starts_with("http") { + Url::parse(&format!("http://{reference}")) + } else { + Url::parse(reference) + } + }) .collect::, _>>()?; Ok(Self { transport: FailoverTransport::new(urls)?, @@ -1944,55 +1952,11 @@ impl FuelClient { mod tests { use super::*; - #[test] - fn with_urls_normalizes_urls_to_graphql_endpoint() { - // Given - let urls = &["http://localhost:8080", "http://example.com:4000"]; - - // When - let client = FuelClient::with_urls(urls).expect("should create client"); - - // Then - assert_eq!( - client.get_default_url().as_str(), - "http://localhost:8080/v1/graphql" - ); - } - - #[test] - fn with_urls_adds_http_scheme_if_missing() { - // Given - let urls = &["localhost:8080"]; - - // When - let client = FuelClient::with_urls(urls).expect("should create client"); - - // Then - assert_eq!( - client.get_default_url().as_str(), - "http://localhost:8080/v1/graphql" - ); - } - - #[test] - fn with_urls_overwrites_existing_path() { - // Given - URLs that already have some path - let urls = &["http://localhost:8080/some/path", "http://example.com/api"]; - - // When - let client = FuelClient::with_urls(urls).expect("should create client"); - - // Then - path should be normalized to /v1/graphql - assert_eq!( - client.get_default_url().as_str(), - "http://localhost:8080/v1/graphql" - ); - } - + #[allow(deprecated)] #[test] fn new_and_with_urls_produce_same_url() { // Given - let url = "http://localhost:8080"; + let url = "http://localhost:8080/v1/graphql"; // When let client_new = FuelClient::new(url).expect("should create client via new"); diff --git a/tests/tests/dos.rs b/tests/tests/dos.rs index ab11cd92fbe..e8ea0de671d 100644 --- a/tests/tests/dos.rs +++ b/tests/tests/dos.rs @@ -634,7 +634,7 @@ async fn heavy_tasks_doesnt_block_graphql() { let node = FuelService::new_node(config).await.unwrap(); let url = format!("http://{}/v1/graphql", node.bound_address); - let client = FuelClient::new(url.clone()).unwrap(); + let client = FuelClient::with_urls(&[url.clone()]).unwrap(); client.produce_blocks(NUM_OF_BLOCKS, None).await.unwrap(); // Given