Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #184 from MindFlavor/cosmos_rewrite/pr
Browse files Browse the repository at this point in the history
Complete CosmosDB crate rewrite
  • Loading branch information
Francesco Cogno authored Jan 5, 2020
2 parents ac5d21a + c2b1812 commit 7b459e2
Show file tree
Hide file tree
Showing 98 changed files with 8,777 additions and 2,757 deletions.
315 changes: 111 additions & 204 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion azure_sdk_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "azure_sdk_core"
version = "0.30.1"
version = "0.30.3"
description = "Rust wrappers around Microsoft Azure REST APIs - Core crate"
readme = "README.md"
authors = ["Francesco Cogno <[email protected]>", "Max Gortman <[email protected]>", "Dong Liu <[email protected]>"]
Expand Down
56 changes: 38 additions & 18 deletions azure_sdk_core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,27 @@ quick_error! {

#[derive(Debug, Clone, PartialEq)]
pub struct UnexpectedHTTPResult {
expected: StatusCode,
expected: Vec<StatusCode>,
received: StatusCode,
body: String,
}

impl UnexpectedHTTPResult {
pub fn new(expected: StatusCode, received: StatusCode, body: &str) -> UnexpectedHTTPResult {
UnexpectedHTTPResult {
expected,
expected: vec![expected],
received,
body: body.to_owned(),
}
}

pub fn new_multiple(
allowed: Vec<StatusCode>,
received: StatusCode,
body: &str,
) -> UnexpectedHTTPResult {
UnexpectedHTTPResult {
expected: allowed,
received,
body: body.to_owned(),
}
Expand All @@ -61,7 +73,7 @@ impl std::fmt::Display for UnexpectedHTTPResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Unexpected HTTP result (expected: {}, received: {})",
"Unexpected HTTP result (expected: {:?}, received: {:?})",
self.expected, self.received
)
}
Expand Down Expand Up @@ -180,6 +192,11 @@ quick_error! {
display("Parse int error: {}", err)
cause(err)
}
ParseFloatError(err: std::num::ParseFloatError) {
from()
display("Parse float error: {}", err)
cause(err)
}
ParseError(err: ParseError){
from()
display("Parse error")
Expand Down Expand Up @@ -238,6 +255,9 @@ quick_error! {
MissingHeaderError(header: String) {
display("A required header is missing: {}", header)
}
MissingValueError(value: String, expected_type: String) {
display("An expected JSON node is missing: {} of expected type {}", value, expected_type)
}
}
}

Expand Down Expand Up @@ -308,11 +328,11 @@ pub async fn check_status_extract_headers_and_body(
if status == expected_status_code {
Ok((headers, body))
} else {
Err(AzureError::UnexpectedHTTPResult(UnexpectedHTTPResult {
expected: expected_status_code,
received: status,
body: str::from_utf8(&body)?.to_owned(),
}))
Err(AzureError::UnexpectedHTTPResult(UnexpectedHTTPResult::new(
expected_status_code,
status,
str::from_utf8(&body)?,
)))
}
}

Expand Down Expand Up @@ -345,11 +365,11 @@ pub async fn check_status_extract_body(
if status == expected_status_code {
Ok(body)
} else {
Err(AzureError::UnexpectedHTTPResult(UnexpectedHTTPResult {
expected: expected_status_code,
received: status,
body,
}))
Err(AzureError::UnexpectedHTTPResult(UnexpectedHTTPResult::new(
expected_status_code,
status,
&body,
)))
}
}

Expand All @@ -363,11 +383,11 @@ pub async fn check_status_extract_body_2(
let s = String::from_utf8(body.to_vec())?;
debug!("body: {}", s);
if received_status != expected_status {
Err(AzureError::UnexpectedHTTPResult(UnexpectedHTTPResult {
expected: expected_status,
received: received_status,
body: s,
}))
Err(AzureError::UnexpectedHTTPResult(UnexpectedHTTPResult::new(
expected_status,
received_status,
&s,
)))
} else {
Ok(s)
}
Expand Down
3 changes: 3 additions & 0 deletions azure_sdk_core/src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ pub const ACCOUNT_KIND: &str = "x-ms-account-kind";
pub const APPEND_POSITION: &str = "x-ms-blob-condition-appendpos";
pub const CACHE_CONTROL: &str = "x-ms-blob-cache-control";
pub const CONTENT_DISPOSITION: &str = "x-ms-blob-content-disposition";
pub const ACTIVITY_ID: &str = "x-ms-activity-id";
pub const HEADER_CONTINUATION: &str = "x-ms-continuation";
pub const SESSION_TOKEN: &str = "x-ms-session-token";
77 changes: 71 additions & 6 deletions azure_sdk_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![recursion_limit = "128"]
#![recursion_limit = "256"]
#![warn(rust_2018_idioms)]

#[macro_use]
Expand Down Expand Up @@ -26,17 +26,19 @@ use std::str::FromStr;
pub mod headers;
pub mod range;
use self::headers::{
ACCOUNT_KIND, APPEND_POSITION, BLOB_ACCESS_TIER, BLOB_CONTENT_LENGTH, BLOB_SEQUENCE_NUMBER,
CACHE_CONTROL, CLIENT_REQUEST_ID, CONTENT_DISPOSITION, CONTENT_MD5, DELETE_SNAPSHOTS,
DELETE_TYPE_PERMANENT, LEASE_BREAK_PERIOD, LEASE_DURATION, LEASE_ID, LEASE_TIME,
PROPOSED_LEASE_ID, REQUEST_ID, REQUEST_SERVER_ENCRYPTED, SKU_NAME,
ACCOUNT_KIND, ACTIVITY_ID, APPEND_POSITION, BLOB_ACCESS_TIER, BLOB_CONTENT_LENGTH,
BLOB_SEQUENCE_NUMBER, CACHE_CONTROL, CLIENT_REQUEST_ID, CONTENT_DISPOSITION, CONTENT_MD5,
DELETE_SNAPSHOTS, DELETE_TYPE_PERMANENT, HEADER_CONTINUATION, LEASE_BREAK_PERIOD,
LEASE_DURATION, LEASE_ID, LEASE_TIME, PROPOSED_LEASE_ID, REQUEST_ID, REQUEST_SERVER_ENCRYPTED,
SESSION_TOKEN, SKU_NAME,
};
use hyper::header::{
HeaderName, CONTENT_ENCODING, CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_TYPE, DATE, ETAG,
LAST_MODIFIED, RANGE,
IF_MODIFIED_SINCE, LAST_MODIFIED, RANGE, USER_AGENT,
};
use uuid::Uuid;
pub type RequestId = Uuid;
pub type SessionToken = String;
use crate::errors::{check_status_extract_body_2, AzureError, TraversingError};
use crate::lease::LeaseId;
use crate::parsing::FromStringOptional;
Expand Down Expand Up @@ -219,6 +221,51 @@ pub trait ContentTypeOption<'a> {
}
}

pub trait IfModifiedSinceSupport<'a> {
type O;
fn with_if_modified_since(self, if_modified_since: &'a DateTime<Utc>) -> Self::O;
}

pub trait IfModifiedSinceOption<'a> {
fn if_modified_since(&self) -> Option<&'a DateTime<Utc>>;

fn add_header(&self, builder: &mut Builder) {
if let Some(if_modified_since) = self.if_modified_since() {
builder.header(IF_MODIFIED_SINCE, if_modified_since.to_rfc2822());
}
}
}

pub trait UserAgentSupport<'a> {
type O;
fn with_user_agent(self, user_agent: &'a str) -> Self::O;
}

pub trait UserAgentOption<'a> {
fn user_agent(&self) -> Option<&'a str>;

fn add_header(&self, builder: &mut Builder) {
if let Some(user_agent) = self.user_agent() {
builder.header(USER_AGENT, user_agent);
}
}
}

pub trait ActivityIdSupport<'a> {
type O;
fn with_activity_id(self, activity_id: &'a str) -> Self::O;
}

pub trait ActivityIdOption<'a> {
fn activity_id(&self) -> Option<&'a str>;

fn add_header(&self, builder: &mut Builder) {
if let Some(activity_id) = self.activity_id() {
builder.header(ACTIVITY_ID, activity_id);
}
}
}

pub trait ContentLanguageSupport<'a> {
type O;
fn with_content_language(self, content_language: &'a str) -> Self::O;
Expand Down Expand Up @@ -816,6 +863,16 @@ pub fn last_modified_from_headers(headers: &HeaderMap) -> Result<DateTime<Utc>,
Ok(last_modified)
}

pub fn continuation_token_from_headers_optional(
headers: &HeaderMap,
) -> Result<Option<String>, AzureError> {
if let Some(hc) = headers.get(HEADER_CONTINUATION) {
Ok(Some(hc.to_str()?.to_owned()))
} else {
Ok(None)
}
}

pub fn date_from_headers(headers: &HeaderMap) -> Result<DateTime<Utc>, AzureError> {
let date = headers
.get(DATE)
Expand Down Expand Up @@ -907,6 +964,14 @@ pub fn sequence_number_from_headers(headers: &HeaderMap) -> Result<u64, AzureErr
Ok(sequence_number)
}

pub fn session_token_from_headers(headers: &HeaderMap) -> Result<SessionToken, AzureError> {
Ok(headers
.get(SESSION_TOKEN)
.ok_or_else(|| AzureError::HeaderNotFound(SESSION_TOKEN.to_owned()))?
.to_str()?
.to_owned())
}

pub fn request_server_encrypted_from_headers(headers: &HeaderMap) -> Result<bool, AzureError> {
let request_server_encrypted = headers
.get(REQUEST_SERVER_ENCRYPTED)
Expand Down
39 changes: 23 additions & 16 deletions azure_sdk_core/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
pub use crate::ba512_range::BA512Range;
pub use crate::modify_conditions::SequenceNumberCondition;
pub use crate::modify_conditions::{IfMatchCondition, SequenceNumberCondition};
pub use crate::range::Range;
pub use crate::{
AccessTierOption, AccessTierSupport, AppendPositionOption, AppendPositionSupport, BA512RangeOption, BA512RangeRequired,
BA512RangeSupport, BlobNameRequired, BlobNameSupport, BlockIdRequired, BlockIdSupport, BodyRequired, BodySupport, CacheControlOption,
CacheControlSupport, ClientRequestIdOption, ClientRequestIdSupport, ContainerNameRequired, ContainerNameSupport,
ContentDispositionOption, ContentDispositionSupport, ContentEncodingOption, ContentEncodingSupport, ContentLanguageOption,
ContentLanguageSupport, ContentLengthOption, ContentLengthRequired, ContentLengthSupport, ContentMD5Option, ContentMD5Support,
ContentTypeOption, ContentTypeSupport, DeleteSnapshotsMethod, DeleteSnapshotsMethodSupport, DelimiterOption, DelimiterSupport,
IfMatchConditionOption, IfMatchConditionSupport, IfSinceConditionOption, IfSinceConditionSupport, IncludeCopyOption,
IncludeCopySupport, IncludeDeletedOption, IncludeDeletedSupport, IncludeListOptions, IncludeMetadataOption, IncludeMetadataSupport,
IncludeSnapshotsOption, IncludeSnapshotsSupport, IncludeUncommittedBlobsOption, IncludeUncommittedBlobsSupport, LeaseBreakPeriodOption,
LeaseBreakPeriodRequired, LeaseBreakPeriodSupport, LeaseDurationRequired, LeaseDurationSupport, LeaseIdOption, LeaseIdRequired,
LeaseIdSupport, MaxResultsOption, MaxResultsSupport, MetadataOption, MetadataSupport, NextMarkerOption, NextMarkerSupport,
PageBlobLengthRequired, PageBlobLengthSupport, PrefixOption, PrefixSupport, ProposedLeaseIdOption, ProposedLeaseIdRequired,
ProposedLeaseIdSupport, RangeOption, RangeSupport, SequenceNumberConditionOption, SequenceNumberConditionSupport, SequenceNumberOption,
SequenceNumberSupport, SnapshotOption, SnapshotRequired, SnapshotSupport, StoredAccessPolicy, StoredAccessPolicyList, TimeoutOption,
TimeoutSupport,
AccessTierOption, AccessTierSupport, ActivityIdOption, ActivityIdSupport, AppendPositionOption,
AppendPositionSupport, BA512RangeOption, BA512RangeRequired, BA512RangeSupport,
BlobNameRequired, BlobNameSupport, BlockIdRequired, BlockIdSupport, BodyRequired, BodySupport,
CacheControlOption, CacheControlSupport, ClientRequestIdOption, ClientRequestIdSupport,
ContainerNameRequired, ContainerNameSupport, ContentDispositionOption,
ContentDispositionSupport, ContentEncodingOption, ContentEncodingSupport,
ContentLanguageOption, ContentLanguageSupport, ContentLengthOption, ContentLengthRequired,
ContentLengthSupport, ContentMD5Option, ContentMD5Support, ContentTypeOption,
ContentTypeSupport, DeleteSnapshotsMethod, DeleteSnapshotsMethodSupport, DelimiterOption,
DelimiterSupport, IfMatchConditionOption, IfMatchConditionSupport, IfModifiedSinceOption,
IfModifiedSinceSupport, IfSinceConditionOption, IfSinceConditionSupport, IncludeCopyOption,
IncludeCopySupport, IncludeDeletedOption, IncludeDeletedSupport, IncludeListOptions,
IncludeMetadataOption, IncludeMetadataSupport, IncludeSnapshotsOption, IncludeSnapshotsSupport,
IncludeUncommittedBlobsOption, IncludeUncommittedBlobsSupport, LeaseBreakPeriodOption,
LeaseBreakPeriodRequired, LeaseBreakPeriodSupport, LeaseDurationRequired, LeaseDurationSupport,
LeaseIdOption, LeaseIdRequired, LeaseIdSupport, MaxResultsOption, MaxResultsSupport,
MetadataOption, MetadataSupport, NextMarkerOption, NextMarkerSupport, PageBlobLengthRequired,
PageBlobLengthSupport, PrefixOption, PrefixSupport, ProposedLeaseIdOption,
ProposedLeaseIdRequired, ProposedLeaseIdSupport, RangeOption, RangeSupport,
SequenceNumberConditionOption, SequenceNumberConditionSupport, SequenceNumberOption,
SequenceNumberSupport, SnapshotOption, SnapshotRequired, SnapshotSupport, StoredAccessPolicy,
StoredAccessPolicyList, TimeoutOption, TimeoutSupport, UserAgentOption, UserAgentSupport,
};
8 changes: 5 additions & 3 deletions azure_sdk_cosmos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "azure_sdk_cosmos"
version = "0.30.0"
version = "0.32.0"
description = "Rust wrappers around Microsoft Azure REST APIs - Azure Cosmos DB crate"
readme = "README.md"
authors = ["Francesco Cogno <[email protected]>", "Max Gortman <[email protected]>"]
Expand All @@ -15,7 +15,7 @@ categories = ["api-bindings"]
edition = "2018"

[dependencies]
azure_sdk_core = { path = "../azure_sdk_core", version = "0.30.0" }
azure_sdk_core = { path = "../azure_sdk_core", version = "0.30.1" }
ring = "0.16"
md5 = "0.7"
RustyXML = "0.1"
Expand All @@ -31,7 +31,7 @@ hyper = { version = "0.13.0-alpha.4" , features = ["u
log = "0.4"
mime = "0.3"
quick-error = "1.2"
serde = "1.0"
serde = { version = "1.0" }
serde_derive = "1.0"
serde_json = "1.0"
serde-xml-rs = "0.3"
Expand All @@ -41,11 +41,13 @@ uuid = { version = "0.8", features = ["v
smallvec = { version = "1.0" , features = ["serde"] }
bytes = "0.5"
hyper-rustls = { version = "0.19.0-alpha.3" , features = [] }
futures = "0.3"

[dev-dependencies]
futures-executor-preview = "0.3.0-alpha.19"
tokio = "0.2.0-alpha.6"
futures-util = "0.3.1"
serde = { version = "1.0", features = ["derive"] }

[features]
test_e2e = []
44 changes: 38 additions & 6 deletions azure_sdk_cosmos/examples/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,50 @@ async fn main() -> Result<(), Box<dyn Error>> {
// The Cosmos' client exposes a lot of methods. This one lists the databases in the specified
// account. Database do not implement Display but deref to &str so you can pass it to methods
// both as struct or id.
let databases = client.list_databases().await?;
let databases = client.list_databases().execute().await?;

println!("Account {} has {} database(s)", account, databases.len());
println!(
"Account {} has {} database(s)",
account,
databases.databases.len()
);

// try get on the first database (if any)
if let Some(db) = databases.databases.first() {
println!("getting info of database {}", &db.id);
let db = client
.with_database(&db.id)
.get_database()
.execute()
.await?;
println!("db {} found == {:?}", &db.database.id, &db);
}

// Each Cosmos' database contains one or more collections. We can enumerate them using the
// list_collection method.
for db in databases {
let collections = client.list_collections(&db.id).await?;
println!("database {} has {} collection(s)", db.id, collections.len());
for db in databases.databases {
let collections = client
.with_database(&db.id)
.list_collections()
.execute()
.await?;
println!(
"database {} has {} collection(s)",
db.id,
collections.collections.len()
);

for collection in collections {
for collection in collections.collections {
println!("\tcollection {}", collection.id);

let collection_response = client
.with_database(&db.id)
.with_collection(&collection.id)
.get_collection()
.execute()
.await?;

println!("\tcollection_response {:?}", collection_response);
}
}

Expand Down
Loading

0 comments on commit 7b459e2

Please sign in to comment.