From b9a88672b8734448615354e3f46748d2fdc2f647 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Sun, 1 Oct 2023 00:00:00 +0100 Subject: feat(prs-create) send commit to relay - add client - use client to send event - add async functionality - enabler for relay interaction whilst getting cli input --- src/client.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/client.rs (limited to 'src/client.rs') diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..a6f7dda --- /dev/null +++ b/src/client.rs @@ -0,0 +1,65 @@ +// have you considered + +// TO USE ASYNC + +// in traits (required for mocking unit tests) +// https://rust-lang.github.io/async-book/07_workarounds/05_async_in_traits.html +// https://github.com/dtolnay/async-trait +// see https://blog.rust-lang.org/inside-rust/2022/11/17/async-fn-in-trait-nightly.html +// I think we can use the async-trait crate and switch to the native feature +// which is currently in nightly. alternatively we can use nightly as it looks +// certain that the implementation is going to make it to stable but we don't +// want to inadvertlty use other features of nightly that might be removed. +use anyhow::Result; +use async_trait::async_trait; +#[cfg(test)] +use mockall::*; +use nostr::Event; + +pub struct Client { + client: nostr_sdk::Client, +} + +#[async_trait] +#[cfg_attr(test, automock)] +pub trait Connect { + fn default() -> Self; + fn new(opts: Params) -> Self; + async fn connect(&self) -> Result<()>; + async fn send_event_to(&self, url: &str, event: nostr::event::Event) -> Result; +} + +#[async_trait] +impl Connect for Client { + fn default() -> Self { + Client { + client: nostr_sdk::Client::new(&nostr::Keys::generate()), + } + } + fn new(opts: Params) -> Self { + Client { + client: nostr_sdk::Client::new(&opts.keys.unwrap_or(nostr::Keys::generate())), + } + } + async fn connect(&self) -> Result<()> { + self.client.add_relay("ws://localhost:8080", None).await?; + self.client.connect().await; + // self.client.s + Ok(()) + } + async fn send_event_to(&self, url: &str, event: Event) -> Result { + Ok(self.client.send_event_to(url, event).await?) + } +} + +#[derive(Default)] +pub struct Params { + pub keys: Option, +} + +impl Params { + pub fn with_keys(mut self, keys: nostr::Keys) -> Self { + self.keys = Some(keys); + self + } +} -- cgit v1.2.3