1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::error;
use crate::subscription::Subscription;
use crate::topic::Topic;
use futures::prelude::*;
use goauth::auth::JwtClaims;
use goauth::scopes::Scope;
use hyper::client::HttpConnector;
use hyper_tls::HttpsConnector;
use smpl_jwt::Jwt;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use tokio::timer::Interval;
type HyperClient = Arc<hyper::Client<HttpsConnector<HttpConnector>, hyper::Body>>;
pub struct State {
token: Option<goauth::auth::Token>,
credentials_path: String,
project: Option<String>,
hyper_client: HyperClient,
}
impl State {
pub fn token_type(&self) -> &str {
self.token.as_ref().unwrap().token_type()
}
pub fn access_token(&self) -> &str {
self.token.as_ref().unwrap().access_token()
}
pub fn project(&self) -> &str {
&(self.project.as_ref().expect("Google Cloud Project has not been set. If it is not in your credential file, call set_project to set it manually."))
}
}
pub struct Client(Arc<RwLock<State>>);
impl Clone for Client {
fn clone(&self) -> Self {
Client(self.0.clone())
}
}
impl Client {
pub fn new(credentials_path: String) -> Result<Self, error::Error> {
let mut client = Client(Arc::new(RwLock::new(State {
token: None,
credentials_path,
project: None,
hyper_client: setup_hyper(),
})));
match client.refresh_token() {
Ok(_) => Ok(client),
Err(e) => Err(e),
}
}
pub fn subscribe(&self, name: String) -> Subscription {
Subscription {
client: Some(self.clone()),
name: format!("projects/{}/subscriptions/{}", self.project(), name),
topic: None,
}
}
pub fn set_project(&mut self, project: String) {
self.0.write().unwrap().project = Some(project);
}
pub fn project(&self) -> String {
self.0.read().unwrap().project().to_string()
}
pub fn topic(&self, name: String) -> Topic {
Topic {
client: Some(Client(self.0.clone())),
name: format!("projects/{}/topics/{}", self.project(), name),
}
}
pub fn spawn_token_renew(&self) {
let mut client = self.clone();
let renew_token_task = Interval::new(Instant::now(), Duration::from_secs(15 * 60))
.for_each(move |_instant| {
println!("Renewing pubsub token");
if let Err(e) = client.refresh_token() {
eprintln!("Failed to update token: {}", e);
}
Ok(())
})
.map_err(|e| eprintln!("token refresh interval errored; err={:?}", e));
tokio::spawn(renew_token_task);
}
pub fn refresh_token(&mut self) -> Result<(), error::Error> {
match self.get_token() {
Ok(token) => {
self.0.write().unwrap().token = Some(token);
Ok(())
}
Err(e) => Err(error::Error::from(e)),
}
}
fn get_token(&mut self) -> Result<goauth::auth::Token, goauth::error::GOErr> {
let credentials =
goauth::credentials::Credentials::from_file(&self.0.read().unwrap().credentials_path)
.unwrap();
self.set_project(credentials.project());
let claims = JwtClaims::new(
credentials.iss(),
&Scope::PubSub,
credentials.token_uri(),
None,
None,
);
let jwt = Jwt::new(claims, credentials.rsa_key().unwrap(), None);
goauth::get_token_with_creds(&jwt, &credentials)
}
pub(crate) fn request<T: Into<hyper::Body>>(
&self,
method: hyper::Method,
data: T,
) -> hyper::Request<hyper::Body>
where
hyper::Body: std::convert::From<T>,
{
let mut req = hyper::Request::new(hyper::Body::from(data));
*req.method_mut() = method;
req.headers_mut().insert(
hyper::header::CONTENT_TYPE,
hyper::header::HeaderValue::from_static("application/json"),
);
let readable = self.0.read().unwrap();
req.headers_mut().insert(
hyper::header::AUTHORIZATION,
hyper::header::HeaderValue::from_str(&format!(
"{} {}",
readable.token_type(),
readable.access_token()
))
.unwrap(),
);
req
}
pub fn hyper_client(&self) -> HyperClient {
self.0.read().unwrap().hyper_client.clone()
}
}
fn setup_hyper() -> HyperClient {
let https = HttpsConnector::new(4).unwrap();
Arc::new(hyper::Client::builder().build::<_, hyper::Body>(https))
}