Added methods to change session and get current session index
This commit is contained in:
parent
59d9532766
commit
f0d24ec03b
|
@ -44,6 +44,13 @@ socialvoid-cli logout",
|
|||
ClientError::SerdeJson(err) => {
|
||||
write!(f, "Error while parsing JSON.\n{:?}", err)
|
||||
}
|
||||
ClientError::SessionIndexOutOfBounds { session_count } => {
|
||||
write!(
|
||||
f,
|
||||
"SessionIndexOutOfBounds. Number of sessions is {}",
|
||||
session_count
|
||||
)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,9 @@ pub enum ClientError {
|
|||
/// Thrown when `current_session` is `None`, typically means there aren't any
|
||||
/// sessions and you should create one.
|
||||
NoSessionsExist,
|
||||
/// Thrown when tried to access a session via index and it's out of bounds.
|
||||
/// Also, returns the number of sessions that we have
|
||||
SessionIndexOutOfBounds { session_count: usize },
|
||||
/// Errors thrown by serde json
|
||||
SerdeJson(serde_json::Error),
|
||||
}
|
||||
|
|
|
@ -108,6 +108,8 @@ impl Client {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Get another video
|
||||
|
||||
/// Tries to establish another session adds it to the client if successful and returns the key of the session
|
||||
pub async fn new_session(&mut self) -> Result<usize, SocialvoidError> {
|
||||
let mut session = SessionHolder::new(self.client_info.clone());
|
||||
|
@ -136,6 +138,25 @@ impl Client {
|
|||
}
|
||||
}
|
||||
|
||||
/// Set the current session to session_key if exists
|
||||
pub fn set_current_session(&mut self, session_key: usize) -> Result<(), SocialvoidError> {
|
||||
if self.sessions.len() > session_key {
|
||||
self.current_session = Some(session_key);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(SocialvoidError::Client(
|
||||
ClientError::SessionIndexOutOfBounds {
|
||||
session_count: self.sessions.len(),
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the current session key
|
||||
pub fn get_current_session_key(&self) -> Option<usize> {
|
||||
self.current_session.clone()
|
||||
}
|
||||
|
||||
/// Gets a Session object for the current session
|
||||
pub async fn get_session(&mut self) -> Result<Session, SocialvoidError> {
|
||||
match self.current_session {
|
||||
|
|
Loading…
Reference in New Issue