@@ -3,44 +3,68 @@ use crate::prelude::*;
33use crate :: resources:: Database ;
44use crate :: ResourceQuota ;
55use 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 } ;
77use chrono:: { DateTime , Utc } ;
88
99#[ derive( Debug , Clone ) ]
10- pub struct CreateDatabaseOptions {
10+ pub struct CreateDatabaseBuilder {
11+ client : CosmosClient ,
12+ database_name : String ,
1113 consistency_level : Option < ConsistencyLevel > ,
14+ context : Context ,
1215}
1316
14- impl CreateDatabaseOptions {
15- pub fn new ( ) -> Self {
17+ impl CreateDatabaseBuilder {
18+ pub ( crate ) fn new ( client : CosmosClient , database_name : String ) -> Self {
1619 Self {
20+ client,
21+ database_name,
1722 consistency_level : None ,
23+ context : Context :: new ( ) ,
1824 }
1925 }
2026
2127 setters ! {
2228 consistency_level: ConsistencyLevel => Some ( consistency_level) ,
29+ context: Context => context,
2330 }
24- }
2531
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+ } ;
3746
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+ } )
4157 }
4258}
4359
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+
4468#[ derive( Debug , Clone , PartialEq , PartialOrd ) ]
4569pub struct CreateDatabaseResponse {
4670 pub database : Database ,
0 commit comments