diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-10-01 00:00:00 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-10-01 00:00:00 +0100 |
| commit | b9a88672b8734448615354e3f46748d2fdc2f647 (patch) | |
| tree | fd9b55c4ea8a132ab31abec3b6b8a4833c9bd8ff /src/client.rs | |
| parent | 5dce8a09f0f980b878bd02b7e96fc001155492ec (diff) | |
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
Diffstat (limited to 'src/client.rs')
| -rw-r--r-- | src/client.rs | 65 |
1 files changed, 65 insertions, 0 deletions
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 @@ | |||
| 1 | // have you considered | ||
| 2 | |||
| 3 | // TO USE ASYNC | ||
| 4 | |||
| 5 | // in traits (required for mocking unit tests) | ||
| 6 | // https://rust-lang.github.io/async-book/07_workarounds/05_async_in_traits.html | ||
| 7 | // https://github.com/dtolnay/async-trait | ||
| 8 | // see https://blog.rust-lang.org/inside-rust/2022/11/17/async-fn-in-trait-nightly.html | ||
| 9 | // I think we can use the async-trait crate and switch to the native feature | ||
| 10 | // which is currently in nightly. alternatively we can use nightly as it looks | ||
| 11 | // certain that the implementation is going to make it to stable but we don't | ||
| 12 | // want to inadvertlty use other features of nightly that might be removed. | ||
| 13 | use anyhow::Result; | ||
| 14 | use async_trait::async_trait; | ||
| 15 | #[cfg(test)] | ||
| 16 | use mockall::*; | ||
| 17 | use nostr::Event; | ||
| 18 | |||
| 19 | pub struct Client { | ||
| 20 | client: nostr_sdk::Client, | ||
| 21 | } | ||
| 22 | |||
| 23 | #[async_trait] | ||
| 24 | #[cfg_attr(test, automock)] | ||
| 25 | pub trait Connect { | ||
| 26 | fn default() -> Self; | ||
| 27 | fn new(opts: Params) -> Self; | ||
| 28 | async fn connect(&self) -> Result<()>; | ||
| 29 | async fn send_event_to(&self, url: &str, event: nostr::event::Event) -> Result<nostr::EventId>; | ||
| 30 | } | ||
| 31 | |||
| 32 | #[async_trait] | ||
| 33 | impl Connect for Client { | ||
| 34 | fn default() -> Self { | ||
| 35 | Client { | ||
| 36 | client: nostr_sdk::Client::new(&nostr::Keys::generate()), | ||
| 37 | } | ||
| 38 | } | ||
| 39 | fn new(opts: Params) -> Self { | ||
| 40 | Client { | ||
| 41 | client: nostr_sdk::Client::new(&opts.keys.unwrap_or(nostr::Keys::generate())), | ||
| 42 | } | ||
| 43 | } | ||
| 44 | async fn connect(&self) -> Result<()> { | ||
| 45 | self.client.add_relay("ws://localhost:8080", None).await?; | ||
| 46 | self.client.connect().await; | ||
| 47 | // self.client.s | ||
| 48 | Ok(()) | ||
| 49 | } | ||
| 50 | async fn send_event_to(&self, url: &str, event: Event) -> Result<nostr::EventId> { | ||
| 51 | Ok(self.client.send_event_to(url, event).await?) | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | #[derive(Default)] | ||
| 56 | pub struct Params { | ||
| 57 | pub keys: Option<nostr::Keys>, | ||
| 58 | } | ||
| 59 | |||
| 60 | impl Params { | ||
| 61 | pub fn with_keys(mut self, keys: nostr::Keys) -> Self { | ||
| 62 | self.keys = Some(keys); | ||
| 63 | self | ||
| 64 | } | ||
| 65 | } | ||