Add functions to get and set cookies to low level api

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/3/head
trivernis 3 years ago
parent b3a5b6fee8
commit 5d25aceab5
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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<S: AsRef<str>>(&self, domain: S) -> Result<GetCookiesResponse> {
self.get_and_parse::<GetCookies, [(&str, &str)]>(&[("domain", domain.as_ref())])
.await
}
/// Sets some cookies for some websites.
/// Each entry needs to be in the format `[<name>, <value>, <domain>, <path>, <expires>]`
/// with the types `[String, String, String, String, u64]`
pub async fn set_cookies(&self, cookies: Vec<[OptionalStringNumber; 5]>) -> Result<()> {
self.post::<SetCookies>(SetCookiesRequest { cookies })
.await?;
Ok(())
}
}

@ -60,3 +60,23 @@ pub struct PageInformation {
#[serde(default = "Vec::new")]
pub pages: Vec<PageInformation>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum OptionalStringNumber {
String(String),
Number(u64),
None,
}
impl From<u64> for OptionalStringNumber {
fn from(value: u64) -> Self {
Self::Number(value)
}
}
impl From<String> for OptionalStringNumber {
fn from(value: String) -> Self {
Self::String(value)
}
}

@ -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<S: ToString>(mut self, name: S) -> Self {
self.name = name.to_string().into();
self
}
pub fn value<S: ToString>(mut self, value: S) -> Self {
self.value = value.to_string().into();
self
}
pub fn domain<S: ToString>(mut self, domain: S) -> Self {
self.domain = domain.to_string().into();
self
}
pub fn path<S: ToString>(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]
}
}

@ -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;

@ -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;

@ -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();
}
Loading…
Cancel
Save