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
use crate::{CryptoKVStorage, Error, GetResponse, KVStorage};
use diem_github_client::Client;
use diem_time_service::{TimeService, TimeServiceTrait};
use serde::{de::DeserializeOwned, Serialize};
pub struct GitHubStorage {
client: Client,
time_service: TimeService,
}
impl GitHubStorage {
pub fn new(owner: String, repository: String, branch: String, token: String) -> Self {
Self {
client: Client::new(owner, repository, branch, token),
time_service: TimeService::real(),
}
}
}
impl KVStorage for GitHubStorage {
fn available(&self) -> Result<(), Error> {
if !self.client.get_branches()?.is_empty() {
Ok(())
} else {
Err(Error::InternalError("No branches found.".into()))
}
}
fn get<T: DeserializeOwned>(&self, key: &str) -> Result<GetResponse<T>, Error> {
let data = self.client.get_file(key)?;
let data = base64::decode(&data)?;
let data = std::str::from_utf8(&data).map_err(|e| {
Error::InternalError(format!(
"Unparseable data: {:?}\n returned from Github KV Storage, met Error:{}",
data, e
))
})?;
serde_json::from_str(data).map_err(|e| e.into())
}
fn set<T: Serialize>(&mut self, key: &str, value: T) -> Result<(), Error> {
let now = self.time_service.now_secs();
let data = GetResponse::new(value, now);
let data = serde_json::to_string(&data)?;
let data = base64::encode(&data);
self.client.put(key, &data)?;
Ok(())
}
#[cfg(any(test, feature = "testing"))]
fn reset_and_clear(&mut self) -> Result<(), Error> {
self.client.delete_directory("/").map_err(|e| e.into())
}
}
impl CryptoKVStorage for GitHubStorage {}