wx_rust_common/util/http/
simple_get_request_executor.rs1use async_trait::async_trait;
7
8use crate::enums::WxType;
9use crate::error::{WxError, WxErrorError, WxErrorException};
10use crate::util::http::{RequestExecutor, ResponseHandler};
11
12#[derive(Debug, Clone)]
17pub struct SimpleGetRequestExecutor {
18 client: reqwest::Client,
20}
21
22impl SimpleGetRequestExecutor {
23 pub fn new(client: reqwest::Client) -> Self {
28 Self { client }
29 }
30
31 pub fn handle_response(
40 wx_type: WxType,
41 response_content: &str,
42 ) -> Result<String, WxErrorException> {
43 let error = WxError::from_json_with_type(response_content, Some(wx_type));
44 if error.error_code != 0 {
45 return Err(WxErrorException::Wx(WxErrorError::new(error)));
50 }
51 Ok(response_content.to_string())
52 }
53}
54
55#[async_trait]
56impl RequestExecutor<String, String> for SimpleGetRequestExecutor {
57 async fn execute(
58 &self,
59 uri: &str,
60 data: String,
61 wx_type: WxType,
62 ) -> Result<String, WxErrorException> {
63 let url = if data.is_empty() {
65 uri.to_string()
66 } else if uri.contains('?') {
67 format!("{uri}&{data}")
68 } else {
69 format!("{uri}?{data}")
70 };
71 let resp = self.client.get(&url).send().await?;
72 let body = resp.text().await?;
73 Self::handle_response(wx_type, &body)
74 }
75}
76
77#[async_trait]
78impl ResponseHandler<String> for SimpleGetRequestExecutor {
79 async fn handle(&self, response: String) {
80 let _ = response;
81 }
82}