Skip to content

Commit cb5a0a3

Browse files
committed
feat(hub): allow to set user-agent
Fixes #24
1 parent 9d401f5 commit cb5a0a3

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/mako/lib.rs.mako

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
c = new_context(schemas, resources, context.get('methods'))
1212
hub_type = hub_type(c.schemas, util.canonical_name())
1313
ht_params = hub_type_params_s()
14+
15+
default_user_agent = "google-api-rust-client/" + cargo.build_version
1416
%>\
1517
<%block filter="rust_comment">\
1618
<%util:gen_info source="${self.uri}" />\
@@ -73,6 +75,7 @@ ${lib.hub_usage_example(c)}\
7375
pub struct ${hub_type}${ht_params} {
7476
client: RefCell<C>,
7577
auth: RefCell<A>,
78+
_user_agent: String,
7679

7780
_m: PhantomData<NC>
7881
}
@@ -86,6 +89,7 @@ impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params}
8689
${hub_type} {
8790
client: RefCell::new(client),
8891
auth: RefCell::new(authenticator),
92+
_user_agent: "${default_user_agent}".to_string(),
8993
_m: PhantomData
9094
}
9195
}
@@ -95,6 +99,16 @@ impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params}
9599
${rb_type(resource)} { hub: &self }
96100
}
97101
% endfor
102+
103+
/// Set the user-agent header field to use in all requests to the server.
104+
/// It defaults to `${default_user_agent}`.
105+
///
106+
/// Returns the previously set user-agent.
107+
pub fn user_agent(&mut self, agent_name: String) -> String {
108+
let prev = self._user_agent.clone();
109+
self._user_agent = agent_name;
110+
prev
111+
}
98112
}
99113

100114

src/mako/lib/mbuild.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ else {
634634
% endif
635635
636636
let mut req = client.borrow_mut().request(hyper::method::Method::Extension("${m.httpMethod}".to_string()), url.as_slice())
637-
.header(hyper::header::UserAgent("google-api-rust-client/${cargo.build_version}".to_string()))\
637+
.header(hyper::header::UserAgent(self.hub._user_agent.clone()))\
638638
% if supports_scopes(auth):
639639
640640
.header(auth_header)\

0 commit comments

Comments
 (0)