|
| 1 | +use std::future::Future; |
| 2 | +use std::pin::Pin; |
| 3 | + |
1 | 4 | use crate::headers::from_headers::*;
|
2 | 5 | use crate::prelude::*;
|
3 | 6 | use crate::resources::Database;
|
4 | 7 | use crate::ResourceQuota;
|
5 | 8 | use azure_core::headers::{etag_from_headers, session_token_from_headers};
|
6 |
| -use azure_core::{collect_pinned_stream, Request as HttpRequest, Response as HttpResponse}; |
| 9 | +use azure_core::{collect_pinned_stream, Context, PipelineContext, Response as HttpResponse}; |
7 | 10 | use chrono::{DateTime, Utc};
|
8 | 11 |
|
9 | 12 | #[derive(Debug, Clone)]
|
10 |
| -pub struct CreateDatabaseOptions { |
| 13 | +pub struct CreateDatabaseBuilder { |
| 14 | + client: CosmosClient, |
| 15 | + database_name: String, |
11 | 16 | consistency_level: Option<ConsistencyLevel>,
|
| 17 | + context: Context, |
12 | 18 | }
|
13 | 19 |
|
14 |
| -impl CreateDatabaseOptions { |
15 |
| - pub fn new() -> Self { |
| 20 | +impl CreateDatabaseBuilder { |
| 21 | + pub(crate) fn new(client: CosmosClient, database_name: String) -> Self { |
16 | 22 | Self {
|
| 23 | + client, |
| 24 | + database_name, |
17 | 25 | consistency_level: None,
|
| 26 | + context: Context::new(), |
18 | 27 | }
|
19 | 28 | }
|
20 | 29 |
|
21 | 30 | setters! {
|
22 | 31 | consistency_level: ConsistencyLevel => Some(consistency_level),
|
| 32 | + context: Context => context, |
| 33 | + } |
| 34 | + |
| 35 | + async fn build(self) -> crate::Result<CreateDatabaseResponse> { |
| 36 | + let mut request = self |
| 37 | + .client |
| 38 | + .prepare_request_pipeline("dbs", http::Method::POST); |
| 39 | + |
| 40 | + let mut pipeline_context = |
| 41 | + PipelineContext::new(self.context, ResourceType::Databases.into()); |
| 42 | + |
| 43 | + let body = CreateDatabaseBody { |
| 44 | + id: self.database_name.as_str(), |
| 45 | + }; |
| 46 | + |
| 47 | + azure_core::headers::add_optional_header2(&self.consistency_level, &mut request)?; |
| 48 | + request.set_body(bytes::Bytes::from(serde_json::to_string(&body)?).into()); |
| 49 | + let response = self |
| 50 | + .client |
| 51 | + .pipeline() |
| 52 | + .send(&mut pipeline_context, &mut request) |
| 53 | + .await?; |
| 54 | + |
| 55 | + Ok(CreateDatabaseResponse::try_from(response).await?) |
| 56 | + } |
| 57 | + |
| 58 | + pub fn insert<E: Send + Sync + 'static>(&mut self, entity: E) -> &mut Self { |
| 59 | + self.context.insert(entity); |
| 60 | + self |
| 61 | + } |
| 62 | + |
| 63 | + // impl std::future::IntoFuture for CreateDatabaseBuilder {} |
| 64 | + pub fn into_future(self) -> CreateDatabase { |
| 65 | + CreateDatabase(Box::pin(self.build())) |
23 | 66 | }
|
24 | 67 | }
|
25 | 68 |
|
26 |
| -impl CreateDatabaseOptions { |
27 |
| - pub(crate) fn decorate_request( |
28 |
| - &self, |
29 |
| - request: &mut HttpRequest, |
30 |
| - database_name: &str, |
31 |
| - ) -> crate::Result<()> { |
32 |
| - #[derive(Serialize)] |
33 |
| - struct CreateDatabaseRequest<'a> { |
34 |
| - pub id: &'a str, |
35 |
| - } |
36 |
| - let req = CreateDatabaseRequest { id: database_name }; |
| 69 | +pub struct CreateDatabase( |
| 70 | + Pin<Box<dyn Future<Output = crate::Result<CreateDatabaseResponse>> + Send + 'static>>, |
| 71 | +); |
37 | 72 |
|
38 |
| - azure_core::headers::add_optional_header2(&self.consistency_level, request)?; |
39 |
| - request.set_body(bytes::Bytes::from(serde_json::to_string(&req)?).into()); |
40 |
| - Ok(()) |
| 73 | +impl Future for CreateDatabase { |
| 74 | + type Output = crate::Result<CreateDatabaseResponse>; |
| 75 | + fn poll( |
| 76 | + mut self: std::pin::Pin<&mut Self>, |
| 77 | + cx: &mut std::task::Context<'_>, |
| 78 | + ) -> std::task::Poll<Self::Output> { |
| 79 | + Pin::new(&mut self.0).poll(cx) |
41 | 80 | }
|
42 | 81 | }
|
43 | 82 |
|
| 83 | +#[derive(Serialize)] |
| 84 | +struct CreateDatabaseBody<'a> { |
| 85 | + pub id: &'a str, |
| 86 | +} |
| 87 | + |
44 | 88 | #[derive(Debug, Clone, PartialEq, PartialOrd)]
|
45 | 89 | pub struct CreateDatabaseResponse {
|
46 | 90 | pub database: Database,
|
|
0 commit comments