Skip to content
Open
Show file tree
Hide file tree
Changes from 12 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
1 change: 1 addition & 0 deletions .changes/added/3183.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add unchecked constructor for FuelClient
5 changes: 4 additions & 1 deletion bin/e2e-test-client/src/test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::{
use fuel_core_chain_config::ContractConfig;
use fuel_core_client::client::{
FuelClient,
normalize_url,
types::{
CoinType,
TransactionStatus,
Expand Down Expand Up @@ -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()
}
}

Expand Down
5 changes: 3 additions & 2 deletions bin/e2e-test-client/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use std::{
str::FromStr,
time::Duration,
};
use tempfile::TempDir; // Used for writing assertions // Run programs
use tempfile::TempDir;
// Used for writing assertions // Run programs
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use tempfile::TempDir;
// Used for writing assertions // Run programs
use tempfile::TempDir;


// Use Jemalloc
#[global_allocator]
Expand Down Expand Up @@ -81,7 +82,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
Expand Down
4 changes: 2 additions & 2 deletions bin/fuel-core-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.

match &self.command {
Command::Transaction(sub_cmd) => match sub_cmd {
Expand Down
55 changes: 6 additions & 49 deletions crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Url> {
pub fn normalize_url(url_str: &str) -> anyhow::Result<Url> {
let mut raw_url = url_str.to_string();
if !raw_url.starts_with("http") {
raw_url = format!("http://{raw_url}");
Expand Down Expand Up @@ -383,6 +383,7 @@ pub fn from_strings_errors_to_std_error(errors: Vec<String>) -> io::Error {
}

impl FuelClient {
#[deprecated(since = "0.47.1", note = "Use `new_unchecked` instead")]
pub fn new(url: impl AsRef<str>) -> anyhow::Result<Self> {
Self::from_str(url.as_ref())
}
Expand All @@ -393,7 +394,7 @@ impl FuelClient {
rpc_url: R,
) -> anyhow::Result<Self> {
let urls: Vec<_> = graph_ql_urls
.map(|str| normalize_url(str.as_ref()))
.map(|str| Url::parse(str.as_ref()))
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.
.try_collect()?;
let mut client = Self::with_urls(&urls)?;
let mut raw_rpc_url = <R as AsRef<str>>::as_ref(&rpc_url).to_string();
Expand All @@ -412,7 +413,7 @@ impl FuelClient {
}
let urls = urls
.iter()
.map(|url| normalize_url(url.as_ref()))
.map(|url| Url::parse(url.as_ref()))
.collect::<Result<Vec<_>, _>>()?;
Ok(Self {
transport: FailoverTransport::new(urls)?,
Expand Down Expand Up @@ -1944,55 +1945,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");
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/dos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::new_unchecked(url.clone()).unwrap();
client.produce_blocks(NUM_OF_BLOCKS, None).await.unwrap();

// Given
Expand Down
Loading