mirror of
https://github.com/aljazceru/pubky-core.git
synced 2025-12-31 04:44:37 +01:00
feat(pubky): add an icann reqwest client
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user