azure_cosmos_mirror/operations/
create_stored_procedure.rs1use crate::headers::from_headers::*;
2use crate::prelude::*;
3use crate::resources::StoredProcedure;
4use crate::ResourceQuota;
5use azure_core::headers::{etag_from_headers, session_token_from_headers};
6use azure_core::Response as HttpResponse;
7use time::OffsetDateTime;
8
9operation! {
10 CreateStoredProcedure,
11 client: StoredProcedureClient,
12 function_body: String,
13 ?consistency_level: ConsistencyLevel
14}
15
16impl CreateStoredProcedureBuilder {
17 pub fn into_future(self) -> CreateStoredProcedure {
18 Box::pin(async move {
19 let mut req = self
20 .client
21 .stored_procedures_request(azure_core::Method::Post);
22
23 if let Some(cl) = &self.consistency_level {
24 req.insert_headers(cl);
25 }
26
27 #[derive(Debug, Serialize)]
28 struct Request<'a> {
29 body: &'a str,
30 id: &'a str,
31 }
32 let body = Request {
33 body: &self.function_body,
34 id: self.client.stored_procedure_name(),
35 };
36
37 req.set_body(serde_json::to_vec(&body)?);
38
39 let response = self
40 .client
41 .pipeline()
42 .send(
43 self.context.clone().insert(ResourceType::StoredProcedures),
44 &mut req,
45 )
46 .await?;
47 CreateStoredProcedureResponse::try_from(response).await
48 })
49 }
50}
51
52#[derive(Debug, Clone, PartialEq)]
54pub struct CreateStoredProcedureResponse {
55 pub stored_procedure: StoredProcedure,
56 pub charge: f64,
57 pub activity_id: uuid::Uuid,
58 pub etag: String,
59 pub session_token: String,
60 pub last_change: OffsetDateTime,
61 pub resource_quota: Vec<ResourceQuota>,
62 pub resource_usage: Vec<ResourceQuota>,
63 pub quorum_acked_lsn: u64,
64 pub current_write_quorum: u64,
65 pub current_replica_set_size: u64,
66}
67
68impl CreateStoredProcedureResponse {
69 pub async fn try_from(response: HttpResponse) -> azure_core::Result<Self> {
70 let (_status_code, headers, body) = response.deconstruct();
71 let body = body.collect().await?;
72
73 Ok(Self {
74 stored_procedure: serde_json::from_slice(&body)?,
75 charge: request_charge_from_headers(&headers)?,
76 activity_id: activity_id_from_headers(&headers)?,
77 etag: etag_from_headers(&headers)?,
78 session_token: session_token_from_headers(&headers)?,
79 last_change: last_state_change_from_headers(&headers)?,
80 resource_quota: resource_quota_from_headers(&headers)?,
81 resource_usage: resource_usage_from_headers(&headers)?,
82 quorum_acked_lsn: quorum_acked_lsn_from_headers(&headers)?,
83 current_write_quorum: current_write_quorum_from_headers(&headers)?,
84 current_replica_set_size: current_replica_set_size_from_headers(&headers)?,
85 })
86 }
87}