Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Url::path_segments_mut to create container & blob URLs #1379

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 18 additions & 6 deletions sdk/storage_blobs/src/clients/blob_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ impl BlobClient {

/// Full URL for the blob.
pub fn url(&self) -> azure_core::Result<url::Url> {
let blob_name = self
.blob_name()
.strip_prefix('/')
.unwrap_or_else(|| self.blob_name());
let url = format!("{}/{}", self.container_client().url()?, blob_name);
Ok(url::Url::parse(&url)?)
let mut url = self.container_client().url()?;
let parts = self.blob_name().trim_matches('/').split('/');
url.path_segments_mut()
.map_err(|_| Error::message(ErrorKind::DataConversion, "Invalid url"))?
.extend(parts);
Ok(url)
}

pub(crate) fn finalize_request(
Expand Down Expand Up @@ -399,5 +399,17 @@ mod tests {
url.as_str(),
"http://127.0.0.1:10000/devstoreaccount1/a/b/c/d?fake_token"
);

let url = build_url("a", "/b/c/d", &sas);
assert_eq!(
url.as_str(),
"http://127.0.0.1:10000/devstoreaccount1/a/b/c/d?fake_token"
);

let url = build_url("a", "b/c/d/hi there", &sas);
assert_eq!(
url.as_str(),
"http://127.0.0.1:10000/devstoreaccount1/a/b/c/d/hi%20there?fake_token"
);
}
}
17 changes: 5 additions & 12 deletions sdk/storage_blobs/src/clients/container_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,11 @@ impl ContainerClient {

/// Full URL for the container.
pub fn url(&self) -> azure_core::Result<url::Url> {
let container_name = self
.container_name()
.strip_prefix('/')
.unwrap_or_else(|| self.container_name());
let sep = if self.service_client.url()?.path().ends_with('/') {
""
} else {
"/"
};

let url = format!("{}{}{}", self.service_client.url()?, sep, container_name);
Ok(url::Url::parse(&url)?)
let mut url = self.service_client.url()?;
url.path_segments_mut()
.map_err(|_| Error::message(ErrorKind::DataConversion, "Invalid url"))?
.push(self.container_name());
Ok(url)
}

pub(crate) fn credentials(&self) -> &StorageCredentials {
Expand Down