alibaba_cloud_sdk_rust/sdk/requests/
mod.rs1#![allow(unused)]
2#![allow(non_upper_case_globals)]
3#![allow(non_snake_case)]
4#![allow(non_camel_case_types)]
5mod types;
6use gostd::io::StringWriter;
7use gostd::strings::Builder;
8use regex::Replacer;
9pub use types::*;
10pub const RPC: &str = "RPC";
11pub const ROA: &str = "ROA";
12
13pub const HTTP: &str = "HTTP";
14pub const HTTPS: &str = "HTTPS";
15
16pub const DefaultHttpPort: &str = "80";
17
18pub const GET: &str = "GET";
19pub const PUT: &str = "PUT";
20pub const POST: &str = "POST";
21pub const DELETE: &str = "DELETE";
22pub const PATCH: &str = "PATCH";
23pub const HEAD: &str = "HEAD";
24pub const OPTIONS: &str = "OPTIONS";
25
26pub const Json: &str = "application/json";
27pub const Xml: &str = "application/xml";
28pub const Raw: &str = "application/octet-stream";
29pub const Form: &str = "application/x-www-form-urlencoded";
30
31pub const Header: &str = "Header";
32pub const Query: &str = "Query";
33pub const Body: &str = "Body";
34pub const Path: &str = "Path";
35
36pub const HeaderSeparator: &str = "\n";
37
38use gostd::io;
39use gostd::net::http::Method;
40use gostd::strings;
41use log::{debug, info, warn};
42use std::borrow::{Borrow, BorrowMut};
43use std::collections::HashMap;
44
45use super::auth::singers::GetUrlFormedMap;
46pub type AcsRequest = RpcRequest;
47
48pub trait BaseRequestExt {
49 fn base(&self) -> &BaseRequest;
50
51 fn base_as_mut(&mut self) -> &mut BaseRequest;
52
53 fn GetQueryParams(&self) -> &HashMap<String, String> {
54 self.base().QueryParams.borrow()
55 }
56
57 fn GetFormParams(&self) -> &HashMap<String, String> {
58 self.base().FormParams.borrow()
59 }
60
61 fn GetHTTPSInsecure(&self) -> bool {
62 self.base().isInsecure
63 }
64
65 fn SetHTTPSInsecure(&mut self, isInsecure: bool) {
66 self.base_as_mut().isInsecure = isInsecure
67 }
68
69 fn GetContent(&self) -> &[u8] {
70 self.base().Content.borrow()
71 }
72
73 fn SetContent(&mut self, content: &[u8]) {
74 self.base_as_mut().Content = content.to_owned()
75 }
76
77 fn SetVersion(&mut self, version: &str) {
78 self.base_as_mut().version = version.to_string();
79 }
80
81 fn GetVersion(&self) -> &str {
82 self.base().version.borrow()
83 }
84
85 fn GetActionName(&self) -> &str {
86 self.base().actionName.borrow()
87 }
88
89 fn SetActionName(&mut self, actionName: &str) {
90 self.base_as_mut().actionName = actionName.to_string();
91 }
92
93 fn GetUserAgent(&self) -> &HashMap<String, String> {
94 self.base().userAgent.borrow()
95 }
96
97 fn AppendUserAgent(&mut self, key: &str, value: &str) {
98 let mut newKey = true;
99 if self.base_as_mut().userAgent.is_empty() {
100 self.base_as_mut().userAgent = HashMap::new();
101 }
102 if strings::ToLower(key).as_str() != "core" && strings::ToLower(key) != "rust" {
103 for (tag, mut v) in self.base_as_mut().userAgent.iter_mut() {
104 if tag == key {
105 *v = value.to_string();
106 newKey = false;
107 }
108 }
109 if newKey {
110 self.base_as_mut()
111 .userAgent
112 .insert(key.to_string(), value.to_string());
113 }
114 }
115 }
116
117 fn addHeaderParam(&mut self, key: &str, value: &str) {
118 self.base_as_mut()
119 .Headers
120 .insert(key.to_string(), value.to_string());
121 }
122
123 fn Hearder_as_mut(&mut self) -> &mut HashMap<String, String> {
124 self.base_as_mut().Headers.borrow_mut()
125 }
126
127 fn addQueryParam(&mut self, key: &str, value: &str) {
128 self.base_as_mut()
129 .QueryParams
130 .insert(key.to_string(), value.to_string());
131 }
132
133 fn QueryParams_as_mut(&mut self) -> &mut HashMap<String, String> {
134 self.base_as_mut().QueryParams.borrow_mut()
135 }
136
137 fn addFormParam(&mut self, key: &str, value: &str) {
138 self.base_as_mut()
139 .FormParams
140 .insert(key.to_string(), value.to_string());
141 }
142
143 fn FormParams_as_mut(&mut self) -> &mut HashMap<String, String> {
144 self.base_as_mut().FormParams.borrow_mut()
145 }
146
147 fn GetAcceptFormat(&self) -> &str {
148 self.base().AcceptFormat.borrow()
149 }
150
151 fn GetLocationServiceCode(&self) -> &str {
152 self.base().locationServiceCode.borrow()
153 }
154
155 fn SetLocationServiceCode(&mut self, locationServiceCode: &str) {
156 self.base_as_mut().locationServiceCode = locationServiceCode.to_string();
157 }
158
159 fn GetLocationEndpointType(&self) -> &str {
160 self.base().locationEndpointType.borrow()
161 }
162
163 fn SetLocationEndpointType(&mut self, locationEndpointType: &str) {
164 self.base_as_mut().locationEndpointType = locationEndpointType.to_string();
165 }
166
167 fn GetProduct(&self) -> &str {
168 self.base().product.borrow()
169 }
170
171 fn SetProduct(&mut self, product: &str) {
172 self.base_as_mut().product = product.to_string();
173 }
174
175 fn GetScheme(&self) -> &str {
176 self.base().Scheme.borrow()
177 }
178
179 fn SetScheme(&mut self, scheme: &str) {
180 self.base_as_mut().Scheme = scheme.to_string()
181 }
182
183 fn GetMethod(&self) -> &str {
184 self.base().Method.borrow()
185 }
186
187 fn SetMethod(&mut self, method: &str) {
188 self.base_as_mut().Method = method.to_string()
189 }
190
191 fn GetDomain(&self) -> &str {
192 self.base().Domain.borrow()
193 }
194
195 fn SetDomain(&mut self, host: &str) {
196 self.base_as_mut().Domain = host.to_string()
197 }
198
199 fn GetPort(&self) -> &str {
200 self.base().Port.borrow()
201 }
202
203 fn GetRegionId(&self) -> &str {
204 self.base().RegionId.borrow()
205 }
206
207 fn GetHeaders(&self) -> &HashMap<String, String> {
208 self.base().Headers.borrow()
209 }
210
211 fn SetContentType(&mut self, contentType: &str) {
212 self.addHeaderParam("Content-Type", contentType)
213 }
214
215 fn GetContentType(&self) -> Option<&str> {
216 self.base().Headers.get("Content-Type").map(|s| s.as_str())
217 }
218
219 fn GetQueries(&self) -> &str {
220 self.base().queries.borrow()
221 }
222
223 fn SetQueries(&mut self, queries: &str) {
224 self.base_as_mut().queries = queries.to_string()
225 }
226
227 fn SetStringToSign(&mut self, stringToSign: &str) {
228 self.base_as_mut().stringToSign = stringToSign.to_string()
229 }
230
231 fn GetStringToSign(&self) -> &str {
232 self.base().stringToSign.borrow()
233 }
234}
235pub struct CommonRequest {
236 base: BaseRequest,
237 pub Version: String,
238 pub ApiName: String,
239 pub Product: String,
240 pub ServiceCode: String,
241 pub EndpointType: String,
242
243 pub PathPattern: String,
245 pub PathParams: HashMap<String, String>,
246
247 pub Ontology: AcsRequest,
248}
249
250impl BaseRequestExt for CommonRequest {
251 fn base(&self) -> &BaseRequest {
252 self.base.borrow()
253 }
254
255 fn base_as_mut(&mut self) -> &mut BaseRequest {
256 self.base.borrow_mut()
257 }
258}
259
260#[derive(Default, Debug)]
261pub struct BaseRequest {
262 pub Scheme: String,
263 pub Method: String,
264 pub Domain: String,
265 pub Port: String,
266 pub RegionId: String,
267 pub isInsecure: bool,
270
271 pub userAgent: HashMap<String, String>,
272 pub product: String,
273 pub version: String,
274
275 pub actionName: String,
276
277 pub AcceptFormat: String,
278
279 pub QueryParams: HashMap<String, String>,
280 pub Headers: HashMap<String, String>,
281 pub FormParams: HashMap<String, String>,
282 pub Content: Vec<u8>,
283
284 pub locationServiceCode: String,
285 pub locationEndpointType: String,
286
287 pub queries: String,
288
289 pub stringToSign: String,
290}
291
292impl BaseRequest {
293 pub fn defaultBaseRequest() -> Self {
294 Self {
295 Scheme: "".to_owned(),
296 AcceptFormat: "JSON".to_owned(),
297 Method: GET.to_owned(),
298 Headers: HashMap::from([
299 ("x-sdk-client".to_owned(), "rust-lang/1.0.0".to_owned()),
300 ("x-sdk-invoke-type".to_owned(), "normal".to_owned()),
301 ("Accept-Encoding".to_owned(), "identity".to_owned()),
302 ]),
303 ..Default::default()
304 }
305 }
306}
307
308impl BaseRequestExt for BaseRequest {
309 fn base(&self) -> &BaseRequest {
310 self
311 }
312
313 fn base_as_mut(&mut self) -> &mut BaseRequest {
314 self.borrow_mut()
315 }
316}
317
318#[derive(Default, Debug)]
319pub struct RpcRequest {
320 base: BaseRequest,
321}
322
323impl BaseRequestExt for RpcRequest {
324 fn base(&self) -> &BaseRequest {
325 self.base.borrow()
326 }
327
328 fn base_as_mut(&mut self) -> &mut BaseRequest {
329 self.base.borrow_mut()
330 }
331}
332impl RpcRequest {
333 fn init(&mut self) {
334 self.base = BaseRequest::defaultBaseRequest();
335 self.SetMethod(POST);
336 debug!("init baseRequest: {:?}", self.base());
337 }
338 pub fn InitWithApiInfo(
339 &mut self,
340 product: &str,
341 version: &str,
342 action: &str,
343 serviceCode: &str,
344 endpointType: &str,
345 ) {
346 self.init();
347 self.SetProduct(product);
348 self.SetVersion(version);
349 self.SetActionName(action);
350 self.SetLocationServiceCode(serviceCode);
351 self.SetLocationEndpointType(endpointType);
352 self.addHeaderParam("x-acs-version", version);
353 self.addHeaderParam("x-acs-action", action);
354 }
355
356 pub fn GetStyle(&self) -> String {
357 RPC.to_string()
358 }
359
360 pub fn GetMethod(&self) -> Method {
361 match self.base().Method.as_str() {
362 GET => Method::Get,
363 PUT => Method::Put,
364 POST => Method::Post,
365 DELETE => Method::Delete,
366 PATCH => Method::Patch,
367 HEAD => Method::Head,
368 OPTIONS => Method::Options,
369 _ => Method::Get,
370 }
371 }
372
373 pub fn BuildUrl(&mut self) -> String {
374 let mut url = format!(
375 "{}://{}",
376 strings::ToLower(&self.GetScheme()),
377 self.GetDomain()
378 );
379 if !self.GetPort().is_empty() {
380 url = format!("{}:{}", url, self.GetPort());
381 }
382 url.push_str(self.BuildQueries().as_str());
383 debug!("url: {:?}", url);
384 url
385 }
386
387 pub fn BuildQueries(&mut self) -> String {
388 self.SetQueries(
389 ("/?".to_owned() + GetUrlFormedMap(&self.GetQueryParams()).as_str()).as_str(),
390 );
391 self.GetQueries().to_owned()
392 }
393
394 pub fn GetBodyReader(&self) -> Builder {
395 let mut buf = strings::Builder::new();
396 if !self.GetFormParams().is_empty() {
397 let formString = GetUrlFormedMap(&self.GetFormParams());
398
399 buf.WriteString(&formString);
400 buf
401 } else {
402 buf.WriteString("");
403 buf
404 }
405 }
406}