|
| 1 | +use crate::headers::from_headers::*; |
| 2 | +use crate::prelude::*; |
| 3 | +use crate::resources::ResourceType; |
| 4 | +use crate::resources::StoredProcedure; |
| 5 | +use crate::ResourceQuota; |
| 6 | +use azure_core::collect_pinned_stream; |
| 7 | +use azure_core::headers; |
| 8 | +use azure_core::headers::{continuation_token_from_headers_optional, session_token_from_headers}; |
| 9 | +use azure_core::prelude::*; |
| 10 | +use azure_core::{Pageable, Response as HttpResponse}; |
| 11 | +use chrono::{DateTime, Utc}; |
| 12 | + |
| 13 | +#[derive(Debug, Clone)] |
| 14 | +pub struct ListStoredProceduresBuilder { |
| 15 | + client: CollectionClient, |
| 16 | + consistency_level: Option<ConsistencyLevel>, |
| 17 | + max_item_count: MaxItemCount, |
| 18 | + context: Context, |
| 19 | +} |
| 20 | + |
| 21 | +impl ListStoredProceduresBuilder { |
| 22 | + pub(crate) fn new(client: CollectionClient) -> Self { |
| 23 | + Self { |
| 24 | + client, |
| 25 | + consistency_level: None, |
| 26 | + max_item_count: MaxItemCount::new(-1), |
| 27 | + context: Context::new(), |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + setters! { |
| 32 | + consistency_level: ConsistencyLevel => Some(consistency_level), |
| 33 | + max_item_count: i32 => MaxItemCount::new(max_item_count), |
| 34 | + context: Context => context, |
| 35 | + } |
| 36 | + |
| 37 | + pub fn into_stream(self) -> ListStoredProcedures { |
| 38 | + let make_request = move |continuation: Option<String>| { |
| 39 | + let this = self.clone(); |
| 40 | + let ctx = self.context.clone(); |
| 41 | + async move { |
| 42 | + let mut request = this.client.cosmos_client().prepare_request_pipeline( |
| 43 | + &format!( |
| 44 | + "dbs/{}/colls/{}/sprocs", |
| 45 | + this.client.database_client().database_name(), |
| 46 | + this.client.collection_name(), |
| 47 | + ), |
| 48 | + http::Method::GET, |
| 49 | + ); |
| 50 | + |
| 51 | + azure_core::headers::add_optional_header2(&this.consistency_level, &mut request)?; |
| 52 | + azure_core::headers::add_mandatory_header2(&this.max_item_count, &mut request)?; |
| 53 | + |
| 54 | + if let Some(c) = continuation { |
| 55 | + let h = http::HeaderValue::from_str(c.as_str()) |
| 56 | + .map_err(azure_core::HttpHeaderError::InvalidHeaderValue)?; |
| 57 | + request.headers_mut().append(headers::CONTINUATION, h); |
| 58 | + } |
| 59 | + |
| 60 | + let response = this |
| 61 | + .client |
| 62 | + .pipeline() |
| 63 | + .send( |
| 64 | + ctx.clone().insert(ResourceType::StoredProcedures), |
| 65 | + &mut request, |
| 66 | + ) |
| 67 | + .await?; |
| 68 | + ListStoredProceduresResponse::try_from(response).await |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + Pageable::new(make_request) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +pub type ListStoredProcedures = Pageable<ListStoredProceduresResponse, crate::Error>; |
| 77 | + |
| 78 | +#[derive(Debug, Clone, PartialEq)] |
| 79 | +pub struct ListStoredProceduresResponse { |
| 80 | + pub stored_procedures: Vec<StoredProcedure>, |
| 81 | + pub charge: f64, |
| 82 | + pub activity_id: uuid::Uuid, |
| 83 | + pub session_token: String, |
| 84 | + pub last_change: DateTime<Utc>, |
| 85 | + pub resource_quota: Vec<ResourceQuota>, |
| 86 | + pub resource_usage: Vec<ResourceQuota>, |
| 87 | + pub gateway_version: String, |
| 88 | + pub continuation_token: Option<String>, |
| 89 | +} |
| 90 | + |
| 91 | +impl ListStoredProceduresResponse { |
| 92 | + pub async fn try_from(response: HttpResponse) -> crate::Result<Self> { |
| 93 | + let (_status_code, headers, pinned_stream) = response.deconstruct(); |
| 94 | + let body = collect_pinned_stream(pinned_stream).await?; |
| 95 | + |
| 96 | + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 97 | + struct Response { |
| 98 | + pub _rid: String, |
| 99 | + #[serde(rename = "StoredProcedures")] |
| 100 | + pub stored_procedures: Vec<StoredProcedure>, |
| 101 | + pub _count: u64, |
| 102 | + } |
| 103 | + |
| 104 | + Ok(Self { |
| 105 | + stored_procedures: serde_json::from_slice::<Response>(&body)?.stored_procedures, |
| 106 | + charge: request_charge_from_headers(&headers)?, |
| 107 | + activity_id: activity_id_from_headers(&headers)?, |
| 108 | + session_token: session_token_from_headers(&headers)?, |
| 109 | + last_change: last_state_change_from_headers(&headers)?, |
| 110 | + resource_quota: resource_quota_from_headers(&headers)?, |
| 111 | + resource_usage: resource_usage_from_headers(&headers)?, |
| 112 | + gateway_version: gateway_version_from_headers(&headers)?.to_owned(), |
| 113 | + continuation_token: continuation_token_from_headers_optional(&headers)?, |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl Continuable for ListStoredProceduresResponse { |
| 119 | + fn continuation(&self) -> Option<String> { |
| 120 | + self.continuation_token.clone() |
| 121 | + } |
| 122 | +} |
0 commit comments