line_bot_sdk_module_attach/apis/
line_module_attach_api.rs1use super::{configuration, ContentType, Error};
12use crate::{apis::ResponseContent, models};
13use reqwest;
14use serde::{de::Error as _, Deserialize, Serialize};
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum AttachModuleError {
20 UnknownValue(serde_json::Value),
21}
22
23pub async fn attach_module(
25 configuration: &configuration::Configuration,
26 grant_type: &str,
27 code: &str,
28 redirect_uri: &str,
29 code_verifier: Option<&str>,
30 client_id: Option<&str>,
31 client_secret: Option<&str>,
32 region: Option<&str>,
33 basic_search_id: Option<&str>,
34 scope: Option<&str>,
35 brand_type: Option<&str>,
36) -> Result<models::AttachModuleResponse, Error<AttachModuleError>> {
37 let p_form_grant_type = grant_type;
39 let p_form_code = code;
40 let p_form_redirect_uri = redirect_uri;
41 let p_form_code_verifier = code_verifier;
42 let p_form_client_id = client_id;
43 let p_form_client_secret = client_secret;
44 let p_form_region = region;
45 let p_form_basic_search_id = basic_search_id;
46 let p_form_scope = scope;
47 let p_form_brand_type = brand_type;
48
49 let uri_str = format!("{}/module/auth/v1/token", configuration.base_path);
50 let mut req_builder = configuration
51 .client
52 .request(reqwest::Method::POST, &uri_str);
53
54 if let Some(ref user_agent) = configuration.user_agent {
55 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
56 }
57 if let Some(ref auth_conf) = configuration.basic_auth {
58 req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
59 };
60 let mut multipart_form_params = std::collections::HashMap::new();
61 multipart_form_params.insert("grant_type", p_form_grant_type.to_string());
62 multipart_form_params.insert("code", p_form_code.to_string());
63 multipart_form_params.insert("redirect_uri", p_form_redirect_uri.to_string());
64 if let Some(param_value) = p_form_code_verifier {
65 multipart_form_params.insert("code_verifier", param_value.to_string());
66 }
67 if let Some(param_value) = p_form_client_id {
68 multipart_form_params.insert("client_id", param_value.to_string());
69 }
70 if let Some(param_value) = p_form_client_secret {
71 multipart_form_params.insert("client_secret", param_value.to_string());
72 }
73 if let Some(param_value) = p_form_region {
74 multipart_form_params.insert("region", param_value.to_string());
75 }
76 if let Some(param_value) = p_form_basic_search_id {
77 multipart_form_params.insert("basic_search_id", param_value.to_string());
78 }
79 if let Some(param_value) = p_form_scope {
80 multipart_form_params.insert("scope", param_value.to_string());
81 }
82 if let Some(param_value) = p_form_brand_type {
83 multipart_form_params.insert("brand_type", param_value.to_string());
84 }
85 req_builder = req_builder.form(&multipart_form_params);
86
87 let req = req_builder.build()?;
88 let resp = configuration.client.execute(req).await?;
89
90 let status = resp.status();
91 let content_type = resp
92 .headers()
93 .get("content-type")
94 .and_then(|v| v.to_str().ok())
95 .unwrap_or("application/octet-stream");
96 let content_type = super::ContentType::from(content_type);
97
98 if !status.is_client_error() && !status.is_server_error() {
99 let content = resp.text().await?;
100 match content_type {
101 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
102 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AttachModuleResponse`"))),
103 ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::AttachModuleResponse`")))),
104 }
105 } else {
106 let content = resp.text().await?;
107 let entity: Option<AttachModuleError> = serde_json::from_str(&content).ok();
108 Err(Error::ResponseError(ResponseContent {
109 status,
110 content,
111 entity,
112 }))
113 }
114}