@@ -45,14 +45,10 @@ impl BackendClient {
4545 pub fn new ( cli : & Cli ) -> Result < Self > {
4646 let fs = FileSystem :: new ( ) . unwrap ( ) ;
4747
48- // Load the user's session information from the filesystem.
49- let session = fs. load_file ( SessionFile ) . map ( |session| session ) ;
48+ // Load the user's session information from the filesystem, if it exists
49+ let session = fs. load_file ( SessionFile ) ? ;
5050
51- let creds = match session {
52- Session :: User ( creds) => creds,
53- } ;
54-
55- let conf = BackendConfig :: new ( cli. origin ( ) , Some ( creds. jwt . clone ( ) ) ) ;
51+ let conf = BackendConfig :: new ( cli. origin ( ) , Some ( session. clone ( ) ) ) ;
5652
5753 let raw_conf: Configuration = conf. clone ( ) . into ( ) ;
5854
@@ -64,8 +60,12 @@ impl BackendClient {
6460 } )
6561 }
6662
67- pub fn is_authenicated ( & self ) -> bool {
68- self . session . is_some ( )
63+ pub fn is_authenicated ( & self ) -> Result < ( ) > {
64+ if self . session . clone ( ) . is_some_and ( Session :: is_not_expired) {
65+ return Ok ( ( ) ) ;
66+ } else {
67+ bail ! ( "Please login before running this command." ) ;
68+ }
6969 }
7070
7171 /// Given the workspace name and the application name, fetch
@@ -114,9 +114,8 @@ impl BackendClient {
114114
115115 /// Return information about the workspace given its name.
116116 async fn get_workspace_by_name ( & self , name : & str ) -> Result < WorkspaceSummary > {
117- if !self . is_authenicated ( ) {
118- bail ! ( "Please login before running this command." ) ;
119- }
117+ self . is_authenicated ( ) ?;
118+
120119 // let mut workspaces = list_workspaces(&self.conf, Some(name))
121120 let mut workspaces: Vec < _ > = self
122121 . client
@@ -148,9 +147,7 @@ impl BackendClient {
148147 workspace_id : Uuid ,
149148 name : & str ,
150149 ) -> Result < ApplicationDetails > {
151- if !self . is_authenicated ( ) {
152- bail ! ( "Please login before running this command." ) ;
153- }
150+ self . is_authenicated ( ) ?;
154151
155152 let mut applications: Vec < _ > = self
156153 . client
@@ -200,12 +197,15 @@ pub(super) struct BackendConfig {
200197}
201198
202199impl BackendConfig {
203- pub fn new < T : AsRef < str > > ( origin : Option < T > , jwt : Option < String > ) -> Self {
200+ pub fn new < T : AsRef < str > > ( origin : Option < T > , session : Option < Session > ) -> Self {
204201 // • Convert the Option<T> to a String.
205202 let origin = origin. map ( |val| val. as_ref ( ) . to_owned ( ) ) ;
206203 // • Set up the default configuration values.
204+ let jwt = session. and_then ( |session| match session {
205+ Session :: User ( creds) => Some ( creds. jwt ) ,
206+ } ) ;
207207 let conf = Configuration {
208- base_path : origin. unwrap ( ) ,
208+ base_path : origin. unwrap_or ( "https://api.multitool.run" . to_string ( ) ) ,
209209 bearer_access_token : jwt,
210210 ..Configuration :: default ( )
211211 } ;
0 commit comments