Add wrapper for addresses to set and get cookies

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

@ -80,3 +80,21 @@ impl From<String> for OptionalStringNumber {
Self::String(value) Self::String(value)
} }
} }
impl OptionalStringNumber {
pub fn string(&self) -> Option<&str> {
if let Self::String(s) = &self {
Some(s)
} else {
None
}
}
pub fn number(&self) -> Option<u64> {
if let Self::Number(n) = &self {
Some(*n)
} else {
None
}
}
}

@ -0,0 +1,99 @@
use crate::api_core::common::OptionalStringNumber;
use crate::api_core::managing_cookies_and_http_headers::CookieBuilder;
use crate::error::Result;
use crate::Client;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
pub struct Address {
client: Client,
domain: String,
path: String,
}
impl Address {
pub(crate) fn from_str(client: Client, domain: &str) -> Self {
let (domain, path) = domain.split_once("/").unwrap_or((domain, "/"));
Self {
client,
domain: domain.to_string(),
path: path.to_string(),
}
}
/// Returns the path after the domain name
pub fn path(&self) -> &str {
&self.path
}
/// Sets the path of the domain that can be used for setting cookies
pub fn set_path<S: ToString>(&mut self, path: S) {
self.path = path.to_string();
}
/// Sets cookies for the domain
pub async fn set_cookies(&self, cookies: Vec<DomainCookie>) -> Result<()> {
let cookies = cookies
.into_iter()
.map(|cookie| {
let mut builder = CookieBuilder::default()
.domain(&self.domain)
.path(&self.path)
.name(cookie.name)
.value(cookie.value);
if let Some(expires) = cookie.expires {
builder =
builder.expires(expires.duration_since(UNIX_EPOCH).unwrap().as_secs());
}
builder.build()
})
.collect();
self.client.set_cookies(cookies).await
}
/// Returns all cookies stored for this domain
pub async fn get_cookies(&self) -> Result<Vec<DomainCookie>> {
let response = self.client.get_cookies(&self.domain).await?;
let cookies = response
.cookies
.into_iter()
.map(DomainCookie::from)
.collect();
Ok(cookies)
}
}
#[derive(Clone, Debug)]
pub struct DomainCookie {
pub name: String,
pub value: String,
pub expires: Option<SystemTime>,
}
impl DomainCookie {
/// Creates a new cookie that will be expire after the given instant or only last for the session
pub fn new<S1: ToString, S2: ToString>(
name: S1,
value: S2,
expires: Option<SystemTime>,
) -> Self {
Self {
name: name.to_string(),
value: value.to_string(),
expires,
}
}
}
impl From<[OptionalStringNumber; 5]> for DomainCookie {
fn from(cookie_entry: [OptionalStringNumber; 5]) -> Self {
let name = cookie_entry[0].string().unwrap_or("");
let value = cookie_entry[1].string().unwrap_or("");
let expires = cookie_entry[4]
.number()
.map(|n| UNIX_EPOCH + Duration::from_secs(n));
Self::new(name, value, expires)
}
}

@ -2,6 +2,7 @@ use crate::api_core::common::FileIdentifier;
use crate::api_core::searching_and_fetching_files::FileSearchLocation; use crate::api_core::searching_and_fetching_files::FileSearchLocation;
use crate::error::Result; use crate::error::Result;
use crate::utils::tag_list_to_string_list; use crate::utils::tag_list_to_string_list;
use crate::wrapper::address::Address;
use crate::wrapper::builders::import_builder::ImportBuilder; use crate::wrapper::builders::import_builder::ImportBuilder;
use crate::wrapper::builders::tagging_builder::TaggingBuilder; use crate::wrapper::builders::tagging_builder::TaggingBuilder;
use crate::wrapper::hydrus_file::HydrusFile; use crate::wrapper::hydrus_file::HydrusFile;
@ -47,6 +48,11 @@ impl Hydrus {
} }
} }
/// Returns the address as an object that can be used to get and set cookies
pub fn address<S: AsRef<str>>(&self, address: S) -> Address {
Address::from_str(self.client.clone(), address.as_ref())
}
/// Returns information about a given url in an object that allows /// Returns information about a given url in an object that allows
/// further operations with that url /// further operations with that url
pub async fn url<S: AsRef<str>>(&self, url: S) -> Result<Url> { pub async fn url<S: AsRef<str>>(&self, url: S) -> Result<Url> {

@ -1,3 +1,4 @@
pub mod address;
pub mod builders; pub mod builders;
pub mod hydrus; pub mod hydrus;
pub mod hydrus_file; pub mod hydrus_file;

@ -2,6 +2,7 @@ use crate::api_core::adding_urls::{
URL_TYPE_FILE, URL_TYPE_GALLERY, URL_TYPE_POST, URL_TYPE_WATCHABLE, URL_TYPE_FILE, URL_TYPE_GALLERY, URL_TYPE_POST, URL_TYPE_WATCHABLE,
}; };
use crate::error::Result; use crate::error::Result;
use crate::wrapper::address::Address;
use crate::wrapper::builders::import_builder::UrlImportBuilder; use crate::wrapper::builders::import_builder::UrlImportBuilder;
use crate::wrapper::hydrus_file::HydrusFile; use crate::wrapper::hydrus_file::HydrusFile;
use crate::Client; use crate::Client;
@ -59,6 +60,16 @@ impl Url {
UrlImportBuilder::new(self.client.clone(), &self.url) UrlImportBuilder::new(self.client.clone(), &self.url)
} }
/// Returns the address to manipulate cookies for this url
pub fn address(&self) -> Address {
let url = self
.normalised_url
.trim_start_matches("http://")
.trim_start_matches("https://");
Address::from_str(self.client.clone(), url)
}
/// Associates the url with a list of file hashes /// Associates the url with a list of file hashes
pub async fn associate(&mut self, hashes: Vec<String>) -> Result<()> { pub async fn associate(&mut self, hashes: Vec<String>) -> Result<()> {
self.client self.client

@ -3,3 +3,4 @@ mod test_hydrus;
mod test_import; mod test_import;
mod test_url; mod test_url;
mod test_page; mod test_page;
mod test_address;

@ -0,0 +1,31 @@
use super::super::common;
use hydrus_api::wrapper::address::{Address, DomainCookie};
use std::time::{Duration, SystemTime};
fn get_address() -> Address {
let hydrus = common::get_hydrus();
hydrus.address("trivernis.net/some/path")
}
#[tokio::test]
async fn it_sets_cookies() {
let address = get_address();
address
.set_cookies(vec![
DomainCookie::new("name", "value", None),
DomainCookie::new(
"name2",
"value2",
Some(SystemTime::now() + Duration::from_secs(30)),
),
])
.await
.unwrap();
}
#[tokio::test]
async fn it_retrieves_cookies() {
let address = get_address();
address.get_cookies().await.unwrap();
}
Loading…
Cancel
Save