From 620e2ad2ee6e25f26b16ab2136bb94c1b05bbbd2 Mon Sep 17 00:00:00 2001 From: nazeh Date: Sun, 25 Aug 2024 11:54:24 +0300 Subject: [PATCH] feat(pubky): add PubkyClientBuilder --- pubky/src/native.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/pubky/src/native.rs b/pubky/src/native.rs index 0ca2d7c..547f8db 100644 --- a/pubky/src/native.rs +++ b/pubky/src/native.rs @@ -27,6 +27,33 @@ impl Default for PubkyClient { } } +#[derive(Debug, Default)] +pub struct PubkyClientBuilder { + pkarr_settings: Option, +} + +impl PubkyClientBuilder { + /// Set Pkarr client [pkarr::Settings]. + pub fn pkarr_settings(mut self, settings: pkarr::Settings) -> Self { + self.pkarr_settings = settings.into(); + self + } + + /// Build [PubkyClient] + pub fn build(self) -> PubkyClient { + PubkyClient { + http: reqwest::Client::builder() + .cookie_store(true) + .user_agent(DEFAULT_USER_AGENT) + .build() + .unwrap(), + pkarr: PkarrClient::new(self.pkarr_settings.unwrap_or_default()) + .unwrap() + .as_async(), + } + } +} + // === Public API === impl PubkyClient { @@ -37,11 +64,15 @@ impl PubkyClient { .user_agent(DEFAULT_USER_AGENT) .build() .unwrap(), - #[cfg(not(target_arch = "wasm32"))] pkarr: PkarrClient::new(Default::default()).unwrap().as_async(), } } + /// Returns a builder to edit settings before creating [PubkyClient]. + pub fn builder() -> PubkyClientBuilder { + PubkyClientBuilder::default() + } + pub fn test(testnet: &Testnet) -> Self { Self { http: reqwest::Client::builder()