Files
sorsfonalak/crates/horoscope-client/src/lib.rs
T
fecoandClaude Opus 5.5 0358e137f1
CI / test (push) Successful in 3m17s
CI / build (push) Skipped
CI / deploy (push) Skipped
Weekly horoscope: its own page, a Monday e-mail, and one page for every e-mail setting
- The week's sky on the member's own chart (horoscope-api POST /v1/week,
  astro_engine::week): the planets' aspects to the natal points with the day
  each is exact, positions, the Moon's signs, lunar phases, stations and sign
  changes; stored once per chart and week (weekly_skies, migration 0047).
- Its text is a 'weekly' analysis written by the summary's (cheap) model when
  the member opens /heti-horoszkop: one month per page, the newest week
  first, the waiting animation while it is written. In the menu, and linked
  from the chart page.
- Monday 7:00 e-mail (worker task 'weekly'): only that it is there; on by
  default for everyone with a chart, members of today too. Its footer opens
  /ertesitesek?token=…, where the weekly e-mail and every forum e-mail can
  be set without logging in; the mail client's one-click unsubscribe turns
  off the weekly one. Also a switch on the page and on the Profile page.
- Privacy notice (HU/EN) describes it; ratings, admin labels and the model
  page know the new kind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-27 09:40:09 +00:00

110 lines
3.9 KiB
Rust

//! Client of the horoscope-api service (charts, forecasts, the sky by date).
//!
//! Requests and responses are passed as JSON: the horoscope service validates
//! the birth data itself, and its error objects are handed back unchanged so
//! callers can show them to the user.
use std::time::Duration;
use serde_json::Value;
#[derive(Debug, thiserror::Error)]
pub enum HoroscopeError {
/// The service rejected the input (4xx); `body` is its `{"error": {...}}` object.
#[error("horoscope service rejected the request ({status})")]
Rejected { status: u16, body: Value },
/// The service is unreachable or failed (5xx, timeout, invalid response).
#[error("horoscope service unavailable: {0}")]
Unavailable(String),
}
#[derive(Clone)]
pub struct HoroscopeClient {
base_url: String,
http: reqwest::Client,
}
impl HoroscopeClient {
pub fn new(base_url: impl Into<String>) -> Self {
let http = reqwest::Client::builder()
.timeout(Duration::from_secs(15))
.build()
.expect("static reqwest configuration is valid");
Self { base_url: base_url.into().trim_end_matches('/').to_string(), http }
}
/// `POST /v1/natal`
pub async fn natal(&self, request: &Value) -> Result<Value, HoroscopeError> {
self.post("/v1/natal", request).await
}
/// `POST /v1/synastry`
pub async fn synastry(&self, request: &Value) -> Result<Value, HoroscopeError> {
self.post("/v1/synastry", request).await
}
/// `POST /v1/forecast`
pub async fn forecast(&self, request: &Value) -> Result<Value, HoroscopeError> {
self.post("/v1/forecast", request).await
}
/// `POST /v1/week`: one week of a natal chart (the weekly horoscope's sky).
pub async fn week(&self, request: &Value) -> Result<Value, HoroscopeError> {
self.post("/v1/week", request).await
}
/// `GET /v1/sky`: the sky now (planets, retrograde ones, the Moon).
pub async fn sky(&self) -> Result<Value, HoroscopeError> {
self.get("/v1/sky", &[]).await
}
/// `GET /v1/sky/events?year=`: a year's lunar phases, Moon sign changes and retrograde periods.
pub async fn sky_events(&self, year: i32) -> Result<Value, HoroscopeError> {
self.get("/v1/sky/events", &[("year", &year.to_string())]).await
}
/// `GET /v1/countries`
pub async fn countries(&self) -> Result<Value, HoroscopeError> {
self.get("/v1/countries", &[]).await
}
/// `GET /v1/places?country=&q=&limit=`
pub async fn places(&self, country: &str, query: &str, limit: u32) -> Result<Value, HoroscopeError> {
self.get("/v1/places", &[("country", country), ("q", query), ("limit", &limit.to_string())]).await
}
async fn get(&self, path: &str, query: &[(&str, &str)]) -> Result<Value, HoroscopeError> {
let response = self
.http
.get(format!("{}{path}", self.base_url))
.query(query)
.send()
.await
.map_err(|e| HoroscopeError::Unavailable(e.to_string()))?;
Self::answer(response).await
}
async fn post(&self, path: &str, body: &Value) -> Result<Value, HoroscopeError> {
let response = self
.http
.post(format!("{}{path}", self.base_url))
.json(body)
.send()
.await
.map_err(|e| HoroscopeError::Unavailable(e.to_string()))?;
Self::answer(response).await
}
async fn answer(response: reqwest::Response) -> Result<Value, HoroscopeError> {
let status = response.status();
let body: Value = response.json().await.map_err(|e| HoroscopeError::Unavailable(e.to_string()))?;
if status.is_success() {
Ok(body)
} else if status.is_client_error() {
Err(HoroscopeError::Rejected { status: status.as_u16(), body })
} else {
Err(HoroscopeError::Unavailable(format!("{status}: {body}")))
}
}
}