Add searching and fetching of files
Signed-off-by: trivernis <trivernis@protonmail.com>pull/2/head
parent
2c5b3225f0
commit
5563d61155
@ -0,0 +1,77 @@
|
||||
use crate::endpoints::common::FileMetadataInfo;
|
||||
use crate::endpoints::Endpoint;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct SearchFilesResponse {
|
||||
pub file_ids: Vec<u64>,
|
||||
}
|
||||
|
||||
pub enum FileSearchLocation {
|
||||
All,
|
||||
Inbox,
|
||||
Archive,
|
||||
}
|
||||
|
||||
impl FileSearchLocation {
|
||||
pub fn is_inbox(&self) -> bool {
|
||||
if let &Self::Inbox = &self {
|
||||
true
|
||||
} else {
|
||||
self.is_all()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_all(&self) -> bool {
|
||||
if let &Self::All = &self {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_archive(&self) -> bool {
|
||||
if let &Self::Archive = &self {
|
||||
true
|
||||
} else {
|
||||
self.is_all()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SearchFiles;
|
||||
|
||||
impl Endpoint for SearchFiles {
|
||||
type Request = ();
|
||||
type Response = SearchFilesResponse;
|
||||
|
||||
fn get_path() -> String {
|
||||
String::from("get_files/search_files")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Deserialize)]
|
||||
pub struct FileMetadataResponse {
|
||||
metadata: Vec<FileMetadataInfo>,
|
||||
}
|
||||
|
||||
pub struct FileMetadata;
|
||||
|
||||
impl Endpoint for FileMetadata {
|
||||
type Request = ();
|
||||
type Response = FileMetadataResponse;
|
||||
|
||||
fn get_path() -> String {
|
||||
String::from("get_files/file_metadata")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GetFile;
|
||||
|
||||
impl Endpoint for GetFile {
|
||||
type Request = ();
|
||||
type Response = ();
|
||||
|
||||
fn get_path() -> String {
|
||||
String::from("get_files/file")
|
||||
}
|
||||
}
|
@ -1,3 +1,14 @@
|
||||
pub fn string_list_to_json_array(l: Vec<String>) -> String {
|
||||
format!("[\"{}\"]", l.join("\",\""))
|
||||
}
|
||||
|
||||
pub fn number_list_to_json_array<T: ToString>(l: Vec<T>) -> String {
|
||||
format!(
|
||||
"[{}]",
|
||||
l.into_iter().fold(String::from(""), |acc, val| format!(
|
||||
"{},{}",
|
||||
acc,
|
||||
val.to_string()
|
||||
))
|
||||
)
|
||||
}
|
||||
|
@ -0,0 +1,37 @@
|
||||
use hydrus_api::endpoints::common::FileIdentifier;
|
||||
use hydrus_api::endpoints::searching_and_fetching_files::FileSearchLocation;
|
||||
|
||||
mod common;
|
||||
|
||||
#[tokio::test]
|
||||
async fn is_searches_files() {
|
||||
let mut client = common::get_client();
|
||||
client
|
||||
.search_files(vec!["beach".to_string()], FileSearchLocation::Archive)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_fetches_file_metadata() {
|
||||
let mut client = common::get_client();
|
||||
client
|
||||
.get_file_metadata(
|
||||
vec![],
|
||||
vec!["0000000000000000000000000000000000000000000000000000000000000000".to_string()],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_fetches_single_files() {
|
||||
let mut client = common::get_client();
|
||||
let response = client
|
||||
.get_file(FileIdentifier::Hash(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000".to_string(),
|
||||
))
|
||||
.await;
|
||||
|
||||
assert!(response.is_err()); // can't find the file
|
||||
}
|
Loading…
Reference in New Issue