Skip to content

Commit

Permalink
[Cosmos] Remove options builders and (most) impl parameters (#1880)
Browse files Browse the repository at this point in the history
  • Loading branch information
analogrelay authored Oct 31, 2024
1 parent 9c1525c commit 9bc2dae
Show file tree
Hide file tree
Showing 22 changed files with 213 additions and 571 deletions.
8 changes: 4 additions & 4 deletions sdk/cosmos/azure_data_cosmos/examples/cosmos/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ impl CreateCommand {
partition_key,
json,
} => {
let db_client = client.database_client(database);
let container_client = db_client.container_client(container);
let db_client = client.database_client(&database);
let container_client = db_client.container_client(&container);

let pk = PartitionKey::from(&partition_key);
let item: serde_json::Value = serde_json::from_str(&json)?;
Expand All @@ -85,7 +85,7 @@ impl CreateCommand {

Subcommands::Database { id } => {
let db = client
.create_database(id, None)
.create_database(&id, None)
.await?
.deserialize_body()
.await?
Expand All @@ -110,7 +110,7 @@ impl CreateCommand {
},
};
let container = client
.database_client(database)
.database_client(&database)
.create_container(properties, None)
.await?
.deserialize_body()
Expand Down
12 changes: 6 additions & 6 deletions sdk/cosmos/azure_data_cosmos/examples/cosmos/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ impl DeleteCommand {
item_id,
partition_key,
} => {
let db_client = client.database_client(database);
let container_client = db_client.container_client(container);
let db_client = client.database_client(&database);
let container_client = db_client.container_client(&container);

let response = container_client
.delete_item(partition_key, item_id, None)
.delete_item(partition_key, &item_id, None)
.await;
match response {
Err(e) if e.http_status() == Some(StatusCode::NotFound) => {
Expand All @@ -72,14 +72,14 @@ impl DeleteCommand {
}

Subcommands::Database { id } => {
let db_client = client.database_client(id);
let db_client = client.database_client(&id);
db_client.delete(None).await?;
Ok(())
}

Subcommands::Container { database, id } => {
let db_client = client.database_client(database);
let container_client = db_client.container_client(id);
let db_client = client.database_client(&database);
let container_client = db_client.container_client(&id);
container_client.delete(None).await?;
Ok(())
}
Expand Down
10 changes: 7 additions & 3 deletions sdk/cosmos/azure_data_cosmos/examples/cosmos/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();

let args = ProgramArgs::parse();
let mut args = ProgramArgs::parse();

let Some(cmd) = args.subcommand else {
ProgramArgs::command().print_long_help()?;
return Ok(());
};

let client = create_client(&args.shared_args)?;
let client = create_client(&mut args.shared_args)?;

match cmd {
Subcommands::Create(cmd) => cmd.run(client).await,
Expand All @@ -78,7 +78,11 @@ fn create_client(args: &SharedArgs) -> Result<CosmosClient, Box<dyn Error>> {
if let Some(key) = args.key.as_ref() {
#[cfg(feature = "key_auth")]
{
Ok(CosmosClient::with_key(&args.endpoint, key.clone(), None)?)
Ok(CosmosClient::with_key(
&args.endpoint,
key.clone().into(),
None,
)?)
}
#[cfg(not(feature = "key_auth"))]
{
Expand Down
88 changes: 47 additions & 41 deletions sdk/cosmos/azure_data_cosmos/src/clients/container_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
DeleteContainerOptions, ItemOptions, PartitionKey, Query, QueryPartitionStrategy,
};

use azure_core::{Context, Method, Pager, Request, Response};
use azure_core::{Method, Pager, Request, Response};
use serde::{de::DeserializeOwned, Serialize};

/// A client for working with a specific container in a Cosmos DB account.
Expand Down Expand Up @@ -60,15 +60,16 @@ impl ContainerClient {
/// ```
pub async fn read(
&self,

#[allow(unused_variables)]
// REASON: This is a documented public API so prefixing with '_' is undesirable.
options: Option<ReadContainerOptions>,
options: Option<ReadContainerOptions<'_>>,
) -> azure_core::Result<Response<ContainerProperties>> {
let url = self.pipeline.url(&self.link);
let mut req = Request::new(url, Method::Get);
self.pipeline
.send(Context::new(), &mut req, self.link.clone())
.send(
options.map(|o| o.method_options.context),
&mut req,
self.link.clone(),
)
.await
}

Expand All @@ -80,14 +81,16 @@ impl ContainerClient {
/// * `options` - Optional parameters for the request.
pub async fn delete(
&self,
#[allow(unused_variables)]
// REASON: This is a documented public API so prefixing with '_' is undesirable.
options: Option<DeleteContainerOptions>,
options: Option<DeleteContainerOptions<'_>>,
) -> azure_core::Result<Response> {
let url = self.pipeline.url(&self.link);
let mut req = Request::new(url, Method::Delete);
self.pipeline
.send(Context::new(), &mut req, self.link.clone())
.send(
options.map(|o| o.method_options.context),
&mut req,
self.link.clone(),
)
.await
}

Expand Down Expand Up @@ -130,17 +133,18 @@ impl ContainerClient {
&self,
partition_key: impl Into<PartitionKey>,
item: T,

#[allow(unused_variables)]
// REASON: This is a documented public API so prefixing with '_' is undesirable.
options: Option<ItemOptions>,
options: Option<ItemOptions<'_>>,
) -> azure_core::Result<Response<Item<T>>> {
let url = self.pipeline.url(&self.items_link);
let mut req = Request::new(url, Method::Post);
req.insert_headers(&partition_key.into())?;
req.set_json(&item)?;
self.pipeline
.send(Context::new(), &mut req, self.items_link.clone())
.send(
options.map(|o| o.method_options.context),
&mut req,
self.items_link.clone(),
)
.await
}

Expand Down Expand Up @@ -183,19 +187,21 @@ impl ContainerClient {
pub async fn replace_item<T: Serialize>(
&self,
partition_key: impl Into<PartitionKey>,
item_id: impl AsRef<str>,
item_id: &str,
item: T,

#[allow(unused_variables)]
// REASON: This is a documented public API so prefixing with '_' is undesirable.
options: Option<ItemOptions>,
options: Option<ItemOptions<'_>>,
) -> azure_core::Result<Response<Item<T>>> {
let link = self.items_link.item(item_id);
let url = self.pipeline.url(&link);
let mut req = Request::new(url, Method::Put);
req.insert_headers(&partition_key.into())?;
req.set_json(&item)?;
self.pipeline.send(Context::new(), &mut req, link).await
self.pipeline
.send(options.map(|o| o.method_options.context), &mut req, link)
.await
}

/// Creates or replaces an item in the container.
Expand Down Expand Up @@ -240,18 +246,19 @@ impl ContainerClient {
&self,
partition_key: impl Into<PartitionKey>,
item: T,

#[allow(unused_variables)]
// REASON: This is a documented public API so prefixing with '_' is undesirable.
options: Option<ItemOptions>,
options: Option<ItemOptions<'_>>,
) -> azure_core::Result<Response<Item<T>>> {
let url = self.pipeline.url(&self.items_link);
let mut req = Request::new(url, Method::Post);
req.insert_header(constants::IS_UPSERT, "true");
req.insert_headers(&partition_key.into())?;
req.set_json(&item)?;
self.pipeline
.send(Context::new(), &mut req, self.items_link.clone())
.send(
options.map(|o| o.method_options.context),
&mut req,
self.items_link.clone(),
)
.await
}

Expand Down Expand Up @@ -288,17 +295,16 @@ impl ContainerClient {
pub async fn read_item<T: DeserializeOwned>(
&self,
partition_key: impl Into<PartitionKey>,
item_id: impl AsRef<str>,

#[allow(unused_variables)]
// REASON: This is a documented public API so prefixing with '_' is undesirable.
options: Option<ItemOptions>,
item_id: &str,
options: Option<ItemOptions<'_>>,
) -> azure_core::Result<Response<Item<T>>> {
let link = self.items_link.item(item_id);
let url = self.pipeline.url(&link);
let mut req = Request::new(url, Method::Get);
req.insert_headers(&partition_key.into())?;
self.pipeline.send(Context::new(), &mut req, link).await
self.pipeline
.send(options.map(|o| o.method_options.context), &mut req, link)
.await
}

/// Deletes an item from the container.
Expand All @@ -323,17 +329,16 @@ impl ContainerClient {
pub async fn delete_item(
&self,
partition_key: impl Into<PartitionKey>,
item_id: impl AsRef<str>,

#[allow(unused_variables)]
// REASON: This is a documented public API so prefixing with '_' is undesirable.
options: Option<ItemOptions>,
item_id: &str,
options: Option<ItemOptions<'_>>,
) -> azure_core::Result<Response> {
let link = self.items_link.item(item_id);
let url = self.pipeline.url(&link);
let mut req = Request::new(url, Method::Delete);
req.insert_headers(&partition_key.into())?;
self.pipeline.send(Context::new(), &mut req, link).await
self.pipeline
.send(options.map(|o| o.method_options.context), &mut req, link)
.await
}

/// Executes a single-partition query against items in the container.
Expand Down Expand Up @@ -394,17 +399,18 @@ impl ContainerClient {
&self,
query: impl Into<Query>,
partition_key: impl Into<QueryPartitionStrategy>,

#[allow(unused_variables)]
// REASON: This is a documented public API so prefixing with '_' is undesirable.
options: Option<QueryOptions>,
options: Option<QueryOptions<'_>>,
) -> azure_core::Result<Pager<QueryResults<T>>> {
let url = self.pipeline.url(&self.items_link);
let mut base_request = Request::new(url, Method::Post);
let QueryPartitionStrategy::SinglePartition(partition_key) = partition_key.into();
base_request.insert_headers(&partition_key)?;

self.pipeline
.send_query_request(query.into(), base_request, self.items_link.clone())
self.pipeline.send_query_request(
options.map(|o| o.method_options.context),
query.into(),
base_request,
self.items_link.clone(),
)
}
}
Loading

0 comments on commit 9bc2dae

Please sign in to comment.