Skip to content

Commit e8da546

Browse files
committed
Pass Transport to builder structs
This commit changes the ctor signatures of builder structs to accept a Transport reference as opposed to an Elasticsearch reference. (cherry picked from commit e0bf029)
1 parent 7a5cb18 commit e8da546

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

api_generator/src/generator/code_gen/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn use_declarations() -> Tokens {
4040
Method,
4141
request::{Body, NdBody, JsonBody, PARTS_ENCODED},
4242
response::Response,
43+
transport::Transport,
4344
},
4445
};
4546
use std::borrow::Cow;

api_generator/src/generator/code_gen/namespace_clients.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,28 @@ pub fn generate(api: &Api, docs_dir: &PathBuf) -> Result<Vec<(String, String)>,
7676

7777
#namespace_doc
7878
pub struct #namespace_client_name<'a> {
79-
client: &'a Elasticsearch
79+
transport: &'a Transport
8080
}
8181

8282
impl<'a> #namespace_client_name<'a> {
8383
#new_namespace_client_doc
84-
pub fn new(client: &'a Elasticsearch) -> Self {
84+
pub fn new(transport: &'a Transport) -> Self {
8585
Self {
86-
client
86+
transport
8787
}
8888
}
89+
90+
pub fn transport(&self) -> &Transport {
91+
self.transport
92+
}
93+
8994
#(#methods)*
9095
}
9196

9297
impl Elasticsearch {
9398
#namespace_fn_doc
9499
pub fn #namespace_name(&self) -> #namespace_client_name {
95-
#namespace_client_name::new(&self)
100+
#namespace_client_name::new(self.transport())
96101
}
97102
}
98103
));

api_generator/src/generator/code_gen/request/request_builder.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ impl<'a> RequestBuilder<'a> {
194194
let doc = doc(format!("Creates a new instance of [{}]", &builder_name));
195195
quote!(
196196
#doc
197-
pub fn new(client: &'a Elasticsearch) -> Self {
197+
pub fn new(transport: &'a Transport) -> Self {
198198
#headers
199199

200200
#builder_ident {
201-
client,
201+
transport,
202202
parts: #enum_ty::None,
203203
headers,
204204
#(#default_fields),*,
@@ -212,11 +212,11 @@ impl<'a> RequestBuilder<'a> {
212212
));
213213
quote!(
214214
#doc
215-
pub fn new(client: &'a Elasticsearch, parts: #enum_ty) -> Self {
215+
pub fn new(transport: &'a Transport, parts: #enum_ty) -> Self {
216216
#headers
217217

218218
#builder_ident {
219-
client,
219+
transport,
220220
parts,
221221
headers,
222222
#(#default_fields),*,
@@ -296,7 +296,7 @@ impl<'a> RequestBuilder<'a> {
296296
syn::Block {
297297
stmts: vec![syn::Stmt::Expr(Box::new(parse_expr(quote!(
298298
#builder_ident {
299-
client: self.client,
299+
transport: self.transport,
300300
parts: self.parts,
301301
body: #field_arg,
302302
#(#fields),*,
@@ -577,7 +577,7 @@ impl<'a> RequestBuilder<'a> {
577577
#[derive(Clone, Debug)]
578578
#[doc = #builder_doc]
579579
pub struct #builder_expr {
580-
client: &'a Elasticsearch,
580+
transport: &'a Transport,
581581
parts: #enum_ty,
582582
#(#fields),*,
583583
}
@@ -593,7 +593,7 @@ impl<'a> RequestBuilder<'a> {
593593
let headers = self.headers;
594594
let query_string = #query_string_expr;
595595
let body = #body_expr;
596-
let response = self.client.send(method, &path, headers, query_string.as_ref(), body).await?;
596+
let response = self.transport.send(method, &path, headers, query_string.as_ref(), body).await?;
597597
Ok(response)
598598
}
599599
}
@@ -662,11 +662,7 @@ impl<'a> RequestBuilder<'a> {
662662
_ => doc(format!("{} API{}", api_name_for_docs, markdown_doc)),
663663
};
664664

665-
let clone_expr = if is_root_method {
666-
quote!(&self)
667-
} else {
668-
quote!(&self.client)
669-
};
665+
let clone_expr = quote!(self.transport());
670666

671667
if enum_builder.contains_single_parameterless_part() {
672668
quote!(

elasticsearch/src/client.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ impl Elasticsearch {
6464
Elasticsearch { transport }
6565
}
6666

67+
/// Gets the transport of the client
68+
pub fn transport(&self) -> &Transport {
69+
&self.transport
70+
}
71+
6772
/// Creates an asynchronous request that can be awaited
6873
///
6974
/// Accepts the HTTP method and relative path to an API,

0 commit comments

Comments
 (0)