use crate::models::authen::{RefreshUserAccessTokenV1Request, UserAccessTokenResponse};
use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
validate_required,
};
use serde::{Deserialize, Serialize};
pub struct RefreshUserAccessTokenV1Builder {
refresh_token: String,
app_id: String,
app_secret: String,
config: Config,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RefreshUserAccessTokenV1ResponseData {
pub data: UserAccessTokenResponse,
}
impl ApiResponseTrait for RefreshUserAccessTokenV1ResponseData {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl RefreshUserAccessTokenV1Builder {
pub fn new(config: Config) -> Self {
Self {
refresh_token: String::new(),
app_id: String::new(),
app_secret: String::new(),
config,
}
}
pub fn refresh_token(mut self, refresh_token: impl Into<String>) -> Self {
self.refresh_token = refresh_token.into();
self
}
pub fn app_id(mut self, app_id: impl Into<String>) -> Self {
self.app_id = app_id.into();
self
}
pub fn app_secret(mut self, app_secret: impl Into<String>) -> Self {
self.app_secret = app_secret.into();
self
}
pub async fn execute(self) -> SDKResult<RefreshUserAccessTokenV1ResponseData> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(
self,
option: RequestOption,
) -> SDKResult<RefreshUserAccessTokenV1ResponseData> {
validate_required!(self.refresh_token, "刷新令牌不能为空");
validate_required!(self.app_id, "应用ID不能为空");
validate_required!(self.app_secret, "应用密钥不能为空");
use crate::common::api_endpoints::AuthenApiV1;
let api_endpoint = AuthenApiV1::RefreshAccessToken;
let request_body = RefreshUserAccessTokenV1Request {
refresh_token: self.refresh_token.clone(),
app_id: self.app_id.clone(),
app_secret: self.app_secret.clone(),
};
let api_request: ApiRequest<RefreshUserAccessTokenV1ResponseData> =
ApiRequest::post(api_endpoint.path()).body(serde_json::to_value(&request_body)?);
let response = Transport::request(api_request, &self.config, Some(option)).await?;
response.data.ok_or_else(|| {
openlark_core::error::validation_error("刷新 user_access_token v1", "响应数据为空")
})
}
}
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
#[test]
fn test_serialization_roundtrip() {
let json = r#"{"test": "value"}"#;
assert!(serde_json::from_str::<serde_json::Value>(json).is_ok());
}
#[test]
fn test_deserialization_from_json() {
let json = r#"{"field": "data"}"#;
let value: serde_json::Value = serde_json::from_str(json).expect("JSON 反序列化失败");
assert_eq!(value["field"], "data");
}
}