@@ -2,24 +2,34 @@ use crate::headers::from_headers::*;
22use crate :: prelude:: * ;
33use crate :: resources:: collection:: { IndexingPolicy , PartitionKey } ;
44use azure_core:: headers:: { etag_from_headers, session_token_from_headers} ;
5- use azure_core:: { collect_pinned_stream, Request as HttpRequest , Response as HttpResponse } ;
5+ use azure_core:: { collect_pinned_stream, Context , Response as HttpResponse } ;
66use chrono:: { DateTime , Utc } ;
77
88#[ derive( Debug , Clone ) ]
9- pub struct CreateCollectionOptions {
9+ pub struct CreateCollectionBuilder {
10+ client : DatabaseClient ,
1011 partition_key : PartitionKey ,
1112 consistency_level : Option < ConsistencyLevel > ,
1213 indexing_policy : Option < IndexingPolicy > ,
14+ collection_name : String ,
1315 offer : Option < Offer > ,
16+ context : Context ,
1417}
1518
16- impl CreateCollectionOptions {
17- pub fn new < P : Into < PartitionKey > > ( partition_key : P ) -> Self {
19+ impl CreateCollectionBuilder {
20+ pub ( crate ) fn new (
21+ client : DatabaseClient ,
22+ collection_name : String ,
23+ partition_key : PartitionKey ,
24+ ) -> Self {
1825 Self {
19- partition_key : partition_key. into ( ) ,
26+ client,
27+ collection_name,
28+ partition_key,
2029 consistency_level : None ,
2130 indexing_policy : None ,
2231 offer : None ,
32+ context : Context :: new ( ) ,
2333 }
2434 }
2535
@@ -29,25 +39,49 @@ impl CreateCollectionOptions {
2939 offer: Offer => Some ( offer) ,
3040 }
3141
32- pub ( crate ) fn decorate_request (
33- & self ,
34- request : & mut HttpRequest ,
35- collection_name : & str ,
36- ) -> crate :: Result < ( ) > {
37- azure_core:: headers:: add_optional_header2 ( & self . offer , request) ?;
38- azure_core:: headers:: add_optional_header2 ( & self . consistency_level , request) ?;
42+ pub fn into_future ( self ) -> CreateCollection {
43+ Box :: pin ( async move {
44+ let mut request = self . client . cosmos_client ( ) . prepare_request_pipeline (
45+ & format ! ( "dbs/{}/colls" , self . client. database_name( ) ) ,
46+ http:: Method :: POST ,
47+ ) ;
48+ azure_core:: headers:: add_optional_header2 ( & self . offer , & mut request) ?;
49+ azure_core:: headers:: add_optional_header2 ( & self . consistency_level , & mut request) ?;
3950
40- let collection = CreateCollectionBody {
41- id : collection_name,
42- indexing_policy : & self . indexing_policy ,
43- partition_key : & self . partition_key ,
44- } ;
51+ let collection = CreateCollectionBody {
52+ id : & self . collection_name ,
53+ indexing_policy : & self . indexing_policy ,
54+ partition_key : & self . partition_key ,
55+ } ;
4556
46- request. set_body ( bytes:: Bytes :: from ( serde_json:: to_string ( & collection) ?) . into ( ) ) ;
47- Ok ( ( ) )
57+ request. set_body ( bytes:: Bytes :: from ( serde_json:: to_string ( & collection) ?) . into ( ) ) ;
58+
59+ let response = self
60+ . client
61+ . pipeline ( )
62+ . send (
63+ self . context . clone ( ) . insert ( ResourceType :: Collections ) ,
64+ & mut request,
65+ )
66+ . await ?;
67+
68+ Ok ( CreateCollectionResponse :: try_from ( response) . await ?)
69+ } )
4870 }
4971}
5072
73+ #[ cfg( feature = "into_future" ) ]
74+ impl std:: future:: IntoFuture for CreateCollectionBuilder {
75+ type Future = CreateCollection ;
76+ type Output = <CreateCollection as std:: future:: Future >:: Output ;
77+ fn into_future ( self ) -> Self :: Future {
78+ Self :: into_future ( self )
79+ }
80+ }
81+
82+ type CreateCollection =
83+ futures:: future:: BoxFuture < ' static , crate :: Result < CreateCollectionResponse > > ;
84+
5185/// Body for the create collection request
5286#[ derive( Serialize , Debug ) ]
5387struct CreateCollectionBody < ' a > {
0 commit comments