Skip to content

Commit

Permalink
move to using azure_core::Body instead of Bytes (#943)
Browse files Browse the repository at this point in the history
* move to using Body instead of Bytes

While this doesn't actually enable users to AsyncRead+AsyncSync yet, this is a step towards that.

* add is_empty due to clippy

Co-authored-by: Brian Caswell <[email protected]>
  • Loading branch information
bmc-msft and Brian Caswell authored Jul 19, 2022
1 parent 4084579 commit ba63d7d
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 61 deletions.
4 changes: 4 additions & 0 deletions sdk/core/src/bytes_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ impl SeekableStream for BytesStream {
self.bytes_read = 0;
Ok(())
}

fn len(&self) -> usize {
self.bytes.len()
}
}

impl AsyncRead for BytesStream {
Expand Down
13 changes: 13 additions & 0 deletions sdk/core/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ pub enum Body {
SeekableStream(Box<dyn SeekableStream>),
}

impl Body {
pub fn len(&self) -> usize {
match self {
Body::Bytes(bytes) => bytes.len(),
Body::SeekableStream(stream) => stream.len(),
}
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

impl<B> From<B> for Body
where
B: Into<Bytes>,
Expand Down
5 changes: 5 additions & 0 deletions sdk/core/src/seekable_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ pub trait SeekableStream:
AsyncRead + Unpin + std::fmt::Debug + Send + Sync + dyn_clone::DynClone
{
async fn reset(&mut self) -> crate::error::Result<()>;
fn len(&self) -> usize;

fn is_empty(&self) -> bool {
self.len() == 0
}
}

dyn_clone::clone_trait_object!(SeekableStream);
Expand Down
5 changes: 2 additions & 3 deletions sdk/data_tables/src/clients/entity_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use crate::{operations::*, prelude::*};
use azure_core::{
error::{Error, ErrorKind},
headers::Headers,
Context, Method, Request, Response,
Body, Context, Method, Request, Response,
};
use bytes::Bytes;
use serde::{de::DeserializeOwned, Serialize};
use url::Url;

Expand Down Expand Up @@ -119,7 +118,7 @@ impl EntityClient {
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
request_body: Option<Body>,
) -> azure_core::Result<Request> {
self.partition_key_client
.finalize_request(url, method, headers, request_body)
Expand Down
5 changes: 2 additions & 3 deletions sdk/data_tables/src/clients/partition_key_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{operations::*, prelude::*, transaction::TransactionOperations};
use azure_core::{headers::Headers, Context, Method, Request, Response, Url};
use azure_core::{headers::Headers, Body, Context, Method, Request, Response, Url};
use azure_storage::core::clients::StorageClient;
use bytes::Bytes;

#[derive(Debug, Clone)]
pub struct PartitionKeyClient {
Expand Down Expand Up @@ -42,7 +41,7 @@ impl PartitionKeyClient {
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
request_body: Option<Body>,
) -> azure_core::Result<Request> {
self.table_client
.finalize_request(url, method, headers, request_body)
Expand Down
5 changes: 2 additions & 3 deletions sdk/data_tables/src/clients/table_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{clients::*, operations::*};
use azure_core::{headers::Headers, Context, Method, Request, Response, Url};
use azure_core::{headers::Headers, Body, Context, Method, Request, Response, Url};
use azure_storage::clients::StorageClient;
use bytes::Bytes;
use serde::{de::DeserializeOwned, Serialize};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -67,7 +66,7 @@ impl TableClient {
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
request_body: Option<Body>,
) -> azure_core::Result<Request> {
self.table_service_client
.finalize_request(url, method, headers, request_body)
Expand Down
5 changes: 2 additions & 3 deletions sdk/data_tables/src/clients/table_service_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::operations::ListTablesBuilder;
use azure_core::{headers::Headers, Context, Method, Request, Response};
use azure_core::{headers::Headers, Body, Context, Method, Request, Response};
use azure_storage::core::clients::{ServiceType, StorageClient};
use bytes::Bytes;
use url::Url;

use super::TableClient;
Expand Down Expand Up @@ -56,7 +55,7 @@ impl TableServiceClient {
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
request_body: Option<Body>,
) -> azure_core::Result<Request> {
self.storage_client
.finalize_request(url, method, headers, request_body)
Expand Down
7 changes: 3 additions & 4 deletions sdk/data_tables/src/operations/insert_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ use azure_core::{
error::{Error, ErrorKind},
headers::*,
prelude::*,
CollectedResponse, Context, Method,
Body, CollectedResponse, Context, Method,
};
use bytes::Bytes;
use serde::de::DeserializeOwned;
use std::{convert::TryInto, marker::PhantomData};

#[derive(Debug, Clone)]
pub struct InsertEntityBuilder<T> {
table_client: TableClient,
body: Bytes,
body: Body,
return_entity: ReturnEntity,
context: Context,
_entity: PhantomData<T>,
Expand All @@ -22,7 +21,7 @@ impl<T> InsertEntityBuilder<T>
where
T: DeserializeOwned + Send,
{
pub(crate) fn new(table_client: TableClient, body: Bytes) -> Self {
pub(crate) fn new(table_client: TableClient, body: Body) -> Self {
Self {
table_client,
body,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::{operations::*, prelude::*};
use azure_core::{headers::*, prelude::*, CollectedResponse, Method};
use bytes::Bytes;
use azure_core::{headers::*, prelude::*, Body, CollectedResponse, Method};
use std::convert::TryInto;

operation! {
InsertOrReplaceOrMergeEntity,
client: EntityClient,
body: Bytes,
body: Body,
operation: InsertOperation,
}

Expand Down
5 changes: 2 additions & 3 deletions sdk/data_tables/src/operations/update_or_merge_entity.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::{operations::*, prelude::*, IfMatchCondition};
use azure_core::{headers::*, prelude::*, CollectedResponse, Method};
use bytes::Bytes;
use azure_core::{headers::*, prelude::*, Body, CollectedResponse, Method};
use std::convert::TryInto;

operation! {
UpdateOrMergeEntity,
client: EntityClient,
body: Bytes,
body: Body,
if_match_condition: IfMatchCondition,
operation: UpdateOperation,
}
Expand Down
8 changes: 3 additions & 5 deletions sdk/storage/src/core/clients/storage_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ use crate::shared_access_signature::account_sas::{
};
use crate::ConnectionString;
use crate::TimeoutPolicy;
use azure_core::prelude::Timeout;
use azure_core::Method;
use azure_core::{
auth::TokenCredential,
error::{Error, ErrorKind, ResultExt},
headers::*,
ClientOptions, Context, Pipeline, Request, Response,
prelude::Timeout,
Body, ClientOptions, Context, Method, Pipeline, Request, Response,
};
use bytes::Bytes;
use chrono::{DateTime, Utc};
use std::sync::Arc;
use url::Url;
Expand Down Expand Up @@ -371,7 +369,7 @@ impl StorageClient {
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
request_body: Option<Body>,
) -> azure_core::Result<Request> {
let dt = chrono::Utc::now();
let time = format!("{}", dt.format("%a, %d %h %Y %T GMT"));
Expand Down
5 changes: 2 additions & 3 deletions sdk/storage_blobs/src/blob/operations/append_block.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::{blob::operations::put_block::PutBlockResponse, prelude::*};
use azure_core::{headers::*, prelude::*};
use bytes::Bytes;
use azure_core::{headers::*, prelude::*, Body};

operation! {
AppendBlock,
client: BlobClient,
body: Bytes,
body: Body,
?hash: Hash,
?condition_max_size: ConditionMaxSize,
?condition_append_position: ConditionAppendPosition,
Expand Down
5 changes: 2 additions & 3 deletions sdk/storage_blobs/src/blob/operations/put_block.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::prelude::*;
use azure_core::{headers::*, prelude::*, RequestId};
use azure_core::{headers::*, prelude::*, Body, RequestId};
use azure_storage::{headers::consistency_from_headers, ConsistencyCRC64, ConsistencyMD5};
use bytes::Bytes;
use chrono::{DateTime, Utc};

operation! {
PutBlock,
client: BlobClient,
block_id: BlockId,
body: Bytes,
body: Body,
?hash: Hash,
?lease_id: LeaseId
}
Expand Down
5 changes: 2 additions & 3 deletions sdk/storage_blobs/src/blob/operations/put_block_blob.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::prelude::*;
use azure_core::{headers::*, prelude::*, RequestId};
use azure_core::{headers::*, prelude::*, Body, RequestId};
use azure_storage::{headers::consistency_from_headers, ConsistencyCRC64, ConsistencyMD5};
use bytes::Bytes;
use chrono::{DateTime, Utc};

operation! {
PutBlockBlob,
client: BlobClient,
body: Bytes,
body: Body,
?hash: Hash,
?content_type: ContentType,
?content_encoding: ContentEncoding,
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage_blobs/src/blob/operations/put_block_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl PutBlockListBuilder {
url,
azure_core::Method::Put,
headers,
Some(body_bytes),
Some(body_bytes.into()),
)?;

let response = self.client.send(&mut self.context, &mut request).await?;
Expand Down
5 changes: 2 additions & 3 deletions sdk/storage_blobs/src/blob/operations/put_page.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::prelude::*;
use azure_core::{headers::*, prelude::*, RequestId};
use azure_core::{headers::*, prelude::*, Body, RequestId};
use azure_storage::{headers::content_md5_from_headers, ConsistencyMD5};
use bytes::Bytes;
use chrono::{DateTime, Utc};

operation! {
PutPage,
client: BlobClient,
ba512_range: BA512Range,
content: Bytes,
content: Body,
?hash: Hash,
?sequence_number_condition: SequenceNumberCondition,
?if_modified_since_condition: IfModifiedSinceCondition,
Expand Down
13 changes: 6 additions & 7 deletions sdk/storage_blobs/src/clients/blob_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use azure_core::{
error::{Error, ErrorKind},
headers::Headers,
prelude::*,
Method, Request, Response, StatusCode,
Body, Method, Request, Response, StatusCode,
};
use azure_storage::core::{
clients::StorageCredentials,
Expand All @@ -17,7 +17,6 @@ use azure_storage::core::{
SasToken,
},
};
use bytes::Bytes;
use chrono::{DateTime, Utc};
use futures::StreamExt;
use url::Url;
Expand Down Expand Up @@ -110,7 +109,7 @@ impl BlobClient {
}

/// Creates a new block blob, or update the content of an existing block blob.
pub fn put_block_blob(&self, body: impl Into<Bytes>) -> PutBlockBlobBuilder {
pub fn put_block_blob(&self, body: impl Into<Body>) -> PutBlockBlobBuilder {
PutBlockBlobBuilder::new(self.clone(), body.into())
}

Expand Down Expand Up @@ -158,7 +157,7 @@ impl BlobClient {
pub fn put_block(
&self,
block_id: impl Into<BlockId>,
body: impl Into<Bytes>,
body: impl Into<Body>,
) -> PutBlockBuilder {
PutBlockBuilder::new(self.clone(), block_id.into(), body.into())
}
Expand Down Expand Up @@ -186,7 +185,7 @@ impl BlobClient {
}

/// Write a range of pages to a page blob.
pub fn put_page(&self, ba512_range: BA512Range, content: impl Into<Bytes>) -> PutPageBuilder {
pub fn put_page(&self, ba512_range: BA512Range, content: impl Into<Body>) -> PutPageBuilder {
PutPageBuilder::new(self.clone(), ba512_range, content.into())
}

Expand All @@ -196,7 +195,7 @@ impl BlobClient {
}

/// Commits a new block of data to the end of an existing append blob.
pub fn append_block(&self, body: impl Into<Bytes>) -> AppendBlockBuilder {
pub fn append_block(&self, body: impl Into<Body>) -> AppendBlockBuilder {
AppendBlockBuilder::new(self.clone(), body.into())
}

Expand Down Expand Up @@ -282,7 +281,7 @@ impl BlobClient {
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
request_body: Option<Body>,
) -> azure_core::Result<Request> {
self.container_client
.finalize_request(url, method, headers, request_body)
Expand Down
5 changes: 2 additions & 3 deletions sdk/storage_blobs/src/clients/blob_lease_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{blob::operations::*, prelude::*};
use azure_core::{headers::Headers, prelude::*, Context, Method, Request, Response, Url};
use azure_core::{headers::Headers, prelude::*, Body, Context, Method, Request, Response, Url};
use azure_storage::core::prelude::*;
use bytes::Bytes;

#[derive(Debug, Clone)]
pub struct BlobLeaseClient {
Expand Down Expand Up @@ -57,7 +56,7 @@ impl BlobLeaseClient {
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
request_body: Option<Body>,
) -> azure_core::Result<Request> {
self.blob_client
.finalize_request(url, method, headers, request_body)
Expand Down
6 changes: 2 additions & 4 deletions sdk/storage_blobs/src/clients/container_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use azure_core::{
error::{Error, ErrorKind},
headers::Headers,
prelude::*,
Request, Response,
Body, Method, Request, Response, Url,
};
use azure_core::{Method, Url};
use azure_storage::{
core::clients::{ServiceType, StorageClient, StorageCredentials},
prelude::BlobSasPermissions,
Expand All @@ -14,7 +13,6 @@ use azure_storage::{
SasToken,
},
};
use bytes::Bytes;
use chrono::{DateTime, Utc};

pub trait AsContainerClient<CN: Into<String>> {
Expand Down Expand Up @@ -148,7 +146,7 @@ impl ContainerClient {
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
request_body: Option<Body>,
) -> azure_core::Result<Request> {
self.storage_client
.finalize_request(url, method, headers, request_body)
Expand Down
Loading

0 comments on commit ba63d7d

Please sign in to comment.