Skip to content

Commit fe69997

Browse files
committed
feat: add DUMPSYNC_API constant and implement api_key error alert
1 parent 2f6bdf0 commit fe69997

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

src/cloud/api.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
error::Error,
66

77
io::{
8-
ErrorKind,
8+
ErrorKind,
99
Error as ErrorIo,
1010
},
1111
};
@@ -15,7 +15,11 @@ use reqwest::header::{
1515
AUTHORIZATION,
1616
};
1717

18-
use crate::helpers::env::Env;
18+
use crate::{
19+
helpers::env::Env,
20+
constants::urls::Urls,
21+
ui::errors_alerts::ErrorsAlerts,
22+
};
1923

2024
#[allow(dead_code)]
2125
#[derive(Debug, Deserialize)]
@@ -43,14 +47,17 @@ impl API {
4347
}
4448

4549
pub async fn get(&self) -> Result<Response, Box<dyn Error>> {
46-
let mut api_url = String::from("https://api.dumpsync.com/backups/get/");
50+
let mut api_url = String::from(Urls::DUMPSYNC_API);
51+
api_url.push_str("backups/get/");
4752
api_url.push_str(&self.backup);
4853

4954
let api_token = env::var("DS_API_KEY").unwrap_or_else(|_| {
5055
Env::get_var("DS_API_KEY")
5156
});
5257

5358
if api_token.is_empty() {
59+
ErrorsAlerts::api_key();
60+
5461
return Err(Box::new(ErrorIo::new(
5562
ErrorKind::Other,
5663
"API token is not set",

src/constants/urls.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ impl Urls {
77

88
pub const PASTEBIN_API_URI: &'static str = "https://pastebin.com/api/api_post.php";
99
pub const CDN_BOOTSTRAP: &'static str = "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css";
10+
11+
pub const DUMPSYNC_API: &'static str = "https://api.dumpsync.com/";
1012

1113
}

src/helpers/env.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ impl Env {
1111
}
1212

1313
pub fn get_var(var: &str) -> String {
14-
env::var(var).expect(&format!("{} is not defined in the .env", var))
14+
env::var(var).expect(
15+
&format!("{} is not defined in the .env", var)
16+
)
1517
}
1618

1719
pub fn get_var_u64(var: &str) -> u64 {
18-
env::var(var).expect(&format!("{} is not defined in the .env", var)).parse().expect(&format!("{} is not a valid number", var))
20+
env::var(var).expect(
21+
&format!("{} is not defined in the .env", var)
22+
).parse().expect(
23+
&format!("{} is not a valid number", var)
24+
)
1925
}
2026

2127
}

src/ui/errors_alerts.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,13 @@ impl ErrorsAlerts {
5959
);
6060
}
6161

62+
pub fn api_key() {
63+
let current_datetime = Date::date_time();
64+
65+
println!(
66+
"{} API key is not set",
67+
current_datetime.red().bold(),
68+
);
69+
}
70+
6271
}

0 commit comments

Comments
 (0)