-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub_client.rs
More file actions
84 lines (72 loc) · 2.12 KB
/
github_client.rs
File metadata and controls
84 lines (72 loc) · 2.12 KB
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
78
79
80
81
82
83
84
use reqwest::{header::USER_AGENT, Url};
use tokio::{fs, join};
use uuid::Uuid;
use std::path::{Path, PathBuf};
use serde::Deserialize;
use crate::{path_parser::RequestMetaData, directory::Directory};
#[derive(Debug, Deserialize)]
pub struct ResponseObject {
pub name: String,
pub path: String,
pub sha: String,
pub url: String,
pub download_url: Option<String>,
pub r#type: String,
pub size: u32
}
/* todo: add tests for utilities */
pub async fn get_from_github_api(valid_url: &Url) -> Vec<ResponseObject>{
let client = reqwest::Client::new();
println!("url, {:#?}", valid_url.as_str());
client.get(valid_url.as_str()) // hacky but works for now
.header(USER_AGENT, "nsrcodes")
.send()
.await
.unwrap()
.json::<Vec<ResponseObject> >()
.await
.unwrap()
}
fn generate_uuid() -> String {
return Uuid::new_v4().to_string();
}
#[derive(Debug)]
pub struct GithubData {
pub id: String,
pub path: PathBuf,
pub res_path: PathBuf,
}
impl GithubData {
pub fn new(request: RequestMetaData) -> (Self, Directory) {
let uuid = generate_uuid();
let mut work_path = Path::new(".").join("tmp");
work_path.push(&uuid);
let name = match request.path.as_str().rsplit_once("/") {
Some((_, name)) => name,
None => request.path.as_str(),
};
let path = work_path.join(&name);
let result_dir = Directory::new(name.to_string(), path, request.api_target());
(GithubData {
path: work_path,
res_path: Path::new(".").join("tmp").join(format!("{}.tar.gz",&uuid)),
id: uuid,
}, result_dir)
}
pub async fn clean(self) {
let _ = join!(
fs::remove_dir_all(&self.path),
fs::remove_file(&self.res_path)
);
}
}
#[cfg(test)]
mod github_client_tests {
// write a dummy Github data struct creator
#[test]
fn deserielize_github_response() {
// todo
// give the function an example github response
// compare it with the expected response struct object
}
}