From cb5a0a35bc36cbf234e2ac5d2cec0b2c14ac1d2f Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 19 Mar 2015 07:54:15 +0100 Subject: [PATCH] feat(hub): allow to set user-agent Fixes #24 --- src/mako/lib.rs.mako | 14 ++++++++++++++ src/mako/lib/mbuild.mako | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/mako/lib.rs.mako b/src/mako/lib.rs.mako index 17ba29c30e..2a3a09addf 100644 --- a/src/mako/lib.rs.mako +++ b/src/mako/lib.rs.mako @@ -11,6 +11,8 @@ c = new_context(schemas, resources, context.get('methods')) hub_type = hub_type(c.schemas, util.canonical_name()) ht_params = hub_type_params_s() + + default_user_agent = "google-api-rust-client/" + cargo.build_version %>\ <%block filter="rust_comment">\ <%util:gen_info source="${self.uri}" />\ @@ -73,6 +75,7 @@ ${lib.hub_usage_example(c)}\ pub struct ${hub_type}${ht_params} { client: RefCell, auth: RefCell, + _user_agent: String, _m: PhantomData } @@ -86,6 +89,7 @@ impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params} ${hub_type} { client: RefCell::new(client), auth: RefCell::new(authenticator), + _user_agent: "${default_user_agent}".to_string(), _m: PhantomData } } @@ -95,6 +99,16 @@ impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params} ${rb_type(resource)} { hub: &self } } % endfor + + /// Set the user-agent header field to use in all requests to the server. + /// It defaults to `${default_user_agent}`. + /// + /// Returns the previously set user-agent. + pub fn user_agent(&mut self, agent_name: String) -> String { + let prev = self._user_agent.clone(); + self._user_agent = agent_name; + prev + } } diff --git a/src/mako/lib/mbuild.mako b/src/mako/lib/mbuild.mako index f0ac7dc015..a245b0695e 100644 --- a/src/mako/lib/mbuild.mako +++ b/src/mako/lib/mbuild.mako @@ -634,7 +634,7 @@ else { % endif let mut req = client.borrow_mut().request(hyper::method::Method::Extension("${m.httpMethod}".to_string()), url.as_slice()) - .header(hyper::header::UserAgent("google-api-rust-client/${cargo.build_version}".to_string()))\ + .header(hyper::header::UserAgent(self.hub._user_agent.clone()))\ % if supports_scopes(auth): .header(auth_header)\