@@ -11,6 +11,7 @@ use tokio::timer::Interval;
1111pub struct State {
1212 token : Option < goauth:: auth:: Token > ,
1313 credentials_path : String ,
14+ project : Option < String > ,
1415}
1516
1617impl State {
@@ -21,20 +22,24 @@ impl State {
2122 pub fn access_token ( & self ) -> & str {
2223 self . token . as_ref ( ) . unwrap ( ) . access_token ( )
2324 }
25+
26+ pub fn project ( & self ) -> & str {
27+ & ( 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." ) )
28+ }
2429}
2530
2631pub trait Client
2732where
28- Self : std:: marker:: Sized + TokenAuthenticated ,
33+ Self : std:: marker:: Sized ,
2934{
3035 fn create ( credentials_path : String ) -> Result < Self , error:: Error > ;
3136 fn subscribe ( & self , name : String ) -> Subscription ;
32- }
37+ fn set_project ( & mut self , project : String ) ;
38+ fn project ( & self ) -> String ;
3339
34- pub trait TokenAuthenticated {
3540 fn spawn_token_renew ( & self ) ;
3641 fn refresh_token ( & mut self ) -> Result < ( ) , error:: Error > ;
37- fn get_token ( & self ) -> Result < goauth:: auth:: Token , goauth:: error:: GOErr > ;
42+ fn get_token ( & mut self ) -> Result < goauth:: auth:: Token , goauth:: error:: GOErr > ;
3843}
3944
4045pub type BaseClient = Arc < RwLock < State > > ;
@@ -43,6 +48,7 @@ impl Client for BaseClient {
4348 fn subscribe ( & self , name : String ) -> Subscription {
4449 Subscription {
4550 client : self . clone ( ) ,
51+ canonical_name : format ! ( "projects/{}/subscriptions/{}" , self . project( ) , name) ,
4652 name,
4753 }
4854 }
@@ -51,16 +57,23 @@ impl Client for BaseClient {
5157 let mut client = Arc :: new ( RwLock :: new ( State {
5258 token : None ,
5359 credentials_path,
60+ project : None ,
5461 } ) ) ;
5562
5663 match client. refresh_token ( ) {
5764 Ok ( _) => Ok ( client) ,
5865 Err ( e) => Err ( e) ,
5966 }
6067 }
61- }
6268
63- impl TokenAuthenticated for BaseClient {
69+ fn set_project ( & mut self , project : String ) {
70+ self . write ( ) . unwrap ( ) . project = Some ( project) ;
71+ }
72+
73+ fn project ( & self ) -> String {
74+ self . read ( ) . unwrap ( ) . project ( ) . to_string ( )
75+ }
76+
6477 fn spawn_token_renew ( & self ) {
6578 let mut client = self . clone ( ) ;
6679 let renew_token_task = Interval :: new ( Instant :: now ( ) , Duration :: from_secs ( 15 * 60 ) )
@@ -86,11 +99,13 @@ impl TokenAuthenticated for BaseClient {
8699 }
87100 }
88101
89- fn get_token ( & self ) -> Result < goauth:: auth:: Token , goauth:: error:: GOErr > {
102+ fn get_token ( & mut self ) -> Result < goauth:: auth:: Token , goauth:: error:: GOErr > {
90103 let credentials =
91104 goauth:: credentials:: Credentials :: from_file ( & self . read ( ) . unwrap ( ) . credentials_path )
92105 . unwrap ( ) ;
93106
107+ self . set_project ( credentials. project ( ) ) ;
108+
94109 let claims = JwtClaims :: new (
95110 credentials. iss ( ) ,
96111 & Scope :: PubSub ,
0 commit comments