use crate::prelude::*;
use crate::responses::ListStoredProceduresResponse;
use crate::ResourceType;
use azure_sdk_core::errors::{check_status_extract_headers_and_body, AzureError};
use azure_sdk_core::prelude::*;
use futures::stream::{unfold, Stream};
use hyper::StatusCode;
use std::convert::TryInto;
#[derive(Debug)]
pub struct ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
collection_client: &'a dyn CollectionClient<C, D>,
user_agent: Option<&'b str>,
activity_id: Option<&'b str>,
consistency_level: Option<ConsistencyLevel<'b>>,
continuation: Option<&'b str>,
max_item_count: i32,
}
impl<'a, 'b, C, D> ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
#[inline]
pub(crate) fn new(
collection_client: &'a dyn CollectionClient<C, D>,
) -> ListStoredProceduresBuilder<'a, 'b, C, D> {
ListStoredProceduresBuilder {
collection_client,
user_agent: None,
activity_id: None,
consistency_level: None,
continuation: None,
max_item_count: -1,
}
}
}
impl<'a, 'b, C, D> Clone for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
fn clone(&self) -> Self {
Self {
collection_client: self.collection_client,
user_agent: self.user_agent,
activity_id: self.activity_id,
consistency_level: self.consistency_level.clone(),
continuation: self.continuation,
max_item_count: self.max_item_count,
}
}
}
impl<'a, 'b, C, D> CollectionClientRequired<'a, C, D> for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
#[inline]
fn collection_client(&self) -> &'a dyn CollectionClient<C, D> {
self.collection_client
}
}
impl<'a, 'b, C, D> UserAgentOption<'b> for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
#[inline]
fn user_agent(&self) -> Option<&'b str> {
self.user_agent
}
}
impl<'a, 'b, C, D> ActivityIdOption<'b> for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
#[inline]
fn activity_id(&self) -> Option<&'b str> {
self.activity_id
}
}
impl<'a, 'b, C, D> ConsistencyLevelOption<'b> for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
#[inline]
fn consistency_level(&self) -> Option<ConsistencyLevel<'b>> {
self.consistency_level.clone()
}
}
impl<'a, 'b, C, D> ContinuationOption<'b> for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
#[inline]
fn continuation(&self) -> Option<&'b str> {
self.continuation
}
}
impl<'a, 'b, C, D> MaxItemCountOption for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
#[inline]
fn max_item_count(&self) -> i32 {
self.max_item_count
}
}
impl<'a, 'b, C, D> UserAgentSupport<'b> for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
type O = ListStoredProceduresBuilder<'a, 'b, C, D>;
#[inline]
fn with_user_agent(self, user_agent: &'b str) -> Self::O {
ListStoredProceduresBuilder {
collection_client: self.collection_client,
user_agent: Some(user_agent),
activity_id: self.activity_id,
consistency_level: self.consistency_level,
continuation: self.continuation,
max_item_count: self.max_item_count,
}
}
}
impl<'a, 'b, C, D> ActivityIdSupport<'b> for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
type O = ListStoredProceduresBuilder<'a, 'b, C, D>;
#[inline]
fn with_activity_id(self, activity_id: &'b str) -> Self::O {
ListStoredProceduresBuilder {
collection_client: self.collection_client,
user_agent: self.user_agent,
activity_id: Some(activity_id),
consistency_level: self.consistency_level,
continuation: self.continuation,
max_item_count: self.max_item_count,
}
}
}
impl<'a, 'b, C, D> ConsistencyLevelSupport<'b> for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
type O = ListStoredProceduresBuilder<'a, 'b, C, D>;
#[inline]
fn with_consistency_level(self, consistency_level: ConsistencyLevel<'b>) -> Self::O {
ListStoredProceduresBuilder {
collection_client: self.collection_client,
user_agent: self.user_agent,
activity_id: self.activity_id,
consistency_level: Some(consistency_level),
continuation: self.continuation,
max_item_count: self.max_item_count,
}
}
}
impl<'a, 'b, C, D> ContinuationSupport<'b> for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
type O = ListStoredProceduresBuilder<'a, 'b, C, D>;
#[inline]
fn with_continuation(self, continuation: &'b str) -> Self::O {
ListStoredProceduresBuilder {
collection_client: self.collection_client,
user_agent: self.user_agent,
activity_id: self.activity_id,
consistency_level: self.consistency_level,
continuation: Some(continuation),
max_item_count: self.max_item_count,
}
}
}
impl<'a, 'b, C, D> MaxItemCountSupport for ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
type O = ListStoredProceduresBuilder<'a, 'b, C, D>;
#[inline]
fn with_max_item_count(self, max_item_count: i32) -> Self::O {
ListStoredProceduresBuilder {
collection_client: self.collection_client,
user_agent: self.user_agent,
activity_id: self.activity_id,
consistency_level: self.consistency_level,
continuation: self.continuation,
max_item_count,
}
}
}
impl<'a, 'b, C, D> ListStoredProceduresBuilder<'a, 'b, C, D>
where
C: CosmosClient,
D: DatabaseClient<C>,
{
pub async fn execute(&self) -> Result<ListStoredProceduresResponse, AzureError> {
trace!("ListStoredProceduresBuilder::execute called");
let request = self.collection_client.cosmos_client().prepare_request(
&format!(
"dbs/{}/colls/{}/sprocs",
self.collection_client.database_client().database_name(),
self.collection_client.collection_name(),
),
hyper::Method::GET,
ResourceType::StoredProcedures,
);
let request = UserAgentOption::add_header(self, request);
let request = ActivityIdOption::add_header(self, request);
let request = ConsistencyLevelOption::add_header(self, request);
let request = ContinuationOption::add_header(self, request);
let request = MaxItemCountOption::add_header(self, request);
let request = request.body(hyper::Body::empty())?;
let (headers, body) = check_status_extract_headers_and_body(
self.collection_client().hyper_client().request(request),
StatusCode::OK,
)
.await?;
Ok((&headers, &body as &[u8]).try_into()?)
}
pub fn stream(
&self,
) -> impl Stream<Item = Result<ListStoredProceduresResponse, AzureError>> + '_ {
#[derive(Debug, Clone, PartialEq)]
enum States {
Init,
Continuation(String),
};
unfold(
Some(States::Init),
move |continuation_token: Option<States>| {
async move {
debug!("continuation_token == {:?}", &continuation_token);
let response = match continuation_token {
Some(States::Init) => self.execute().await,
Some(States::Continuation(continuation_token)) => {
self.clone()
.with_continuation(&continuation_token)
.execute()
.await
}
None => return None,
};
let response = match response {
Ok(response) => response,
Err(err) => return Some((Err(err), None)),
};
let continuation_token = match &response.continuation_token {
Some(ct) => Some(States::Continuation(ct.to_owned())),
None => None,
};
Some((Ok(response), continuation_token))
}
},
)
}
}