Skip to content

Commit 8ea32f9

Browse files
authored
Remove traits AsDataLakeClient and AsFileSystemClient (#491)
* Removes AsStorageClient trait in favor of into_storage_client function. Matches CosmosClient in that it doesn't use Arc anymore. Still uses Arc e.g. in StorageAccountClient.new_emulator which should probably be removed as well. e2e test passes. * Format code. * Removes AsDataLakeClient and AsFileSystemClient traits in favor of into_data_lake_client and into_file_system_client functions. Matches CosmosClient in that it doesn't use Arc and doesn't return Errors anymore. e2e test passes. * Revert "Removes AsStorageClient trait in favor of into_storage_client function. Matches CosmosClient in that it doesn't use Arc anymore. Still uses Arc e.g. in StorageAccountClient.new_emulator which should probably be removed as well. e2e test passes." This reverts commit d252f98 * Fix up problems stemming from reverting commit d252f98. StorageClient can no longer create a DataLakeClient and callers must use DataLakeClient.new() instead. * PR feedback: Change from String to &str.
1 parent e6b2138 commit 8ea32f9

File tree

6 files changed

+45
-114
lines changed

6 files changed

+45
-114
lines changed

sdk/storage/examples/data_lake_00.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
3232
println!("token expires on {}", bearer_token.expires_on);
3333
println!();
3434

35-
let data_lake = storage_account_client
36-
.as_storage_client()
37-
.as_data_lake_client(account, bearer_token.token.secret().to_owned())?;
35+
let storage_client = storage_account_client.as_storage_client();
36+
let data_lake = DataLakeClient::new(
37+
storage_client,
38+
account,
39+
bearer_token.token.secret().to_owned(),
40+
None,
41+
);
3842

39-
let file_system = data_lake.as_file_system_client(&file_system_name)?;
43+
let file_system = data_lake
44+
.clone()
45+
.into_file_system_client(file_system_name.to_string());
4046

4147
let mut fs_properties = Properties::new();
4248
fs_properties.insert("AddedVia", "Azure SDK for Rust");

sdk/storage/src/data_lake/clients/data_lake_client.rs

Lines changed: 14 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::core::prelude::*;
22
use crate::data_lake::authorization_policy::AuthorizationPolicy;
33
use crate::data_lake::authorization_policy::DataLakeContext;
4+
use crate::data_lake::clients::FileSystemClient;
45
use crate::data_lake::requests::*;
56
use azure_core::pipeline::Pipeline;
67
use azure_core::prelude::*;
@@ -9,83 +10,16 @@ use bytes::Bytes;
910
use http::method::Method;
1011
use http::request::{Builder, Request};
1112
use std::sync::Arc;
12-
use url::{ParseError, Url};
1313

1414
const DEFAULT_DNS_SUFFIX: &str = "dfs.core.windows.net";
1515

16-
pub trait AsDataLakeClient<A: Into<String>> {
17-
fn as_data_lake_client(
18-
&self,
19-
account: A,
20-
bearer_token: String,
21-
) -> Result<Arc<DataLakeClient>, url::ParseError>;
22-
23-
#[cfg(feature = "mock_transport_framework")]
24-
fn as_data_lake_client_with_transaction(
25-
&self,
26-
account: A,
27-
bearer_token: String,
28-
transaction_name: impl Into<String>,
29-
) -> Result<Arc<DataLakeClient>, url::ParseError>;
30-
}
31-
32-
pub trait AsCustomDataLakeClient<DS: Into<String>, A: Into<String>> {
33-
fn as_data_lake_client_with_custom_dns_suffix(
34-
&self,
35-
account: A,
36-
bearer_token: String,
37-
dns_suffix: DS,
38-
) -> Result<Arc<DataLakeClient>, url::ParseError>;
39-
}
40-
41-
impl<A: Into<String>> AsDataLakeClient<A> for Arc<StorageClient> {
42-
fn as_data_lake_client(
43-
&self,
44-
account: A,
45-
bearer_token: String,
46-
) -> Result<Arc<DataLakeClient>, url::ParseError> {
47-
DataLakeClient::new(self.clone(), account.into(), bearer_token, None)
48-
}
49-
50-
#[cfg(feature = "mock_transport_framework")]
51-
fn as_data_lake_client_with_transaction(
52-
&self,
53-
account: A,
54-
bearer_token: String,
55-
transaction_name: impl Into<String>,
56-
) -> Result<Arc<DataLakeClient>, url::ParseError> {
57-
DataLakeClient::new_with_transaction(
58-
self.clone(),
59-
account.into(),
60-
bearer_token,
61-
transaction_name,
62-
)
63-
}
64-
}
65-
66-
impl<DS: Into<String>, A: Into<String>> AsCustomDataLakeClient<DS, A> for Arc<StorageClient> {
67-
fn as_data_lake_client_with_custom_dns_suffix(
68-
&self,
69-
account: A,
70-
bearer_token: String,
71-
dns_suffix: DS,
72-
) -> Result<Arc<DataLakeClient>, url::ParseError> {
73-
DataLakeClient::new(
74-
self.clone(),
75-
account.into(),
76-
bearer_token,
77-
Some(dns_suffix.into()),
78-
)
79-
}
80-
}
81-
8216
#[derive(Debug, Clone)]
8317
pub struct DataLakeClient {
8418
pipeline: Pipeline<DataLakeContext>,
8519
storage_client: Arc<StorageClient>,
8620
account: String,
8721
custom_dns_suffix: Option<String>,
88-
url: Url, // TODO: Use CloudLocation similar to CosmosClient
22+
url: String, // TODO: Use CloudLocation similar to CosmosClient
8923
}
9024

9125
impl DataLakeClient {
@@ -95,20 +29,17 @@ impl DataLakeClient {
9529
bearer_token: String,
9630
custom_dns_suffix: Option<String>,
9731
options: ClientOptions<DataLakeContext>,
98-
) -> Result<Arc<Self>, url::ParseError> {
32+
) -> Self {
9933
// we precalculate the url once in the constructor
10034
// so we do not have to do it at every request.
101-
// This means we have to account for possible
102-
// malfolmed urls in the constructor, hence
103-
// the Result<_, url::ParseError>.
104-
let url = url::Url::parse(&format!(
35+
let url = format!(
10536
"https://{}.{}",
10637
account,
10738
match custom_dns_suffix.as_ref() {
10839
Some(custom_dns_suffix) => custom_dns_suffix,
10940
None => DEFAULT_DNS_SUFFIX,
11041
}
111-
))?;
42+
);
11243

11344
let per_call_policies = Vec::new();
11445
let auth_policy: Arc<dyn azure_core::Policy<DataLakeContext>> =
@@ -127,21 +58,21 @@ impl DataLakeClient {
12758
per_retry_policies,
12859
);
12960

130-
Ok(Arc::new(Self {
61+
Self {
13162
pipeline,
13263
storage_client,
13364
account,
13465
custom_dns_suffix,
13566
url,
136-
}))
67+
}
13768
}
13869

13970
pub fn new(
14071
storage_client: Arc<StorageClient>,
14172
account: String,
14273
bearer_token: String,
14374
custom_dns_suffix: Option<String>,
144-
) -> Result<Arc<DataLakeClient>, ParseError> {
75+
) -> DataLakeClient {
14576
Self::new_with_options(
14677
storage_client,
14778
account,
@@ -157,7 +88,7 @@ impl DataLakeClient {
15788
account: String,
15889
bearer_token: String,
15990
transaction_name: impl Into<String>,
160-
) -> Result<Arc<DataLakeClient>, ParseError> {
91+
) -> DataLakeClient {
16192
Self::new_with_options(
16293
storage_client,
16394
account,
@@ -175,14 +106,18 @@ impl DataLakeClient {
175106
self.storage_client.storage_account_client().http_client()
176107
}
177108

178-
pub(crate) fn url(&self) -> &Url {
109+
pub(crate) fn url(&self) -> &str {
179110
&self.url
180111
}
181112

182113
pub fn list(&self) -> ListFileSystemsBuilder {
183114
ListFileSystemsBuilder::new(self)
184115
}
185116

117+
pub fn into_file_system_client(self, file_system_name: String) -> FileSystemClient {
118+
FileSystemClient::new(self, file_system_name)
119+
}
120+
186121
pub(crate) fn prepare_request(
187122
&self,
188123
url: &str,

sdk/storage/src/data_lake/clients/file_system_client.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,29 @@ use azure_core::pipeline::Pipeline;
66
use azure_core::{Context, HttpClient, PipelineContext};
77
use bytes::Bytes;
88

9-
use std::sync::Arc;
109
use url::Url;
1110

12-
pub trait AsFileSystemClient<A: Into<String>> {
13-
fn as_file_system_client(&self, name: A) -> Result<Arc<FileSystemClient>, url::ParseError>;
14-
}
15-
16-
impl<A: Into<String>> AsFileSystemClient<A> for Arc<DataLakeClient> {
17-
fn as_file_system_client(&self, name: A) -> Result<Arc<FileSystemClient>, url::ParseError> {
18-
FileSystemClient::new(self.clone(), name.into())
19-
}
20-
}
21-
2211
#[derive(Debug, Clone)]
2312
pub struct FileSystemClient {
24-
data_lake_client: Arc<DataLakeClient>,
13+
data_lake_client: DataLakeClient,
2514
name: String,
2615
url: Url,
2716
}
2817

2918
impl FileSystemClient {
30-
pub(crate) fn new(
31-
data_lake_client: Arc<DataLakeClient>,
32-
name: String,
33-
) -> Result<Arc<Self>, url::ParseError> {
34-
let mut url = data_lake_client.url().to_owned();
19+
pub(crate) fn new(data_lake_client: DataLakeClient, name: String) -> Self {
20+
let mut url = url::Url::parse(data_lake_client.url()).unwrap();
21+
3522
url.path_segments_mut()
36-
.map_err(|_| url::ParseError::SetHostOnCannotBeABaseUrl)?
23+
.map_err(|_| url::ParseError::SetHostOnCannotBeABaseUrl)
24+
.unwrap()
3725
.push(&name);
3826

39-
Ok(Arc::new(Self {
27+
Self {
4028
data_lake_client,
4129
name,
4230
url,
43-
}))
31+
}
4432
}
4533

4634
pub fn create(&self) -> CreateFileSystemBuilder {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
mod data_lake_client;
2-
pub use data_lake_client::{AsDataLakeClient, DataLakeClient};
2+
pub use data_lake_client::DataLakeClient;
33
mod file_system_client;
4-
pub use file_system_client::{AsFileSystemClient, FileSystemClient};
4+
pub use file_system_client::FileSystemClient;

sdk/storage/src/data_lake/requests/list_file_systems_builder.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ impl<'a> ListFileSystemsBuilder<'a> {
4040
pub async fn execute(
4141
&self,
4242
) -> Result<ListFileSystemsResponse, Box<dyn std::error::Error + Sync + Send>> {
43-
// we clone this so we can add custom
44-
// query parameters
45-
let mut url = self.data_lake_client.url().clone();
43+
let mut url = url::Url::parse(self.data_lake_client.url())?;
4644

4745
url.query_pairs_mut().append_pair("resource", "account");
4846

sdk/storage/tests/data_lake.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ async fn test_data_lake_file_system_functions() -> Result<(), Box<dyn Error + Se
3131
.get_token(resource_id)
3232
.await?;
3333

34-
let data_lake_client = storage_account_client
35-
.as_storage_client()
36-
// This test won't work during replay in CI until all operations are converted to pipeline architecture
37-
// .as_data_lake_client_with_transaction(account, bearer_token.token.secret().to_owned(), "test_data_lake_file_system_functions")?;
38-
.as_data_lake_client(account, bearer_token.token.secret().to_owned())?;
34+
let storage_client = storage_account_client.as_storage_client();
35+
let data_lake_client = DataLakeClient::new(
36+
storage_client,
37+
account,
38+
bearer_token.token.secret().to_owned(),
39+
None,
40+
);
3941

40-
let file_system_client = data_lake_client.as_file_system_client(&file_system_name)?;
42+
let file_system_client = data_lake_client
43+
.clone()
44+
.into_file_system_client(file_system_name.to_string());
4145

4246
let mut fs_properties = Properties::new();
4347
fs_properties.insert("AddedVia", "Azure SDK for Rust");

0 commit comments

Comments
 (0)