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
use crate::Error; use crate::Topic; use goauth::auth::JwtClaims; use goauth::credentials::Credentials; use goauth::scopes::Scope; use smpl_jwt::Jwt; use surf::http::Method; use surf::middleware::HttpClient; use surf::url::Url; use surf::Request; #[derive(Clone, Debug)] pub struct Client { credentials: Credentials, access_token: Option<String>, } impl Client { pub fn new(credentials: Credentials) -> Self { Client { access_token: None, credentials, } } pub fn base_request(&self, method: Method, url: &str) -> Request<impl HttpClient> { let parsed_url = Url::parse(url).unwrap(); Request::new(method, parsed_url).set_header( "Authorization", format!( "Bearer {}", self .access_token .clone() .unwrap_or("token_doesnt_exist".into()) ), ) } pub fn topic(&self, name: &str) -> Topic { Topic::new(self.clone(), name) } pub async fn create_topic(&self, name: &str) -> Result<Topic, Error> { Topic::create(self.clone(), name).await } pub fn project(&self) -> String { self.credentials.project() } pub fn refresh_token(&mut self) -> Result<(), Error> { match self.get_token() { Ok(token) => { self.access_token = Some(token.access_token().to_owned()); Ok(()) } Err(e) => Err(Error::from(e)), } } fn get_token(&mut self) -> Result<goauth::auth::Token, goauth::error::GOErr> { let claims = JwtClaims::new( self.credentials.iss(), &Scope::PubSub, self.credentials.token_uri(), None, None, ); let jwt = Jwt::new(claims, self.credentials.rsa_key().unwrap(), None); goauth::get_token_with_creds(&jwt, &self.credentials) } }