Propagate errors from token reading from file

This commit is contained in:
flyingscorpio@clevo 2022-12-08 17:05:40 +01:00
parent bff55dad46
commit b265eae52e
2 changed files with 10 additions and 4 deletions

View file

@ -1,3 +1,5 @@
use std::process;
use reqwest::Url;
use serde_json::Value;
@ -12,7 +14,10 @@ pub struct ApiClient {
impl ApiClient {
pub fn new() -> Self {
Self {
api_key: token::read_api_key(),
api_key: token::read_api_key().unwrap_or_else(|err| {
eprintln!("Unable to read from '.token' file");
process::exit(1);
}),
base_uri: String::from("https://www.virustotal.com/api/v3"),
client: reqwest::blocking::Client::new(),
}

View file

@ -1,7 +1,8 @@
use std::io;
use std::fs::read_to_string;
pub fn read_api_key() -> String {
let api_key = read_to_string(".token").expect("Unable to read from '.token' file");
pub fn read_api_key() -> io::Result<String> {
let api_key = read_to_string(".token")?;
api_key.trim().to_string()
Ok(api_key.trim().to_string())
}