use std::sync::Arc;
use longbridge_httpcli::{HttpClient, Json, Method};
use serde::Deserialize;
use super::types::{
CreateReplyOptions, CreateTopicOptions, ListTopicRepliesOptions, MyTopicsOptions, NewsItem,
OwnedTopic, TopicItem, TopicReply,
};
use crate::{Config, Result};
struct InnerContentContext {
http_cli: HttpClient,
}
#[derive(Clone)]
pub struct ContentContext(Arc<InnerContentContext>);
impl ContentContext {
pub fn new(config: Arc<Config>) -> Self {
Self(Arc::new(InnerContentContext {
http_cli: config.create_http_client(),
}))
}
pub async fn my_topics(&self, opts: MyTopicsOptions) -> Result<Vec<OwnedTopic>> {
#[derive(Debug, Deserialize)]
struct Response {
items: Vec<OwnedTopic>,
}
Ok(self
.0
.http_cli
.request(Method::GET, "/v1/content/topics/mine")
.query_params(opts)
.response::<Json<Response>>()
.send()
.await?
.0
.items)
}
pub async fn create_topic(&self, opts: CreateTopicOptions) -> Result<String> {
#[derive(Debug, Deserialize)]
struct TopicId {
id: String,
}
#[derive(Debug, Deserialize)]
struct Response {
item: TopicId,
}
Ok(self
.0
.http_cli
.request(Method::POST, "/v1/content/topics")
.body(Json(opts))
.response::<Json<Response>>()
.send()
.await?
.0
.item
.id)
}
pub async fn topics(&self, symbol: impl Into<String>) -> Result<Vec<TopicItem>> {
#[derive(Debug, Deserialize)]
struct Response {
items: Vec<TopicItem>,
}
let symbol = symbol.into();
Ok(self
.0
.http_cli
.request(Method::GET, format!("/v1/content/{symbol}/topics"))
.response::<Json<Response>>()
.send()
.await?
.0
.items)
}
pub async fn topic_detail(&self, id: impl Into<String>) -> Result<OwnedTopic> {
#[derive(Debug, Deserialize)]
struct Response {
item: OwnedTopic,
}
let id = id.into();
Ok(self
.0
.http_cli
.request(Method::GET, format!("/v1/content/topics/{id}"))
.response::<Json<Response>>()
.send()
.await?
.0
.item)
}
pub async fn list_topic_replies(
&self,
topic_id: impl Into<String>,
opts: ListTopicRepliesOptions,
) -> Result<Vec<TopicReply>> {
#[derive(Debug, Deserialize)]
struct Response {
items: Vec<TopicReply>,
}
let topic_id = topic_id.into();
Ok(self
.0
.http_cli
.request(
Method::GET,
format!("/v1/content/topics/{topic_id}/comments"),
)
.query_params(opts)
.response::<Json<Response>>()
.send()
.await?
.0
.items)
}
pub async fn create_topic_reply(
&self,
topic_id: impl Into<String>,
opts: CreateReplyOptions,
) -> Result<TopicReply> {
#[derive(Debug, Deserialize)]
struct Response {
item: TopicReply,
}
let topic_id = topic_id.into();
Ok(self
.0
.http_cli
.request(
Method::POST,
format!("/v1/content/topics/{topic_id}/comments"),
)
.body(Json(opts))
.response::<Json<Response>>()
.send()
.await?
.0
.item)
}
pub async fn news(&self, symbol: impl Into<String>) -> Result<Vec<NewsItem>> {
#[derive(Debug, Deserialize)]
struct Response {
items: Vec<NewsItem>,
}
let symbol = symbol.into();
Ok(self
.0
.http_cli
.request(Method::GET, format!("/v1/content/{symbol}/news"))
.response::<Json<Response>>()
.send()
.await?
.0
.items)
}
}