Add functions to get and set cookies to low level api
Signed-off-by: trivernis <trivernis@protonmail.com>pull/3/head
parent
b3a5b6fee8
commit
5d25aceab5
@ -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]
|
||||
}
|
||||
}
|
@ -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…
Reference in New Issue