|
| 1 | +use crate::headers::from_headers::*; |
| 2 | +use crate::prelude::*; |
| 3 | +use crate::resources::StoredProcedure; |
| 4 | +use crate::ResourceQuota; |
| 5 | +use azure_core::headers::{etag_from_headers, session_token_from_headers}; |
| 6 | +use azure_core::{collect_pinned_stream, Context, Response as HttpResponse}; |
| 7 | +use chrono::{DateTime, Utc}; |
| 8 | + |
| 9 | +#[derive(Debug, Clone)] |
| 10 | +pub struct CreateStoredProcedureBuilder { |
| 11 | + client: StoredProcedureClient, |
| 12 | + function_body: String, |
| 13 | + consistency_level: Option<ConsistencyLevel>, |
| 14 | + context: Context, |
| 15 | +} |
| 16 | + |
| 17 | +impl CreateStoredProcedureBuilder { |
| 18 | + pub(crate) fn new(client: StoredProcedureClient, body: String) -> Self { |
| 19 | + Self { |
| 20 | + client, |
| 21 | + function_body: body, |
| 22 | + consistency_level: None, |
| 23 | + context: Context::new(), |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + setters! { |
| 28 | + consistency_level: ConsistencyLevel => Some(consistency_level), |
| 29 | + context: Context => context, |
| 30 | + } |
| 31 | + |
| 32 | + pub fn into_future(self) -> CreateStoredProcedure { |
| 33 | + Box::pin(async move { |
| 34 | + let mut req = self.client.prepare_request_pipeline(http::Method::POST); |
| 35 | + |
| 36 | + azure_core::headers::add_optional_header2(&self.consistency_level, &mut req)?; |
| 37 | + |
| 38 | + #[derive(Debug, Serialize)] |
| 39 | + struct Request<'a> { |
| 40 | + body: &'a str, |
| 41 | + id: &'a str, |
| 42 | + } |
| 43 | + let body = Request { |
| 44 | + body: &self.function_body, |
| 45 | + id: self.client.stored_procedure_name(), |
| 46 | + }; |
| 47 | + |
| 48 | + req.set_body(bytes::Bytes::from(serde_json::to_string(&body)?).into()); |
| 49 | + |
| 50 | + let response = self |
| 51 | + .client |
| 52 | + .pipeline() |
| 53 | + .send( |
| 54 | + self.context.clone().insert(ResourceType::StoredProcedures), |
| 55 | + &mut req, |
| 56 | + ) |
| 57 | + .await?; |
| 58 | + CreateStoredProcedureResponse::try_from(response).await |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[cfg(feature = "into_future")] |
| 64 | +impl std::future::IntoFuture for CreateStoredProcedureBuilder { |
| 65 | + type Future = CreateStoredProcedure; |
| 66 | + type Output = <CreateStoredProcedure as std::future::Future>::Output; |
| 67 | + fn into_future(self) -> Self::Future { |
| 68 | + Self::into_future(self) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/// The future returned by calling `into_future` on the builder. |
| 73 | +pub type CreateStoredProcedure = |
| 74 | + futures::future::BoxFuture<'static, crate::Result<CreateStoredProcedureResponse>>; |
| 75 | + |
| 76 | +/// A stored procedure response |
| 77 | +#[derive(Debug, Clone, PartialEq)] |
| 78 | +pub struct CreateStoredProcedureResponse { |
| 79 | + pub stored_procedure: StoredProcedure, |
| 80 | + pub charge: f64, |
| 81 | + pub activity_id: uuid::Uuid, |
| 82 | + pub etag: String, |
| 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 quorum_acked_lsn: u64, |
| 88 | + pub current_write_quorum: u64, |
| 89 | + pub current_replica_set_size: u64, |
| 90 | +} |
| 91 | + |
| 92 | +impl CreateStoredProcedureResponse { |
| 93 | + pub async fn try_from(response: HttpResponse) -> crate::Result<Self> { |
| 94 | + let (_status_code, headers, pinned_stream) = response.deconstruct(); |
| 95 | + let body = collect_pinned_stream(pinned_stream).await?; |
| 96 | + |
| 97 | + Ok(Self { |
| 98 | + stored_procedure: serde_json::from_slice(&body)?, |
| 99 | + charge: request_charge_from_headers(&headers)?, |
| 100 | + activity_id: activity_id_from_headers(&headers)?, |
| 101 | + etag: etag_from_headers(&headers)?, |
| 102 | + session_token: session_token_from_headers(&headers)?, |
| 103 | + last_change: last_state_change_from_headers(&headers)?, |
| 104 | + resource_quota: resource_quota_from_headers(&headers)?, |
| 105 | + resource_usage: resource_usage_from_headers(&headers)?, |
| 106 | + quorum_acked_lsn: quorum_acked_lsn_from_headers(&headers)?, |
| 107 | + current_write_quorum: current_write_quorum_from_headers(&headers)?, |
| 108 | + current_replica_set_size: current_replica_set_size_from_headers(&headers)?, |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
0 commit comments