@@ -3,44 +3,68 @@ use crate::prelude::*;
3
3
use crate :: resources:: Database ;
4
4
use crate :: ResourceQuota ;
5
5
use azure_core:: headers:: { etag_from_headers, session_token_from_headers} ;
6
- use azure_core:: { collect_pinned_stream, Request as HttpRequest , Response as HttpResponse } ;
6
+ use azure_core:: { collect_pinned_stream, Context , Response as HttpResponse } ;
7
7
use chrono:: { DateTime , Utc } ;
8
8
9
9
#[ derive( Debug , Clone ) ]
10
- pub struct CreateDatabaseOptions {
10
+ pub struct CreateDatabaseBuilder {
11
+ client : CosmosClient ,
12
+ database_name : String ,
11
13
consistency_level : Option < ConsistencyLevel > ,
14
+ context : Context ,
12
15
}
13
16
14
- impl CreateDatabaseOptions {
15
- pub fn new ( ) -> Self {
17
+ impl CreateDatabaseBuilder {
18
+ pub ( crate ) fn new ( client : CosmosClient , database_name : String ) -> Self {
16
19
Self {
20
+ client,
21
+ database_name,
17
22
consistency_level : None ,
23
+ context : Context :: new ( ) ,
18
24
}
19
25
}
20
26
21
27
setters ! {
22
28
consistency_level: ConsistencyLevel => Some ( consistency_level) ,
29
+ context: Context => context,
23
30
}
24
- }
25
31
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 } ;
32
+ pub fn insert < E : Send + Sync + ' static > ( & mut self , entity : E ) -> & mut Self {
33
+ self . context . insert ( entity) ;
34
+ self
35
+ }
36
+
37
+ pub fn into_future ( mut self ) -> CreateDatabase {
38
+ Box :: pin ( async move {
39
+ let mut request = self
40
+ . client
41
+ . prepare_request_pipeline ( "dbs" , http:: Method :: POST ) ;
42
+
43
+ let body = CreateDatabaseBody {
44
+ id : self . database_name . as_str ( ) ,
45
+ } ;
37
46
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 ( ( ) )
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 ( self . context . insert ( ResourceType :: Databases ) , & mut request)
53
+ . await ?;
54
+
55
+ CreateDatabaseResponse :: try_from ( response) . await
56
+ } )
41
57
}
42
58
}
43
59
60
+ /// A future of a create database response
61
+ type CreateDatabase = futures:: future:: BoxFuture < ' static , crate :: Result < CreateDatabaseResponse > > ;
62
+
63
+ #[ derive( Serialize ) ]
64
+ struct CreateDatabaseBody < ' a > {
65
+ pub id : & ' a str ,
66
+ }
67
+
44
68
#[ derive( Debug , Clone , PartialEq , PartialOrd ) ]
45
69
pub struct CreateDatabaseResponse {
46
70
pub database : Database ,
0 commit comments