use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
config::Config,
constants::AccessTokenType,
http::Transport,
req_option::RequestOption,
standard_response::StandardResponse,
SDKResult,
};
use crate::impl_full_service;
pub struct ChatsService {
pub config: Config,
}
impl ChatsService {
pub async fn list(
&self,
list_chat_request: ListChatRequest,
option: Option<RequestOption>,
) -> SDKResult<ListChatRespData> {
let mut api_req = list_chat_request.api_req;
api_req.http_method = Method::GET;
api_req.api_path = crate::core::endpoints::im::IM_CHAT_CREATE.to_string();
api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
let api_resp: BaseResponse<ListChatRespData> =
Transport::request(api_req, &self.config, option).await?;
api_resp.into_result()
}
pub fn list_iter(
&self,
list_chat_request: ListChatRequest,
option: Option<RequestOption>,
) -> ListChatIterator<'_> {
ListChatIterator {
service: self,
request: list_chat_request,
option,
has_more: true,
}
}
}
impl_full_service!(ChatsService, "im.chats", "v1");
pub struct ListChatIterator<'a> {
service: &'a ChatsService,
request: ListChatRequest,
option: Option<RequestOption>,
has_more: bool,
}
impl ListChatIterator<'_> {
pub async fn next(&mut self) -> Option<Vec<ListChat>> {
if !self.has_more {
return None;
}
match self
.service
.list(self.request.clone(), self.option.clone())
.await
{
Ok(data) => {
self.has_more = data.has_more;
if data.has_more {
self.request
.api_req
.query_params
.insert("page_token", data.page_token.to_string());
Some(data.items)
} else if data.items.is_empty() {
None
} else {
Some(data.items)
}
}
Err(_) => None,
}
}
}
#[derive(Default, Clone)]
pub struct ListChatRequest {
api_req: ApiRequest,
}
impl ListChatRequest {
pub fn builder() -> ListChatRequestBuilder {
ListChatRequestBuilder::default()
}
}
#[derive(Default)]
pub struct ListChatRequestBuilder {
request: ListChatRequest,
}
impl ListChatRequestBuilder {
pub fn user_id_type(mut self, user_id_type: impl ToString) -> Self {
self.request
.api_req
.query_params
.insert("user_id_type", user_id_type.to_string());
self
}
pub fn sort_type(mut self, sort_type: impl ToString) -> Self {
self.request
.api_req
.query_params
.insert("sort_type", sort_type.to_string());
self
}
pub fn page_token(mut self, page_token: impl ToString) -> Self {
self.request
.api_req
.query_params
.insert("page_token", page_token.to_string());
self
}
pub fn page_size(mut self, page_size: i32) -> Self {
self.request
.api_req
.query_params
.insert("page_size", page_size.to_string());
self
}
pub fn build(self) -> ListChatRequest {
self.request
}
}
crate::impl_executable_builder_owned!(
ListChatRequestBuilder,
ChatsService,
ListChatRequest,
ListChatRespData,
list
);
#[derive(Debug, Serialize, Deserialize)]
pub struct ListChatRespData {
pub items: Vec<ListChat>,
pub page_token: String,
pub has_more: bool,
}
impl ApiResponseTrait for ListChatRespData {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ListChat {
pub chat_id: String,
pub avatar: String,
pub name: String,
pub description: String,
pub owner_id: String,
pub owner_id_type: String,
pub external: bool,
pub tenant_key: String,
pub chat_status: String,
}