Skip to content

Commit

Permalink
Add no auth mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximMaximS committed Jan 28, 2024
1 parent 700cfc6 commit ea5f990
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/target
*.html
config.json
timetable.json
*.json
18 changes: 10 additions & 8 deletions rezvrh_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use tokio::fs;
#[derive(Serialize, Deserialize)]
pub struct Config {
url: String,
username: String,
password: String,
username: Option<String>,
password: Option<String>,
}

#[derive(Parser, Debug)]
Expand All @@ -25,11 +25,14 @@ async fn main() -> anyhow::Result<()> {
let args = Args::parse();

let s = fs::read_to_string(args.config)
.await
.expect("failed to load config.json");
let conf = serde_json::from_str::<Config>(&s).expect("failed to parse config.json");
.await?;
let conf = serde_json::from_str::<Config>(&s)?;
let url = Url::parse(&conf.url)?;
let bakalari = Bakalari::from_creds((conf.username, conf.password), url).await?;
let bakalari = if let (Some(username), Some(password)) = (conf.username, conf.password) {
Bakalari::from_creds((username, password), url).await?
} else {
Bakalari::no_auth(url).await?
};
bakalari.test().await?;

let typ = Select::new("Choose type", vec![Type::Teacher, Type::Class, Type::Room]).prompt()?;
Expand All @@ -50,8 +53,7 @@ async fn main() -> anyhow::Result<()> {
Type::Teacher => bakalari.get_teacher(&select),
Type::Class => bakalari.get_class(&select),
Type::Room => bakalari.get_room(&select),
}
.unwrap();
}.unwrap();

let table = bakalari.get_timetable(which, &selection).await?;

Expand Down
2 changes: 2 additions & 0 deletions rezvrh_scraper/src/modules/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum Auth {
Credentials(Credentials),
// Token (might expire)
Token(String),
None,
}

impl Auth {
Expand All @@ -42,6 +43,7 @@ impl Auth {
match self {
Self::Token(token) => Ok(Cow::Borrowed(token)),
Self::Credentials(creds) => Ok(Cow::Owned(creds.get_token(client.clone()).await?)),
Self::None => Ok(Cow::Owned(String::new())),
}
}

Expand Down
17 changes: 17 additions & 0 deletions rezvrh_scraper/src/modules/bakalari.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ impl Bakalari {
})
}

/// Create Bakalari instance without authentication
///
/// # Errors
/// Returns error if authentication fails
pub async fn no_auth(url: Url) -> Result<Self, RequestError> {
let client = Arc::new(Client::new(url));
let (classes, teachers, rooms) =
get_info(client.reqwest_client(), client.url(), "").await?;
Ok(Self {
client,
auth: Auth::None,
classes,
teachers,
rooms,
})
}

/// Get token
///
/// # Errors
Expand Down

0 comments on commit ea5f990

Please sign in to comment.