use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
config::Config,
constants::AccessTokenType,
endpoints::cloud_docs::*,
http::Transport,
req_option::RequestOption,
SDKResult,
},
service::bitable::v1::form::Form,
};
#[derive(Debug, Serialize, Default)]
pub struct PatchFormMetaRequest {
#[serde(skip)]
api_request: ApiRequest,
#[serde(skip)]
app_token: String,
#[serde(skip)]
form_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
allow_resubmit: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
show_submit_button: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
submit_button_text: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
shared: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
need_login: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
status: Option<String>,
}
impl PatchFormMetaRequest {
pub fn builder() -> PatchFormMetaRequestBuilder {
PatchFormMetaRequestBuilder::default()
}
pub fn new(app_token: impl ToString, form_id: impl ToString) -> Self {
Self {
app_token: app_token.to_string(),
form_id: form_id.to_string(),
..Default::default()
}
}
}
#[derive(Default)]
pub struct PatchFormMetaRequestBuilder {
request: PatchFormMetaRequest,
}
impl PatchFormMetaRequestBuilder {
pub fn app_token(mut self, app_token: impl ToString) -> Self {
self.request.app_token = app_token.to_string();
self
}
pub fn form_id(mut self, form_id: impl ToString) -> Self {
self.request.form_id = form_id.to_string();
self
}
pub fn name(mut self, name: impl ToString) -> Self {
self.request.name = Some(name.to_string());
self
}
pub fn description(mut self, description: impl ToString) -> Self {
self.request.description = Some(description.to_string());
self
}
pub fn allow_resubmit(mut self, allow_resubmit: bool) -> Self {
self.request.allow_resubmit = Some(allow_resubmit);
self
}
pub fn show_submit_button(mut self, show_submit_button: bool) -> Self {
self.request.show_submit_button = Some(show_submit_button);
self
}
pub fn submit_button_text(mut self, submit_button_text: impl ToString) -> Self {
self.request.submit_button_text = Some(submit_button_text.to_string());
self
}
pub fn shared(mut self, shared: bool) -> Self {
self.request.shared = Some(shared);
self
}
pub fn need_login(mut self, need_login: bool) -> Self {
self.request.need_login = Some(need_login);
self
}
pub fn status(mut self, status: impl ToString) -> Self {
self.request.status = Some(status.to_string());
self
}
pub fn build(mut self) -> PatchFormMetaRequest {
self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
self.request
}
}
#[derive(Debug, Deserialize)]
pub struct PatchFormMetaResponse {
pub form: Form,
}
impl ApiResponseTrait for PatchFormMetaResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn patch_form_meta(
request: PatchFormMetaRequest,
config: &Config,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<PatchFormMetaResponse>> {
let mut api_req = request.api_request;
api_req.http_method = Method::PATCH;
api_req.api_path = BITABLE_V1_FORM_PATCH
.replace("{app_token}", &request.app_token)
.replace("{form_id}", &request.form_id);
api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
let api_resp = Transport::request(api_req, config, option).await?;
Ok(api_resp)
}
#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod tests {
use super::*;
#[test]
fn test_patch_form_meta_request_builder() {
let request = PatchFormMetaRequest::builder()
.app_token("bascnmBA*****yGehy8")
.form_id("vewxxxxxx")
.name("更新后的表单名称")
.description("更新后的表单描述")
.allow_resubmit(true)
.shared(true)
.build();
assert_eq!(request.app_token, "bascnmBA*****yGehy8");
assert_eq!(request.form_id, "vewxxxxxx");
assert_eq!(request.name, Some("更新后的表单名称".to_string()));
assert_eq!(request.description, Some("更新后的表单描述".to_string()));
assert_eq!(request.allow_resubmit, Some(true));
assert_eq!(request.shared, Some(true));
}
}