use std::sync::Arc;
use async_trait::async_trait;
use wx_rust_common::bean::result::WxMinishopImageUploadResult;
use wx_rust_common::error::WxErrorException;
use wx_rust_common::util::http::{SimpleGetRequestExecutor, SimplePostRequestExecutor};
use crate::api::WxOpenComponentService;
use crate::bean::message::WxOpenXmlMessage;
use crate::config::WxOpenConfigStorage;
use crate::constant::wx_open_constants::ACCESS_TOKEN_KEY_COMPONENT;
#[async_trait]
pub trait WxOpenService: Send + Sync {
fn wx_open_component_service(&self) -> Option<Arc<dyn WxOpenComponentService>> {
None
}
fn wx_open_config_storage(&self) -> Arc<dyn WxOpenConfigStorage>;
fn set_wx_open_config_storage(&self, wx_open_config_storage: Arc<dyn WxOpenConfigStorage>);
fn http_client(&self) -> &reqwest::Client;
async fn get(&self, url: &str, query_param: &str) -> Result<String, WxErrorException> {
let executor = SimpleGetRequestExecutor::new(self.http_client().clone());
crate::api::r#impl::base_wx_open_service_impl::execute_with_retry(
self,
&executor,
url,
query_param.to_string(),
ACCESS_TOKEN_KEY_COMPONENT,
)
.await
}
async fn post(&self, url: &str, post_data: &str) -> Result<String, WxErrorException> {
let executor = SimplePostRequestExecutor::new(self.http_client().clone());
crate::api::r#impl::base_wx_open_service_impl::execute_with_retry(
self,
&executor,
url,
post_data.to_string(),
ACCESS_TOKEN_KEY_COMPONENT,
)
.await
}
async fn upload_minishop_media_file(
&self,
_url: &str,
_file_path: &str,
) -> Result<WxMinishopImageUploadResult, WxErrorException> {
Err(WxErrorException::from_code(
-99,
"upload_minishop_media_file 未实现(Wave 1:MinishopUploadRequestExecutor)",
))
}
async fn get_component_access_token(
&self,
force_refresh: bool,
) -> Result<String, WxErrorException> {
match self.wx_open_component_service() {
Some(svc) => svc.get_component_access_token(force_refresh).await,
None => Err(WxErrorException::from_code(
-99,
"组件子服务未装配(getWxOpenComponentService 返回 null)",
)),
}
}
async fn get_pre_auth_code(&self) -> Result<String, WxErrorException> {
match self.wx_open_component_service() {
Some(svc) => svc.get_pre_auth_code().await,
None => Err(WxErrorException::from_code(
-99,
"组件子服务未装配(getWxOpenComponentService 返回 null)",
)),
}
}
async fn get_pre_auth_url(&self, redirect_uri: &str) -> Result<String, WxErrorException> {
match self.wx_open_component_service() {
Some(svc) => svc.get_pre_auth_url(redirect_uri).await,
None => Err(WxErrorException::from_code(
-99,
"组件子服务未装配(getWxOpenComponentService 返回 null)",
)),
}
}
async fn get_pre_auth_url_with(
&self,
redirect_uri: &str,
auth_type: Option<&str>,
biz_appid: Option<&str>,
) -> Result<String, WxErrorException> {
match self.wx_open_component_service() {
Some(svc) => {
svc.get_pre_auth_url_with(redirect_uri, auth_type, biz_appid)
.await
}
None => Err(WxErrorException::from_code(
-99,
"组件子服务未装配(getWxOpenComponentService 返回 null)",
)),
}
}
async fn get_mobile_pre_auth_url(
&self,
redirect_uri: &str,
) -> Result<String, WxErrorException> {
match self.wx_open_component_service() {
Some(svc) => svc.get_mobile_pre_auth_url(redirect_uri).await,
None => Err(WxErrorException::from_code(
-99,
"组件子服务未装配(getWxOpenComponentService 返回 null)",
)),
}
}
async fn get_mobile_pre_auth_url_with(
&self,
redirect_uri: &str,
auth_type: Option<&str>,
biz_appid: Option<&str>,
) -> Result<String, WxErrorException> {
match self.wx_open_component_service() {
Some(svc) => {
svc.get_mobile_pre_auth_url_with(redirect_uri, auth_type, biz_appid)
.await
}
None => Err(WxErrorException::from_code(
-99,
"组件子服务未装配(getWxOpenComponentService 返回 null)",
)),
}
}
async fn get_authorizer_access_token(
&self,
app_id: &str,
force_refresh: bool,
) -> Result<String, WxErrorException> {
match self.wx_open_component_service() {
Some(svc) => svc.get_authorizer_access_token(app_id, force_refresh).await,
None => Err(WxErrorException::from_code(
-99,
"组件子服务未装配(getWxOpenComponentService 返回 null)",
)),
}
}
async fn route(&self, message: &WxOpenXmlMessage) -> Result<String, WxErrorException> {
match self.wx_open_component_service() {
Some(svc) => svc.route(message).await,
None => Err(WxErrorException::from_code(
-99,
"组件子服务未装配(getWxOpenComponentService 返回 null)",
)),
}
}
}