use futures_util::stream::BoxStream;
use reqwest::Method;
use crate::client::LichessClient;
use crate::config::Host;
use crate::error::Result;
use crate::http;
use crate::model::LichessUser;
#[derive(Debug)]
pub struct RelationsApi<'a> {
client: &'a LichessClient,
}
impl<'a> RelationsApi<'a> {
pub(crate) fn new(client: &'a LichessClient) -> Self {
Self { client }
}
pub async fn following(&self) -> Result<BoxStream<'static, Result<LichessUser>>> {
let request = self
.client
.request(Method::GET, Host::Default, "/api/rel/following");
http::stream(request, self.client.max_line_bytes()).await
}
pub async fn follow(&self, username: &str) -> Result<()> {
self.post_relation("follow", username).await
}
pub async fn unfollow(&self, username: &str) -> Result<()> {
self.post_relation("unfollow", username).await
}
pub async fn block(&self, username: &str) -> Result<()> {
self.post_relation("block", username).await
}
pub async fn unblock(&self, username: &str) -> Result<()> {
self.post_relation("unblock", username).await
}
async fn post_relation(&self, action: &str, username: &str) -> Result<()> {
let path = format!(
"/api/rel/{}/{}",
http::segment(action),
http::segment(username)
);
http::ok(self.client.request(Method::POST, Host::Default, &path)).await
}
}
impl LichessClient {
#[must_use]
pub fn relations(&self) -> RelationsApi<'_> {
RelationsApi::new(self)
}
}