Skip to content

Even more into_future #667

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 12 commits into from
Mar 1, 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
11 changes: 4 additions & 7 deletions sdk/data_cosmos/examples/collection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use azure_core::prelude::*;
use azure_data_cosmos::prelude::*;
use futures::stream::StreamExt;
use std::error::Error;
Expand Down Expand Up @@ -60,12 +59,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

for db in databases.databases {
let database_client = client.clone().into_database_client(db.id.clone());
let collections = Box::pin(
database_client.list_collections(Context::new(), ListCollectionsOptions::new()),
)
.next()
.await
.unwrap()?;
let collections = Box::pin(database_client.list_collections().into_stream())
.next()
.await
.unwrap()?;
println!(
"database {} has {} collection(s)",
db.id,
Expand Down
3 changes: 1 addition & 2 deletions sdk/data_cosmos/examples/create_delete_database.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use azure_core::Context;
use azure_data_cosmos::prelude::*;
use futures::stream::StreamExt;
use std::error::Error;
Expand Down Expand Up @@ -61,7 +60,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let get_collection_response = db_collection.get_collection().into_future().await?;
println!("get_collection_response == {:#?}", get_collection_response);

let stream = db_client.list_collections(Context::new(), ListCollectionsOptions::new());
let stream = db_client.list_collections().into_stream();
let mut stream = Box::pin(stream);
while let Some(res) = stream.next().await {
let res = res?;
Expand Down
17 changes: 7 additions & 10 deletions sdk/data_cosmos/examples/database_00.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use azure_core::Context;
use azure_data_cosmos::prelude::*;
use futures::stream::StreamExt;
use serde_json::Value;
Expand All @@ -25,11 +24,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
println!("database == {:?}", db);
let database = client.clone().into_database_client(db.name().to_owned());

let collections =
Box::pin(database.list_collections(Context::new(), ListCollectionsOptions::new()))
.next()
.await
.unwrap()?;
let collections = Box::pin(database.list_collections().into_stream())
.next()
.await
.unwrap()?;
for collection in collections.collections {
println!("collection == {:?}", collection);
let collection_client = database.clone().into_collection_client(collection.id);
Expand Down Expand Up @@ -66,10 +64,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

println!("\nReplacing collection");
let replace_collection_response = collection_client
.replace_collection(
Context::new(),
ReplaceCollectionOptions::new("/age").indexing_policy(indexing_policy_new),
)
.replace_collection("/age")
.indexing_policy(indexing_policy_new)
.into_future()
.await?;
println!(
"replace_collection_response == {:#?}",
Expand Down
10 changes: 4 additions & 6 deletions sdk/data_cosmos/examples/database_01.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use azure_core::Context;
use azure_data_cosmos::prelude::*;
use futures::stream::StreamExt;
use std::error::Error;
Expand All @@ -18,11 +17,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let database_client = client.into_database_client("pollo");
println!("database_name == {}", database_client.database_name());

let collections =
Box::pin(database_client.list_collections(Context::new(), ListCollectionsOptions::new()))
.next()
.await
.unwrap()?;
let collections = Box::pin(database_client.list_collections().into_stream())
.next()
.await
.unwrap()?;
println!("collections == {:#?}", collections);

let collection_client = database_client.into_collection_client("cnt");
Expand Down
12 changes: 7 additions & 5 deletions sdk/data_cosmos/examples/document_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
client
.clone()
.into_database_client(database.id.clone())
.list_collections(Context::new(), ListCollectionsOptions::new()),
.list_collections()
.into_stream(),
)
.next()
.await
Expand Down Expand Up @@ -156,7 +157,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let get_document_response = collection_client
.clone()
.into_document_client(doc.id.clone(), &doc.id)?
.get_document::<MySampleStruct>(Context::new(), GetDocumentOptions::new())
.get_document()
.into_future::<MySampleStruct>()
.await?;
println!("get_document_response == {:#?}", get_document_response);

Expand All @@ -170,12 +172,12 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// the etag received in the previous get_document. The etag is an opaque value that
// changes every time the document is updated. If the passed etag is different in
// CosmosDB it means something else updated the document before us!
let options = ReplaceDocumentOptions::new()
.if_match_condition(IfMatchCondition::Match(document.etag));
let replace_document_response = collection_client
.clone()
.into_document_client(doc.id.clone(), &doc.id)?
.replace_document(Context::new(), &doc, options)
.replace_document(doc)
.if_match_condition(IfMatchCondition::Match(document.etag))
.into_future()
.await?;
println!(
"replace_document_response == {:#?}",
Expand Down
29 changes: 11 additions & 18 deletions sdk/data_cosmos/examples/document_entries_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let id = format!("unique_id{}", 3);
let partition_key = &id;

let response = client
let response: GetDocumentResponse<MySampleStruct> = client
.clone()
.into_document_client(id.clone(), partition_key)?
.get_document::<MySampleStruct>(
Context::new(),
GetDocumentOptions::new().consistency_level(session_token),
)
.get_document()
.into_future()
.await?;

assert!(matches!(response, GetDocumentResponse::Found(_)));
Expand All @@ -137,13 +135,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let replace_document_response = client
.clone()
.into_document_client(id.clone(), &id)?
.replace_document(
Context::new(),
&doc.document,
ReplaceDocumentOptions::new()
.consistency_level(ConsistencyLevel::from(&response))
.if_match_condition(IfMatchCondition::Match(doc.etag)), // use optimistic concurrency check
)
.replace_document(doc.document)
.consistency_level(ConsistencyLevel::from(&response))
.if_match_condition(IfMatchCondition::Match(doc.etag)) // use optimistic concurrency check
.into_future()
.await?;

println!(
Expand All @@ -155,14 +150,12 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// has_been_found == false
println!("\n\nLooking for non-existing item");
let id = format!("unique_id{}", 100);

let response = client
let response: GetDocumentResponse<MySampleStruct> = client
.clone()
.into_document_client(id.clone(), &id)?
.get_document::<MySampleStruct>(
Context::new(),
GetDocumentOptions::new().consistency_level(&response),
)
.get_document()
.consistency_level(&response)
.into_future()
.await?;

assert!(matches!(response, GetDocumentResponse::NotFound(_)));
Expand Down
23 changes: 9 additions & 14 deletions sdk/data_cosmos/examples/document_entries_01.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use azure_core::Context;
use azure_data_cosmos::prelude::*;
use serde::{Deserialize, Serialize};
use std::error::Error;
Expand Down Expand Up @@ -62,20 +61,18 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let get_document_response = client
.clone()
.into_document_client(doc.id.clone(), &doc.id)?
.get_document::<serde_json::Value>(
Context::new(),
GetDocumentOptions::new().consistency_level(&create_document_response),
)
.get_document()
.consistency_level(&create_document_response)
.into_future::<serde_json::Value>()
.await?;
println!("get_document_response == {:#?}", get_document_response);

let get_document_response = client
.clone()
.into_document_client("ciccia", &doc.id)?
.get_document::<serde_json::Value>(
Context::new(),
GetDocumentOptions::new().consistency_level(&create_document_response),
)
.get_document()
.consistency_level(&create_document_response)
.into_future::<serde_json::Value>()
.await?;
println!(
"get_document_response == {:#?}\n\n\n",
Expand Down Expand Up @@ -104,11 +101,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

let replace_document_response = client
.into_document_client(doc.id.clone(), &doc.id)?
.replace_document(
Context::new(),
&doc,
ReplaceDocumentOptions::new().consistency_level(&query_documents_response),
)
.replace_document(doc)
.consistency_level(&query_documents_response)
.into_future()
.await?;
println!(
"replace_document_response == {:#?}",
Expand Down
11 changes: 5 additions & 6 deletions sdk/data_cosmos/examples/permission_00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
);

let get_permission_response = permission_client
.get_permission(
Context::new(),
GetPermissionOptions::new().consistency_level(ConsistencyLevel::Session(
list_permissions_response.session_token,
)),
)
.get_permission()
.consistency_level(ConsistencyLevel::Session(
list_permissions_response.session_token,
))
.into_future()
.await
.unwrap();
println!("get_permission_response == {:#?}", get_permission_response);
Expand Down
8 changes: 3 additions & 5 deletions sdk/data_cosmos/examples/user_00.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use azure_core::Context;
use azure_data_cosmos::prelude::*;
use futures::stream::StreamExt;
use futures::StreamExt;
use std::error::Error;

#[tokio::main]
Expand Down Expand Up @@ -32,16 +32,14 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let create_user_response = user_client.create_user().into_future().await?;
println!("create_user_response == {:#?}", create_user_response);

let users = Box::pin(database_client.list_users(Context::new(), ListUsersOptions::new()))
let users = Box::pin(database_client.list_users().into_stream())
.next()
.await
.unwrap()?;

println!("list_users_response == {:#?}", users);

let get_user_response = user_client
.get_user(Context::new(), GetUserOptions::new())
.await?;
let get_user_response = user_client.get_user().into_future().await?;
println!("get_user_response == {:#?}", get_user_response);

let new_user = format!("{}replaced", user_name);
Expand Down
22 changes: 6 additions & 16 deletions sdk/data_cosmos/src/clients/collection_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use super::{DatabaseClient, UserDefinedFunctionClient};
use crate::clients::*;
use crate::operations::*;
use crate::requests;
use crate::resources::ResourceType;
use crate::resources::collection::PartitionKey;
use crate::CosmosEntity;
use crate::ReadonlyString;
use azure_core::{Context, HttpClient, Pipeline, Request};
use azure_core::{HttpClient, Pipeline, Request};
use serde::Serialize;

/// A client for Cosmos collection resources.
Expand Down Expand Up @@ -52,21 +52,11 @@ impl CollectionClient {
}

/// Replace a collection
pub async fn replace_collection(
pub fn replace_collection<P: Into<PartitionKey>>(
&self,
ctx: Context,
options: ReplaceCollectionOptions,
) -> crate::Result<ReplaceCollectionResponse> {
let mut request = self.prepare_request_with_collection_name(http::Method::PUT);

options.decorate_request(&mut request, self.collection_name())?;

let response = self
.pipeline()
.send(ctx.clone().insert(ResourceType::Collections), &mut request)
.await?;

Ok(ReplaceCollectionResponse::try_from(response).await?)
partition_key: P,
) -> ReplaceCollectionBuilder {
ReplaceCollectionBuilder::new(self.clone(), partition_key.into())
}

/// list documents in a collection
Expand Down
4 changes: 2 additions & 2 deletions sdk/data_cosmos/src/clients/cosmos_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ impl CosmosClient {
}

/// List all databases
pub fn list_databases(&self) -> ListDatabases {
ListDatabases::new(self.clone())
pub fn list_databases(&self) -> ListDatabasesBuilder {
ListDatabasesBuilder::new(self.clone())
}

/// Convert into a [`DatabaseClient`]
Expand Down
Loading