From d4c4d9465f0bcb3ebad219e9e63e2cb7a037a03a Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 7 Apr 2022 19:22:20 +0200 Subject: [PATCH] Control response content type via Accept header Signed-off-by: trivernis --- Cargo.toml | 4 +-- src/api_core/client.rs | 15 +++++++++-- .../endpoints/searching_and_fetching_files.rs | 2 +- src/wrapper/hydrus_file.rs | 3 --- .../test_searching_and_fetching_files.rs | 26 ++++++++++++++----- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bcad5e6..ba7a232 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydrus-api" -version = "0.9.0" +version = "0.9.1" authors = ["trivernis "] edition = "2018" license = "Apache-2.0" @@ -12,7 +12,7 @@ repository = "https://github.com/trivernis/hydrus-api-rs" [dependencies] serde = { version = "1.0.136", features = ["derive"] } -reqwest = { version = "0.11.10", features = ["json"]} +reqwest = { version = "0.11.10"} tracing = "0.1.32" mime = "0.3.16" chrono = "0.4.19" diff --git a/src/api_core/client.rs b/src/api_core/client.rs index 8c08187..3df1477 100644 --- a/src/api_core/client.rs +++ b/src/api_core/client.rs @@ -43,6 +43,8 @@ use std::fmt::Debug; const ACCESS_KEY_HEADER: &str = "Hydrus-Client-API-Access-Key"; const CONTENT_TYPE_HEADER: &str = "Content-Type"; +const ACCEPT_HEADER: &str = "Accept"; + #[cfg(feature = "cbor")] const CONTENT_TYPE_CBOR: &str = "application/cbor"; #[cfg(feature = "json")] @@ -455,6 +457,7 @@ impl Client { .get(format!("{}/{}", self.base_url, E::path())) .header(ACCESS_KEY_HEADER, &self.access_key) .header(CONTENT_TYPE_HEADER, content_type) + .header(ACCEPT_HEADER, content_type) .query(query) .query(¶ms) .send() @@ -509,7 +512,8 @@ impl Client { .post(format!("{}/{}", self.base_url, E::path())) .body(body) .header(ACCESS_KEY_HEADER, &self.access_key) - .header("Content-Type", content_type) + .header(CONTENT_TYPE_HEADER, content_type) + .header(ACCEPT_HEADER, content_type) .send() .await?; let response = Self::extract_error(response).await?; @@ -551,17 +555,24 @@ impl Client { #[tracing::instrument(skip(self, data), level = "trace")] async fn post_binary(&self, data: Vec) -> Result { tracing::trace!("Binary POST request to {}", E::path()); + + #[cfg(feature = "cbor")] + let content_type = CONTENT_TYPE_CBOR; + #[cfg(feature = "json")] + let content_type = CONTENT_TYPE_JSON; + let response = self .inner .post(format!("{}/{}", self.base_url, E::path())) .body(data) .header(ACCESS_KEY_HEADER, &self.access_key) .header(CONTENT_TYPE_HEADER, "application/octet-stream") + .header(ACCEPT_HEADER, content_type) .send() .await?; let response = Self::extract_error(response).await?; - response.json::().await.map_err(Error::from) + Self::extract_content(response).await } /// Returns an error with the response text content if the status doesn't indicate success diff --git a/src/api_core/endpoints/searching_and_fetching_files.rs b/src/api_core/endpoints/searching_and_fetching_files.rs index e7324c6..06d95c2 100644 --- a/src/api_core/endpoints/searching_and_fetching_files.rs +++ b/src/api_core/endpoints/searching_and_fetching_files.rs @@ -192,7 +192,6 @@ pub struct FileBasicMetadata { pub height: Option, pub duration: Option, pub time_modified: Option, - pub file_services: FileMetadataServices, pub has_audio: Option, pub num_frames: Option, pub num_words: Option, @@ -205,6 +204,7 @@ pub struct FileFullMetadata { pub is_inbox: bool, pub is_local: bool, pub is_trashed: bool, + pub file_services: FileMetadataServices, pub known_urls: Vec, #[deprecated] pub service_names_to_statuses_to_tags: HashMap>>, diff --git a/src/wrapper/hydrus_file.rs b/src/wrapper/hydrus_file.rs index ff61b93..fd9ab1e 100644 --- a/src/wrapper/hydrus_file.rs +++ b/src/wrapper/hydrus_file.rs @@ -206,14 +206,12 @@ impl HydrusFile { ) -> Result> { let metadata = self.metadata().await?; let naive_time_imported = metadata - .basic_metadata .file_services .current .get(service_key.as_ref()) .map(|s| s.time_imported) .or_else(|| { metadata - .basic_metadata .file_services .deleted .get(service_key.as_ref()) @@ -231,7 +229,6 @@ impl HydrusFile { ) -> Result> { let metadata = self.metadata().await?; let naive_time_deleted = metadata - .basic_metadata .file_services .deleted .get(service_key.as_ref()) diff --git a/tests/client/test_searching_and_fetching_files.rs b/tests/client/test_searching_and_fetching_files.rs index 134912b..3090a8a 100644 --- a/tests/client/test_searching_and_fetching_files.rs +++ b/tests/client/test_searching_and_fetching_files.rs @@ -1,9 +1,12 @@ use super::super::common; +use crate::common::create_testdata; use hydrus_api::api_core::common::FileIdentifier; use hydrus_api::api_core::endpoints::searching_and_fetching_files::file_sort_type::SORT_FILE_PIXEL_COUNT; use hydrus_api::api_core::endpoints::searching_and_fetching_files::{ BasicMetadata, FileSearchOptions, FullMetadata, Identifiers, SearchQueryEntry, }; +use hydrus_api::wrapper::builders::tag_builder::SystemTagBuilder; +use hydrus_api::wrapper::service::ServiceName; #[tokio::test] async fn is_searches_files() { @@ -58,18 +61,29 @@ async fn it_fetches_file_metadata() { #[tokio::test] async fn it_fetches_file_metadata_by_id() { let client = common::get_client(); + create_testdata(&client).await; + let mut files = client + .search_files( + vec![SearchQueryEntry::Tag( + SystemTagBuilder::new().everything().build().to_string(), + )], + FileSearchOptions::default().file_service_name(ServiceName::my_files()), + ) + .await + .unwrap(); + let test_id = files.file_ids.pop().unwrap(); let response = client - .get_file_metadata::(vec![1], vec![]) + .get_file_metadata::(vec![test_id], vec![]) .await; - assert!(response.is_ok()); + response.unwrap(); let response = client - .get_file_metadata::(vec![1], vec![]) + .get_file_metadata::(vec![test_id], vec![]) .await; - assert!(response.is_ok()); + response.unwrap(); let response = client - .get_file_metadata::(vec![1], vec![]) + .get_file_metadata::(vec![test_id], vec![]) .await; - assert!(response.is_ok()); + response.unwrap(); } #[tokio::test]