From cd5de8ee1cf2cbcfad38947f4b911e9eb160d155 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 24 Jul 2021 18:24:42 +0200 Subject: [PATCH] Add function to set hydrus user agent Signed-off-by: trivernis --- src/api_core/client.rs | 13 ++++++++++++- .../managing_cookies_and_http_headers.rs | 17 +++++++++++++++++ .../test_managing_cookies_and_http_headers.rs | 9 +++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/api_core/client.rs b/src/api_core/client.rs index 81562a7..832be09 100644 --- a/src/api_core/client.rs +++ b/src/api_core/client.rs @@ -13,7 +13,8 @@ use crate::api_core::adding_urls::{ }; use crate::api_core::common::{FileIdentifier, FileMetadataInfo, FileRecord, OptionalStringNumber}; use crate::api_core::managing_cookies_and_http_headers::{ - GetCookies, GetCookiesResponse, SetCookies, SetCookiesRequest, + GetCookies, GetCookiesResponse, SetCookies, SetCookiesRequest, SetUserAgent, + SetUserAgentRequest, }; use crate::api_core::managing_pages::{ FocusPage, FocusPageRequest, GetPageInfo, GetPageInfoResponse, GetPages, GetPagesResponse, @@ -350,4 +351,14 @@ impl Client { Ok(()) } + + /// Sets the user agent that is being used for every request hydrus starts + pub async fn set_user_agent(&self, user_agent: S) -> Result<()> { + self.post::(SetUserAgentRequest { + user_agent: user_agent.to_string(), + }) + .await?; + + Ok(()) + } } diff --git a/src/api_core/managing_cookies_and_http_headers.rs b/src/api_core/managing_cookies_and_http_headers.rs index 3488034..1587657 100644 --- a/src/api_core/managing_cookies_and_http_headers.rs +++ b/src/api_core/managing_cookies_and_http_headers.rs @@ -83,3 +83,20 @@ impl CookieBuilder { [self.name, self.value, self.domain, self.path, self.expires] } } + +#[derive(Clone, Debug, Serialize)] +pub struct SetUserAgentRequest { + #[serde(rename = "user-agent")] + pub user_agent: String, +} + +pub struct SetUserAgent; + +impl Endpoint for SetUserAgent { + type Request = SetUserAgentRequest; + type Response = (); + + fn path() -> String { + String::from("manage_headers/set_user_agent") + } +} diff --git a/tests/client/test_managing_cookies_and_http_headers.rs b/tests/client/test_managing_cookies_and_http_headers.rs index e80203e..31e7b1e 100644 --- a/tests/client/test_managing_cookies_and_http_headers.rs +++ b/tests/client/test_managing_cookies_and_http_headers.rs @@ -18,3 +18,12 @@ async fn it_sets_cookies_for_a_domain() { .build(); client.set_cookies(vec![cookie]).await.unwrap(); } + +#[tokio::test] +async fn it_sets_the_user_agent() { + let client = common::get_client(); + client + .set_user_agent("Mozilla/5.0 (compatible; Hydrus Client)") + .await + .unwrap(); +}