1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use reqwest::blocking::Client;
use base64::{encode};
use crate::handle_b2error_kinds;
use crate::Error;
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[serde(rename_all = "camelCase")]
pub struct B2Auth {
pub account_id: String,
pub authorization_token: String,
pub api_url: String,
pub download_url: String,
pub absolute_minimum_part_size: usize,
pub recommended_part_size: usize
}
impl B2Auth {
pub fn api_url_for(&self, call_name: &str) -> String {
format!("{}/b2api/v2/{}",self.api_url,call_name)
}
pub fn download_url_by_name<T: AsRef<str>>(&self, bucket_name: T, file_name: T) -> String {
format!("{}/file/{}/{}",self.download_url,bucket_name.as_ref(),file_name.as_ref())
}
pub fn download_url_by_id<T: AsRef<str>>(&self, file_id: T) -> String {
format!("{}/b2api/v2/b2_download_file_by_id?fileId={}",self.download_url,file_id.as_ref())
}
}
pub fn b2_authorize_account<T: AsRef<str>>(client: &Client, keystring: T) -> Result<B2Auth, Error> {
let encoded = format!("{}{}", "Basic ", encode(keystring.as_ref()));
let resp = match client.get("https://api.backblazeb2.com/b2api/v2/b2_authorize_account")
.header(reqwest::header::AUTHORIZATION, encoded)
.send() {
Ok(v) => v,
Err(e) => return Err(Error::ReqwestError(e))
};
if !resp.status().is_success() {
return Err(Error::from_response(resp))
}
let response_string = resp.text().unwrap();
let deserialized: B2Auth = match serde_json::from_str(&response_string) {
Ok(v) => v,
Err(_e) => {
return Err(handle_b2error_kinds(&response_string))
}
};
Ok(deserialized)
}