use crate::{
cosmos_request::CosmosRequest,
models::{CosmosResponse, ThroughputProperties},
operation_context::OperationType,
pipeline::GatewayPipeline,
resource_context::{ResourceLink, ResourceType},
Query, QueryFeedPage,
};
use azure_core::http::Context;
use futures::TryStreamExt;
use std::sync::Arc;
#[derive(Clone)]
pub(crate) struct OffersClient {
pipeline: Arc<GatewayPipeline>,
resource_id: String,
}
impl OffersClient {
pub(crate) fn new(pipeline: Arc<GatewayPipeline>, resource_id: String) -> Self {
Self {
pipeline,
resource_id,
}
}
pub(crate) async fn read(
&self,
context: Context<'_>,
) -> azure_core::Result<Option<ThroughputProperties>> {
let query = Query::from("SELECT * FROM c WHERE c.offerResourceId = @rid")
.with_parameter("@rid", &self.resource_id)?;
let offers_link = ResourceLink::root(ResourceType::Offers);
let executor = crate::query::executor::QueryExecutor::new(
self.pipeline.clone(),
offers_link.clone(),
context.into_owned(),
query,
azure_core::http::headers::Headers::new(),
);
let mut page_iter = executor.into_stream()?.into_pages();
let page: Option<QueryFeedPage<ThroughputProperties>> = page_iter.try_next().await?;
Ok(page.and_then(|p| p.into_items().into_iter().next()))
}
pub(crate) async fn replace(
&self,
context: Context<'_>,
throughput: ThroughputProperties,
) -> azure_core::Result<CosmosResponse<ThroughputProperties>> {
let response = self.read(context.clone()).await?;
let mut current_throughput = response.unwrap_or_default();
current_throughput.offer = throughput.offer;
let offer_link =
ResourceLink::root(ResourceType::Offers).item_by_rid(¤t_throughput.offer_id);
let cosmos_request = CosmosRequest::builder(OperationType::Replace, offer_link)
.json(current_throughput)
.build()?;
self.pipeline.send(cosmos_request, context).await
}
}