Skip to content

More into_future usage #663

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

Merged
merged 13 commits into from
Feb 25, 2022
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
1 change: 1 addition & 0 deletions sdk/data_cosmos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ stop-token = { version = "0.7.0", features = ["tokio"] }
[features]
test_e2e = []
mock_transport_framework = [ "azure_core/mock_transport_framework"]
into_future = []
32 changes: 11 additions & 21 deletions sdk/data_cosmos/examples/attachments_00.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
use azure_core::Context;
use azure_data_cosmos::prelude::*;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::error::Error;

// Now we create a sample struct. The Cow trick
// allows us to use the same struct for serializing
// (without having to own the items if not needed) and
// for deserializing (where owning is required).
// We do not need to define the "id" field here, it will be
// specified in the Document struct below.
// Now we create a sample struct.
#[derive(Serialize, Deserialize, Clone, Debug)]
struct MySampleStruct<'a> {
id: Cow<'a, str>,
a_string: Cow<'a, str>,
struct MySampleStruct {
id: String,
a_string: String,
a_number: u64,
a_timestamp: i64,
}

impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
type Entity = &'a str;
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
type Entity = String;

fn partition_key(&'a self) -> Self::Entity {
self.id.as_ref()
fn partition_key(&self) -> Self::Entity {
self.id.clone().into()
}
}

Expand Down Expand Up @@ -50,17 +43,14 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let id = format!("unique_id{}", 100);

let doc = MySampleStruct {
id: Cow::Borrowed(&id),
a_string: Cow::Borrowed("Something here"),
id,
a_string: "Something here".into(),
a_number: 100,
a_timestamp: chrono::Utc::now().timestamp(),
};

// let's add an entity.
match client
.create_document(Context::new(), &doc, CreateDocumentOptions::new())
.await
{
match client.create_document(doc.clone()).into_future().await {
Ok(_) => {
println!("document created");
}
Expand Down
15 changes: 5 additions & 10 deletions sdk/data_cosmos/examples/create_delete_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// create collection!
{
let db_client = client.clone().into_database_client(database_name.clone());

let create_collection_response = db_client
.create_collection(
Context::new(),
"panzadoro",
CreateCollectionOptions::new("/id"),
)
.create_collection("panzadoro", "/id")
.into_future()
.await?;

println!(
Expand All @@ -74,15 +70,14 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
println!("res == {:#?}", res);
}

let delete_response = db_collection
.delete_collection(Context::new(), DeleteCollectionOptions::new())
.await?;
let delete_response = db_collection.delete_collection().into_future().await?;
println!("collection deleted: {:#?}", delete_response);
}

let resp = client
.into_database_client(database_name)
.delete_database(Context::new(), DeleteDatabaseOptions::new())
.delete_database()
.into_future()
.await?;
println!("database deleted. resp == {:#?}", resp);

Expand Down
9 changes: 4 additions & 5 deletions sdk/data_cosmos/examples/database_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
}"#;
let document: Value = serde_json::from_str(data)?;

let options = CreateDocumentOptions::new()
.is_upsert(true)
.partition_key(&43u32)
.unwrap();
let resp = collection_client
.create_document(Context::new(), &document, options)
.create_document(document)
.is_upsert(true)
.partition_key(&43u32)?
.into_future()
.await?;

println!("resp == {:?}", resp);
Expand Down
35 changes: 17 additions & 18 deletions sdk/data_cosmos/examples/document_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ use serde::{Deserialize, Serialize};
// DB.
use azure_core::prelude::*;
use azure_data_cosmos::prelude::*;
use std::borrow::Cow;
use std::error::Error;

#[derive(Clone, Serialize, Deserialize, Debug)]
struct MySampleStruct<'a> {
id: Cow<'a, str>,
a_string: Cow<'a, str>,
struct MySampleStruct {
id: String,
a_string: String,
a_number: u64,
a_timestamp: i64,
}

impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
type Entity = &'a str;
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
type Entity = String;

fn partition_key(&'a self) -> Self::Entity {
self.id.as_ref()
fn partition_key(&self) -> Self::Entity {
self.id.clone()
}
}

Expand Down Expand Up @@ -102,11 +101,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
client
.clone()
.into_database_client(database.id.clone())
.create_collection(
Context::new(),
COLLECTION,
CreateCollectionOptions::new("/id"),
)
.create_collection(COLLECTION, "/id")
.into_future()
.await?
.collection
}
Expand All @@ -118,8 +114,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// data in them. Let's create a Document. The only constraint
// is that we need an id and an arbitrary, Serializable type.
let doc = MySampleStruct {
id: Cow::Owned("unique_id100".to_owned()),
a_string: Cow::Borrowed("Something here"),
id: "unique_id100".into(),
a_string: "Something here".into(),
a_number: 100,
a_timestamp: chrono::Utc::now().timestamp(),
};
Expand All @@ -135,7 +131,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// the document attributes.

let create_document_response = collection_client
.create_document(Context::new(), &doc, CreateDocumentOptions::new())
.create_document(doc.clone())
.into_future()
.await?;
println!(
"create_document_response == {:#?}",
Expand Down Expand Up @@ -191,14 +188,16 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.clone()
.into_database_client(DATABASE.to_owned())
.into_collection_client(COLLECTION.to_owned())
.delete_collection(Context::new(), DeleteCollectionOptions::new())
.delete_collection()
.into_future()
.await?;
println!("collection deleted");

// And then we delete the database.
client
.into_database_client(database.id)
.delete_database(Context::new(), DeleteDatabaseOptions::new())
.delete_database()
.into_future()
.await?;
println!("database deleted");

Expand Down
38 changes: 13 additions & 25 deletions sdk/data_cosmos/examples/document_entries_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,22 @@ use azure_core::prelude::*;
use azure_data_cosmos::prelude::*;
use futures::stream::StreamExt;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::error::Error;

// Now we create a sample struct. The Cow trick
// allows us to use the same struct for serializing
// (without having to own the items if not needed) and
// for deserializing (where owning is required).
// We do not need to define the "id" field here, it will be
// specified in the Document struct below.
// Now we create a sample struct.
#[derive(Serialize, Deserialize, Clone, Debug)]
struct MySampleStruct<'a> {
id: Cow<'a, str>,
a_string: Cow<'a, str>,
struct MySampleStruct {
id: String,
a_string: String,
a_number: u64,
a_timestamp: i64,
}

impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
type Entity = &'a str;
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
type Entity = String;

fn partition_key(&'a self) -> Self::Entity {
self.id.as_ref()
fn partition_key(&self) -> Self::Entity {
self.id.clone()
}
}

Expand Down Expand Up @@ -51,18 +45,14 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut response = None;
for i in 0u64..5 {
let doc = MySampleStruct {
id: Cow::Owned(format!("unique_id{}", i)),
a_string: Cow::Borrowed("Something here"),
id: format!("unique_id{}", i),
a_string: "Something here".into(),
a_number: i,
a_timestamp: chrono::Utc::now().timestamp(),
};

// let's add an entity.
response = Some(
client
.create_document(Context::new(), &doc, CreateDocumentOptions::new())
.await?,
);
response = Some(client.create_document(doc.clone()).into_future().await?);
}

println!("Created 5 documents.");
Expand Down Expand Up @@ -183,10 +173,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
client
.clone()
.into_document_client(id.clone(), &id)?
.delete_document(
Context::new(),
DeleteDocumentOptions::new().consistency_level(&response),
)
.delete_document()
.into_future()
.await?;
}
println!("Cleaned up");
Expand Down
27 changes: 12 additions & 15 deletions sdk/data_cosmos/examples/document_entries_01.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use azure_core::Context;
use azure_data_cosmos::prelude::*;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::error::Error;

#[derive(Serialize, Deserialize, Clone, Debug)]
struct MySampleStruct<'a> {
id: Cow<'a, str>,
a_string: Cow<'a, str>,
struct MySampleStruct {
id: String,
a_string: String,
a_number: u64,
a_timestamp: i64,
}

impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
type Entity = &'a str;
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
type Entity = String;

fn partition_key(&'a self) -> Self::Entity {
self.id.as_ref()
fn partition_key(&self) -> Self::Entity {
self.id.clone()
}
}

Expand All @@ -42,19 +41,17 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let client = client.into_collection_client(collection_name);

let mut doc = MySampleStruct {
id: Cow::Owned(format!("unique_id{}", 500)),
a_string: Cow::Borrowed("Something here"),
id: format!("unique_id{}", 500),
a_string: "Something here".into(),
a_number: 600,
a_timestamp: chrono::Utc::now().timestamp(),
};

// let's add an entity.
let create_document_response = client
.create_document(
Context::new(),
&doc,
CreateDocumentOptions::new().is_upsert(true),
)
.create_document(doc.clone())
.is_upsert(true)
.into_future()
.await?;

println!(
Expand Down
34 changes: 13 additions & 21 deletions sdk/data_cosmos/examples/permission_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,18 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
get_collection2_response
);

let create_user_response = user_client
.create_user(Context::new(), CreateUserOptions::default())
.await?;
let create_user_response = user_client.create_user().into_future().await?;
println!("create_user_response == {:#?}", create_user_response);

// create the first permission!
let permission_client = user_client.clone().into_permission_client("matrix");
let permission_mode = get_collection_response.collection.read_permission();

let create_permission_response = permission_client
.create_permission(
Context::new(),
CreatePermissionOptions::new()
.consistency_level(&create_user_response)
.expiry_seconds(18000u64),
&permission_mode,
)
.create_permission(permission_mode)
.consistency_level(&create_user_response)
.expiry_seconds(18000u64)
.into_future()
.await
.unwrap();
println!(
Expand All @@ -87,11 +82,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let permission_mode = get_collection2_response.collection.all_permission();

let create_permission2_response = permission_client
.create_permission(
Context::new(),
CreatePermissionOptions::new().consistency_level(&create_user_response),
&permission_mode,
)
.create_permission(permission_mode)
.consistency_level(&create_user_response)
.into_future()
.await
.unwrap();

Expand Down Expand Up @@ -145,12 +138,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
);

let delete_permission_response = permission_client
.delete_permission(
Context::new(),
DeletePermissionOptions::new().consistency_level(ConsistencyLevel::Session(
replace_permission_response.session_token,
)),
)
.delete_permission()
.consistency_level(ConsistencyLevel::Session(
replace_permission_response.session_token,
))
.into_future()
.await
.unwrap();

Expand Down
Loading