From 5e48c5608890031bde42bfe478658a1f61d6f0d3 Mon Sep 17 00:00:00 2001 From: tobinio Date: Sun, 29 Mar 2026 12:40:26 +0200 Subject: [PATCH] actually push /libs --- libs/client/.gitignore | 1 + libs/client/Cargo.toml | 18 ++++++++++++ libs/client/src/fetch.rs | 61 ++++++++++++++++++++++++++++++++++++++++ libs/client/src/lib.rs | 2 ++ libs/client/src/types.rs | 60 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 libs/client/.gitignore create mode 100644 libs/client/Cargo.toml create mode 100644 libs/client/src/fetch.rs create mode 100644 libs/client/src/lib.rs create mode 100644 libs/client/src/types.rs diff --git a/libs/client/.gitignore b/libs/client/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/libs/client/.gitignore @@ -0,0 +1 @@ +/target diff --git a/libs/client/Cargo.toml b/libs/client/Cargo.toml new file mode 100644 index 0000000..82390b2 --- /dev/null +++ b/libs/client/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "client" +version = "0.1.0" +edition = "2024" + +[dependencies] +serde = { version = "1.0.228", features = ["derive"] } +serde_json = "1.0.145" + +chrono = { version = "0.4.42", features = ["serde"] } + +reqwest = "0.12.26" +reqwest-eventsource = "0.6.0" + +tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread"] } +futures = "0.3.31" + +color-eyre = "0.6.5" diff --git a/libs/client/src/fetch.rs b/libs/client/src/fetch.rs new file mode 100644 index 0000000..40bceb1 --- /dev/null +++ b/libs/client/src/fetch.rs @@ -0,0 +1,61 @@ +use color_eyre::eyre; +use futures::stream::StreamExt; +use reqwest_eventsource::{Event, EventSource}; +use std::{thread, time::Duration}; +use tokio::sync::mpsc::{self, Receiver}; + +use crate::types::Todo; + +pub fn get_todos(url: String, token: String) -> Receiver> { + let (tx, rx) = mpsc::channel(8); + + tokio::spawn(async move { + loop { + let todos = loop { + match fetch_todos(&url, &token).await { + Ok(todos) => break todos, + Err(err) => { + eprintln!("Error fetching todos: {}", err); + thread::sleep(Duration::from_secs(5)); + } + } + }; + + tx.send(todos).await.unwrap(); + + let mut event_source = EventSource::new( + reqwest::Client::new() + .get(format!("{}{}", url, "/api/todos/sse")) + .header("Authorization", format!("Bearer {}", token)), + ) + .unwrap(); + while let Some(event) = event_source.next().await { + match event { + Ok(Event::Open) => {} + Ok(Event::Message(message)) => { + let todos = serde_json::from_str(&message.data).unwrap(); + tx.send(todos).await.unwrap(); + } + Err(err) => { + eprintln!("Error: {}", err); + break; + } + } + } + } + }); + + rx +} + +async fn fetch_todos(url: &str, token: &str) -> eyre::Result> { + let response = reqwest::Client::new() + .get(format!("{}{}", url, "/api/todos")) + .header("Authorization", format!("Bearer {}", token)) + .send() + .await? + .text() + .await?; + + Ok(serde_json::from_str(&response)?) +} diff --git a/libs/client/src/lib.rs b/libs/client/src/lib.rs new file mode 100644 index 0000000..cf8ea37 --- /dev/null +++ b/libs/client/src/lib.rs @@ -0,0 +1,2 @@ +pub mod fetch; +pub mod types; diff --git a/libs/client/src/types.rs b/libs/client/src/types.rs new file mode 100644 index 0000000..74a7ade --- /dev/null +++ b/libs/client/src/types.rs @@ -0,0 +1,60 @@ +use chrono::NaiveDate; +use serde::{Deserialize, Serialize}; + +pub type UUID = String; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Todo { + pub uuid: UUID, + pub title: String, + pub note: String, + pub time: Option