longbridge/content/
context.rs

1use std::sync::Arc;
2
3use longbridge_httpcli::{HttpClient, Json, Method};
4use serde::Deserialize;
5
6use super::types::{NewsItem, TopicItem};
7use crate::{Config, Result};
8
9struct InnerContentContext {
10    http_cli: HttpClient,
11}
12
13/// Content context
14#[derive(Clone)]
15pub struct ContentContext(Arc<InnerContentContext>);
16
17impl ContentContext {
18    /// Create a `ContentContext`
19    pub fn new(config: Arc<Config>) -> Self {
20        Self(Arc::new(InnerContentContext {
21            http_cli: config.create_http_client(),
22        }))
23    }
24
25    /// Get discussion topics list
26    pub async fn topics(&self, symbol: impl Into<String>) -> Result<Vec<TopicItem>> {
27        #[derive(Debug, Deserialize)]
28        struct Response {
29            items: Vec<TopicItem>,
30        }
31
32        let symbol = symbol.into();
33        Ok(self
34            .0
35            .http_cli
36            .request(Method::GET, format!("/v1/content/{symbol}/topics"))
37            .response::<Json<Response>>()
38            .send()
39            .await?
40            .0
41            .items)
42    }
43
44    /// Get news list
45    pub async fn news(&self, symbol: impl Into<String>) -> Result<Vec<NewsItem>> {
46        #[derive(Debug, Deserialize)]
47        struct Response {
48            items: Vec<NewsItem>,
49        }
50
51        let symbol = symbol.into();
52        Ok(self
53            .0
54            .http_cli
55            .request(Method::GET, format!("/v1/content/{symbol}/news"))
56            .response::<Json<Response>>()
57            .send()
58            .await?
59            .0
60            .items)
61    }
62}