feat(pubky): add an icann reqwest client

This commit is contained in:
nazeh
2024-11-26 13:36:16 +03:00
parent c9ae34584c
commit e6f7fb3347
3 changed files with 16 additions and 5 deletions

View File

@@ -26,6 +26,7 @@ pub struct Client {
#[cfg(not(target_arch = "wasm32"))]
pub(crate) cookie_store: Arc<native::CookieJar>,
http: reqwest::Client,
icann_http: reqwest::Client,
pub(crate) pkarr: pkarr::Client,
}

View File

@@ -52,17 +52,26 @@ impl Settings {
let cookie_store = Arc::new(CookieJar::default());
// TODO: allow custom user agent, but force a Pubky user agent information
let user_agent = DEFAULT_USER_AGENT;
let http = reqwest::ClientBuilder::from(pkarr.clone())
// TODO: use persistent cookie jar
.cookie_provider(cookie_store.clone())
// TODO: allow custom user agent, but force a Pubky user agent information
.user_agent(DEFAULT_USER_AGENT)
.user_agent(user_agent)
.build()
.expect("config expected to not error");
let icann_http = reqwest::ClientBuilder::new()
.cookie_provider(cookie_store.clone())
.user_agent(user_agent)
.build()
.expect("config expected to not error");
Ok(Client {
cookie_store,
http,
icann_http,
pkarr,
})
}

View File

@@ -1,5 +1,6 @@
//! HTTP methods that support `https://` with Pkarr domains, and `pubky://` URLs
use pkarr::PublicKey;
use reqwest::{IntoUrl, Method, RequestBuilder};
use crate::Client;
@@ -27,6 +28,8 @@ impl Client {
let url = format!("https://_pubky.{}", url.split_at(8).1);
return self.http.request(method, url);
} else if url.starts_with("https://") && PublicKey::try_from(url).is_err() {
return self.icann_http.request(method, url);
}
self.http.request(method, url)
@@ -152,10 +155,8 @@ mod tests {
let client = Client::test(&testnet);
let url = format!("http://example.com/");
let response = client
.request(Default::default(), url)
.request(Default::default(), "https://example.com/")
.send()
.await
.unwrap();