use reqwest::Method;
use serde::Deserialize;
use serde_json::json;
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
constants::AccessTokenType,
endpoints::cardkit::*,
http::Transport,
req_option::RequestOption,
SDKResult,
},
impl_executable_builder_owned,
service::cardkit::v1::models::{Card, UserIdType},
};
use super::CardService;
#[derive(Default, Clone)]
pub struct CreateCardRequest {
pub api_req: ApiRequest,
pub title: Option<String>,
pub description: Option<String>,
pub card_json: Option<serde_json::Value>,
pub user_id_type: Option<UserIdType>,
}
impl CreateCardRequest {
pub fn builder() -> CreateCardRequestBuilder {
CreateCardRequestBuilder::default()
}
}
#[derive(Default)]
pub struct CreateCardRequestBuilder {
request: CreateCardRequest,
}
impl CreateCardRequestBuilder {
pub fn title(mut self, title: impl ToString) -> Self {
self.request.title = Some(title.to_string());
self
}
pub fn description(mut self, description: impl ToString) -> Self {
self.request.description = Some(description.to_string());
self
}
pub fn card_json(mut self, card_json: serde_json::Value) -> Self {
self.request.card_json = Some(card_json);
self
}
pub fn user_id_type(mut self, user_id_type: UserIdType) -> Self {
self.request.user_id_type = Some(user_id_type);
self
}
pub fn build(mut self) -> CreateCardRequest {
if let Some(user_id_type) = &self.request.user_id_type {
self.request
.api_req
.query_params
.insert("user_id_type", user_id_type.to_string());
}
let mut body = json!({});
if let Some(ref title) = self.request.title {
body["title"] = json!(title);
}
if let Some(ref description) = self.request.description {
body["description"] = json!(description);
}
if let Some(ref card_json) = self.request.card_json {
body["card_json"] = card_json.clone();
}
self.request.api_req.body = serde_json::to_vec(&body).unwrap_or_default();
self.request
}
}
#[derive(Debug, Deserialize)]
pub struct CreateCardResponseData {
pub card: Card,
}
#[derive(Debug, Deserialize)]
pub struct CreateCardResponse {
pub data: CreateCardResponseData,
}
impl ApiResponseTrait for CreateCardResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl CardService {
pub async fn create(
&self,
request: CreateCardRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<CreateCardResponse>> {
let mut api_req = request.api_req;
api_req.http_method = Method::POST;
api_req.api_path = CARDKIT_V1_CARDS.to_string();
api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
let api_resp = Transport::request(api_req, &self.config, option).await?;
Ok(api_resp)
}
}
impl_executable_builder_owned!(
CreateCardRequestBuilder,
CardService,
CreateCardRequest,
BaseResponse<CreateCardResponse>,
create
);