From 5d25aceab57fa01c299fcc84ac2a9470342252b3 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 24 Jul 2021 15:58:57 +0200 Subject: [PATCH] Add functions to get and set cookies to low level api Signed-off-by: trivernis --- src/api_core/client.rs | 21 ++++- src/api_core/common.rs | 20 +++++ .../managing_cookies_and_http_headers.rs | 85 +++++++++++++++++++ src/api_core/mod.rs | 1 + tests/client/mod.rs | 1 + .../test_managing_cookies_and_http_headers.rs | 20 +++++ 6 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 src/api_core/managing_cookies_and_http_headers.rs create mode 100644 tests/client/test_managing_cookies_and_http_headers.rs diff --git a/src/api_core/client.rs b/src/api_core/client.rs index 13cd91d..81562a7 100644 --- a/src/api_core/client.rs +++ b/src/api_core/client.rs @@ -11,7 +11,10 @@ use crate::api_core::adding_urls::{ AddUrl, AddUrlRequest, AddUrlResponse, AssociateUrl, AssociateUrlRequest, GetUrlFiles, GetUrlFilesResponse, GetUrlInfo, GetUrlInfoResponse, }; -use crate::api_core::common::{FileIdentifier, FileMetadataInfo, FileRecord}; +use crate::api_core::common::{FileIdentifier, FileMetadataInfo, FileRecord, OptionalStringNumber}; +use crate::api_core::managing_cookies_and_http_headers::{ + GetCookies, GetCookiesResponse, SetCookies, SetCookiesRequest, +}; use crate::api_core::managing_pages::{ FocusPage, FocusPageRequest, GetPageInfo, GetPageInfoResponse, GetPages, GetPagesResponse, }; @@ -331,4 +334,20 @@ impl Client { Ok(()) } + + /// Returns all cookies for the given domain + pub async fn get_cookies>(&self, domain: S) -> Result { + self.get_and_parse::(&[("domain", domain.as_ref())]) + .await + } + + /// Sets some cookies for some websites. + /// Each entry needs to be in the format `[, , , , ]` + /// with the types `[String, String, String, String, u64]` + pub async fn set_cookies(&self, cookies: Vec<[OptionalStringNumber; 5]>) -> Result<()> { + self.post::(SetCookiesRequest { cookies }) + .await?; + + Ok(()) + } } diff --git a/src/api_core/common.rs b/src/api_core/common.rs index 20c2c6c..3a18ba5 100644 --- a/src/api_core/common.rs +++ b/src/api_core/common.rs @@ -60,3 +60,23 @@ pub struct PageInformation { #[serde(default = "Vec::new")] pub pages: Vec, } + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(untagged)] +pub enum OptionalStringNumber { + String(String), + Number(u64), + None, +} + +impl From for OptionalStringNumber { + fn from(value: u64) -> Self { + Self::Number(value) + } +} + +impl From for OptionalStringNumber { + fn from(value: String) -> Self { + Self::String(value) + } +} diff --git a/src/api_core/managing_cookies_and_http_headers.rs b/src/api_core/managing_cookies_and_http_headers.rs new file mode 100644 index 0000000..3488034 --- /dev/null +++ b/src/api_core/managing_cookies_and_http_headers.rs @@ -0,0 +1,85 @@ +use crate::api_core::common::OptionalStringNumber; +use crate::api_core::Endpoint; + +#[derive(Clone, Debug, Deserialize)] +pub struct GetCookiesResponse { + pub cookies: Vec<[OptionalStringNumber; 5]>, +} + +pub struct GetCookies; + +impl Endpoint for GetCookies { + type Request = (); + type Response = GetCookiesResponse; + + fn path() -> String { + String::from("manage_cookies/get_cookies") + } +} + +#[derive(Clone, Debug, Serialize)] +pub struct SetCookiesRequest { + pub cookies: Vec<[OptionalStringNumber; 5]>, +} + +pub struct SetCookies; + +impl Endpoint for SetCookies { + type Request = SetCookiesRequest; + type Response = (); + + fn path() -> String { + String::from("manage_cookies/set_cookies") + } +} + +pub struct CookieBuilder { + name: OptionalStringNumber, + value: OptionalStringNumber, + domain: OptionalStringNumber, + path: OptionalStringNumber, + expires: OptionalStringNumber, +} + +impl Default for CookieBuilder { + fn default() -> Self { + Self { + name: String::new().into(), + value: String::new().into(), + domain: String::new().into(), + path: String::new().into(), + expires: OptionalStringNumber::None, + } + } +} + +impl CookieBuilder { + pub fn name(mut self, name: S) -> Self { + self.name = name.to_string().into(); + self + } + + pub fn value(mut self, value: S) -> Self { + self.value = value.to_string().into(); + self + } + + pub fn domain(mut self, domain: S) -> Self { + self.domain = domain.to_string().into(); + self + } + + pub fn path(mut self, path: S) -> Self { + self.path = path.to_string().into(); + self + } + + pub fn expires(mut self, expires: u64) -> Self { + self.expires = expires.into(); + self + } + + pub fn build(self) -> [OptionalStringNumber; 5] { + [self.name, self.value, self.domain, self.path, self.expires] + } +} diff --git a/src/api_core/mod.rs b/src/api_core/mod.rs index f9e58a2..b046348 100644 --- a/src/api_core/mod.rs +++ b/src/api_core/mod.rs @@ -7,6 +7,7 @@ pub mod adding_tags; pub mod adding_urls; pub mod client; pub mod common; +pub mod managing_cookies_and_http_headers; pub mod managing_pages; pub mod searching_and_fetching_files; diff --git a/tests/client/mod.rs b/tests/client/mod.rs index 8f2f88c..8c61b9d 100644 --- a/tests/client/mod.rs +++ b/tests/client/mod.rs @@ -2,5 +2,6 @@ mod test_access_management; mod test_adding_files; mod test_adding_tags; mod test_adding_urls; +mod test_managing_cookies_and_http_headers; mod test_managing_pages; mod test_searching_and_fetching_files; diff --git a/tests/client/test_managing_cookies_and_http_headers.rs b/tests/client/test_managing_cookies_and_http_headers.rs new file mode 100644 index 0000000..e80203e --- /dev/null +++ b/tests/client/test_managing_cookies_and_http_headers.rs @@ -0,0 +1,20 @@ +use super::super::common; +use hydrus_api::api_core::managing_cookies_and_http_headers::CookieBuilder; + +#[tokio::test] +async fn it_returns_cookies_for_a_domain() { + let client = common::get_client(); + client.get_cookies("trivernis.net").await.unwrap(); +} + +#[tokio::test] +async fn it_sets_cookies_for_a_domain() { + let client = common::get_client(); + let cookie = CookieBuilder::default() + .name("my_cookie") + .value("my_value") + .domain("trivernis.net") + .path("/") + .build(); + client.set_cookies(vec![cookie]).await.unwrap(); +}