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

Queue refactor #110

Merged
merged 13 commits into from
Dec 11, 2020
29 changes: 29 additions & 0 deletions sdk/core/src/client_request_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::headers::*;
use crate::AddAsHeader;
use http::request::Builder;
use std::borrow::Cow;

#[derive(Debug, Clone)]
pub struct ClientRequestId<'a>(Cow<'a, str>);
MindFlavor marked this conversation as resolved.
Show resolved Hide resolved

impl<'a> ClientRequestId<'a> {
pub fn new<CRI: Into<Cow<'a, str>>>(client_request_id: CRI) -> Self {
Self(client_request_id.into())
}
}

impl<'a> AddAsHeader for ClientRequestId<'a> {
fn add_as_header(&self, builder: Builder) -> Builder {
builder.header(CLIENT_REQUEST_ID, self.0.as_ref())
}
}

impl<'a> AddAsHeader for Option<ClientRequestId<'a>> {
MindFlavor marked this conversation as resolved.
Show resolved Hide resolved
fn add_as_header(&self, builder: Builder) -> Builder {
if let Some(client_request_id) = self {
AddAsHeader::add_as_header(client_request_id, builder)
} else {
builder
}
}
}
8 changes: 8 additions & 0 deletions sdk/core/src/headers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
mod utilities;

use http::request::Builder;
pub use utilities::*;

pub fn add_header<T: crate::AddAsHeader>(item: &Option<T>, mut builder: Builder) -> Builder {
if let Some(item) = item {
builder = item.add_as_header(builder);
}
builder
}

pub const SERVER: &str = "server"; // -> [String]
pub const SOURCE_IF_MODIFIED_SINCE: &str = "x-ms-source-if-modified-since";
pub const SOURCE_IF_UNMODIFIED_SINCE: &str = "x-ms-source-if-unmodified-since";
Expand Down
10 changes: 10 additions & 0 deletions sdk/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,38 @@ pub mod parsing;
#[macro_use]
pub mod enumerations;
pub mod ba512_range;
mod client_request_id;
pub mod headers;
mod http_client;
pub mod incompletevector;
pub mod lease;
mod metadata;
pub mod modify_conditions;
pub mod prelude;
pub mod range;
mod stored_access_policy;
mod timeout;
pub mod util;

pub use self::stored_access_policy::{StoredAccessPolicy, StoredAccessPolicyList};
use crate::errors::AzureError;
use crate::lease::LeaseId;
use base64::encode;
use chrono::{DateTime, Utc};
pub use client_request_id::ClientRequestId;
use headers::*;
use http::request::Builder;
pub use http_client::*;
use hyper::header::{
CONTENT_ENCODING, CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_TYPE, IF_MODIFIED_SINCE, RANGE,
USER_AGENT,
};
pub use metadata::Metadata;
use modify_conditions::{IfMatchCondition, IfSinceCondition, SequenceNumberCondition};
use oauth2::AccessToken;
use std::collections::HashMap;
use std::fmt::Debug;
pub use timeout::Timeout;
use uuid::Uuid;

pub type RequestId = Uuid;
Expand Down Expand Up @@ -1117,3 +1123,7 @@ pub trait BlobNameRequired<'a> {
pub trait AddAsHeader {
fn add_as_header(&self, builder: Builder) -> Builder;
}

pub trait AppendToUrlQuery {
fn append_to_url_query(&self, url: &mut url::Url);
}
34 changes: 34 additions & 0 deletions sdk/core/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::AddAsHeader;
use http::request::Builder;
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct Metadata(HashMap<String, String>);

impl Metadata {
pub fn new() -> Self {
Self(HashMap::new())
}

pub fn as_mut(&mut self) -> &mut HashMap<String, String> {
MindFlavor marked this conversation as resolved.
Show resolved Hide resolved
&mut self.0
}
}

impl From<HashMap<String, String>> for Metadata {
fn from(metadata: HashMap<String, String>) -> Self {
Self(metadata)
}
}

impl AddAsHeader for &Metadata {
fn add_as_header(&self, builder: Builder) -> Builder {
let mut builder = builder;

for (key, val) in self.0.iter() {
builder = builder.header(&format!("x-ms-meta-{}", key) as &str, val as &str);
MindFlavor marked this conversation as resolved.
Show resolved Hide resolved
}

builder
}
}
38 changes: 20 additions & 18 deletions sdk/core/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ pub use crate::ba512_range::BA512Range;
pub use crate::modify_conditions::{IfMatchCondition, IfSinceCondition, SequenceNumberCondition};
pub use crate::range::Range;
pub use crate::{
AccessTier, AccessTierOption, AccessTierSupport, ActivityIdSupport, AppendPositionOption,
AppendPositionSupport, BA512RangeOption, BA512RangeRequired, BA512RangeSupport,
BlobNameRequired, BlobNameSupport, BlockIdRequired, BlockIdSupport, BodyRequired, BodySupport,
CacheControlOption, CacheControlSupport, ChunkSizeOption, ChunkSizeSupport,
ClientRequestIdOption, ClientRequestIdSupport, ContainerNameRequired, ContainerNameSupport,
ContentDispositionOption, ContentDispositionSupport, ContentEncodingOption,
ContentEncodingSupport, ContentLanguageOption, ContentLanguageSupport, ContentLengthOption,
ContentLengthRequired, ContentLengthSupport, ContentMD5Option, ContentMD5Support, ContentType,
ContentTypeOption, ContentTypeRequired, ContentTypeSupport, Continuation, ContinuationOption,
ContinuationSupport, DeleteSnapshotsMethod, DeleteSnapshotsMethodSupport, DelimiterOption,
DelimiterSupport, HttpClient, IfMatchConditionOption, IfMatchConditionSupport, IfModifiedSince,
AccessTier, AccessTierOption, AccessTierSupport, ActivityIdSupport, AddAsHeader,
AppendPositionOption, AppendPositionSupport, AppendToUrlQuery, BA512RangeOption,
BA512RangeRequired, BA512RangeSupport, BlobNameRequired, BlobNameSupport, BlockIdRequired,
BlockIdSupport, BodyRequired, BodySupport, CacheControlOption, CacheControlSupport,
ChunkSizeOption, ChunkSizeSupport, ClientRequestId, ClientRequestIdOption,
ClientRequestIdSupport, ContainerNameRequired, ContainerNameSupport, ContentDispositionOption,
ContentDispositionSupport, ContentEncodingOption, ContentEncodingSupport,
ContentLanguageOption, ContentLanguageSupport, ContentLengthOption, ContentLengthRequired,
ContentLengthSupport, ContentMD5Option, ContentMD5Support, ContentType, ContentTypeOption,
ContentTypeRequired, ContentTypeSupport, Continuation, ContinuationOption, ContinuationSupport,
DeleteSnapshotsMethod, DeleteSnapshotsMethodSupport, DelimiterOption, DelimiterSupport,
HttpClient, IfMatchConditionOption, IfMatchConditionSupport, IfModifiedSince,
IfModifiedSinceOption, IfModifiedSinceSupport, IfSinceConditionOption, IfSinceConditionSupport,
IfSourceMatchConditionOption, IfSourceMatchConditionSupport, IfSourceSinceConditionOption,
IfSourceSinceConditionSupport, IncludeCopyOption, IncludeCopySupport, IncludeDeletedOption,
Expand All @@ -21,12 +22,13 @@ pub use crate::{
IncludeUncommittedBlobsSupport, IsSynchronousOption, IsSynchronousSupport,
LeaseBreakPeriodOption, LeaseBreakPeriodRequired, LeaseBreakPeriodSupport,
LeaseDurationRequired, LeaseDurationSupport, LeaseIdOption, LeaseIdRequired, LeaseIdSupport,
MaxResultsOption, MaxResultsSupport, MetadataOption, MetadataSupport, NextMarkerOption,
NextMarkerSupport, PageBlobLengthRequired, PageBlobLengthSupport, PrefixOption, PrefixSupport,
ProposedLeaseIdOption, ProposedLeaseIdRequired, ProposedLeaseIdSupport, RangeOption,
RangeRequired, RangeSupport, SequenceNumberConditionOption, SequenceNumberConditionSupport,
SequenceNumberOption, SequenceNumberSupport, SnapshotOption, SnapshotRequired, SnapshotSupport,
MaxResultsOption, MaxResultsSupport, Metadata, MetadataOption, MetadataSupport,
NextMarkerOption, NextMarkerSupport, PageBlobLengthRequired, PageBlobLengthSupport,
PrefixOption, PrefixSupport, ProposedLeaseIdOption, ProposedLeaseIdRequired,
ProposedLeaseIdSupport, RangeOption, RangeRequired, RangeSupport,
SequenceNumberConditionOption, SequenceNumberConditionSupport, SequenceNumberOption,
SequenceNumberSupport, SnapshotOption, SnapshotRequired, SnapshotSupport,
SourceContentMD5Option, SourceContentMD5Support, SourceLeaseIdOption, SourceLeaseIdSupport,
SourceUrlRequired, SourceUrlSupport, StoredAccessPolicy, StoredAccessPolicyList, TimeoutOption,
TimeoutSupport, UserAgentSupport, EMPTY_BODY,
SourceUrlRequired, SourceUrlSupport, StoredAccessPolicy, StoredAccessPolicyList, Timeout,
TimeoutOption, TimeoutSupport, UserAgentSupport, EMPTY_BODY,
};
25 changes: 25 additions & 0 deletions sdk/core/src/timeout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::AppendToUrlQuery;
use std::time::Duration;

#[derive(Debug, Clone)]
pub struct Timeout(Duration);

impl Timeout {
pub fn new(duration: Duration) -> Self {
Self(duration)
}
}

impl AppendToUrlQuery for Timeout {
fn append_to_url_query(&self, url: &mut url::Url) {
url.query_pairs_mut()
.append_pair("timeout", &format!("{}", self.0.as_secs()));
}
}

impl AppendToUrlQuery for Option<Timeout> {
fn append_to_url_query(&self, url: &mut url::Url) {
self.as_ref()
.map(|timeout| AppendToUrlQuery::append_to_url_query(timeout, url));
}
}
20 changes: 16 additions & 4 deletions sdk/storage/examples/queue_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate log;
use azure_core::prelude::*;
use azure_storage::core::prelude::*;
use azure_storage::queue::prelude::*;
use std::collections::HashMap;
use std::error::Error;

#[tokio::main]
Expand All @@ -25,16 +24,29 @@ async fn main() -> Result<(), Box<dyn Error>> {

// this step is optional but here we show
// how to add metadata to a new queue.
let mut hm = HashMap::new();
hm.insert("source", "azure-sdk-for-rust");
let mut metadata = Metadata::new();
metadata
.as_mut()
MindFlavor marked this conversation as resolved.
Show resolved Hide resolved
.insert("source".to_owned(), "azure-sdk-for-rust".to_owned());

let response = queue_client
.create_queue()
.with_metadata(&hm)
.with_metadata(&metadata)
.execute()
.await?;
println!("response == {:#?}", response);

// let's add some more metadata
metadata
.as_mut()
.insert("version".to_owned(), "TBD".to_owned());
metadata
.as_mut()
.insert("date".to_owned(), "Stardate".to_owned());

let response = queue_client.set_queue_metadata(&metadata).execute().await?;
println!("response == {:#?}", response);

// now let's delete it
let response = queue_client
.delete_queue()
Expand Down
8 changes: 8 additions & 0 deletions sdk/storage/src/queue/clients/queue_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::queue::clients::QueueAccountClient;
use crate::queue::PopReceipt;
use crate::requests;
use crate::HasStorageClient;
use azure_core::Metadata;
use std::borrow::Cow;
use std::fmt::Debug;

Expand Down Expand Up @@ -78,4 +79,11 @@ where
pub fn clear_messages(&self) -> requests::ClearMessagesBuilder<'_, C> {
requests::ClearMessagesBuilder::new(self)
}

pub fn set_queue_metadata<'a>(
&'a self,
metadata: &'a Metadata,
) -> requests::SetQueueMetadataBuilder<'a, C> {
requests::SetQueueMetadataBuilder::new(self, metadata)
}
}
Loading