1#![allow(unused_imports)]
15use async_trait::async_trait;
16use derive_builder::Builder;
17use reqwest;
18use rust_decimal::prelude::*;
19use serde::{Deserialize, Serialize};
20use serde_json::{Value, json};
21use std::collections::BTreeMap;
22
23use crate::common::{
24 config::ConfigurationRestApi,
25 models::{ParamBuildError, RestApiResponse},
26 utils::send_request,
27};
28use crate::sub_account::rest_api::models;
29
30const HAS_TIME_UNIT: bool = false;
31
32#[async_trait]
33pub trait ApiManagementApi: Send + Sync {
34 async fn add_ip_restriction_for_sub_account_api_key(
35 &self,
36 params: AddIpRestrictionForSubAccountApiKeyParams,
37 ) -> anyhow::Result<RestApiResponse<models::AddIpRestrictionForSubAccountApiKeyResponse>>;
38 async fn delete_ip_list_for_a_sub_account_api_key(
39 &self,
40 params: DeleteIpListForASubAccountApiKeyParams,
41 ) -> anyhow::Result<RestApiResponse<models::DeleteIpListForASubAccountApiKeyResponse>>;
42 async fn get_ip_restriction_for_a_sub_account_api_key(
43 &self,
44 params: GetIpRestrictionForASubAccountApiKeyParams,
45 ) -> anyhow::Result<RestApiResponse<models::GetIpRestrictionForASubAccountApiKeyResponse>>;
46}
47
48#[derive(Debug, Clone)]
49pub struct ApiManagementApiClient {
50 configuration: ConfigurationRestApi,
51}
52
53impl ApiManagementApiClient {
54 pub fn new(configuration: ConfigurationRestApi) -> Self {
55 Self { configuration }
56 }
57}
58
59#[derive(Clone, Debug, Builder)]
64#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
65pub struct AddIpRestrictionForSubAccountApiKeyParams {
66 #[builder(setter(into))]
70 pub email: String,
71 #[builder(setter(into))]
76 pub sub_account_api_key: String,
77 #[builder(setter(into))]
81 pub status: String,
82 #[builder(setter(into), default)]
86 pub ip_address: Option<String>,
87 #[builder(setter(into), default)]
92 pub recv_window: Option<i64>,
93}
94
95impl AddIpRestrictionForSubAccountApiKeyParams {
96 #[must_use]
105 pub fn builder(
106 email: String,
107 sub_account_api_key: String,
108 status: String,
109 ) -> AddIpRestrictionForSubAccountApiKeyParamsBuilder {
110 AddIpRestrictionForSubAccountApiKeyParamsBuilder::default()
111 .email(email)
112 .sub_account_api_key(sub_account_api_key)
113 .status(status)
114 }
115}
116#[derive(Clone, Debug, Builder)]
121#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
122pub struct DeleteIpListForASubAccountApiKeyParams {
123 #[builder(setter(into))]
127 pub email: String,
128 #[builder(setter(into))]
133 pub sub_account_api_key: String,
134 #[builder(setter(into))]
138 pub ip_address: String,
139 #[builder(setter(into), default)]
144 pub recv_window: Option<i64>,
145}
146
147impl DeleteIpListForASubAccountApiKeyParams {
148 #[must_use]
157 pub fn builder(
158 email: String,
159 sub_account_api_key: String,
160 ip_address: String,
161 ) -> DeleteIpListForASubAccountApiKeyParamsBuilder {
162 DeleteIpListForASubAccountApiKeyParamsBuilder::default()
163 .email(email)
164 .sub_account_api_key(sub_account_api_key)
165 .ip_address(ip_address)
166 }
167}
168#[derive(Clone, Debug, Builder)]
173#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
174pub struct GetIpRestrictionForASubAccountApiKeyParams {
175 #[builder(setter(into))]
179 pub email: String,
180 #[builder(setter(into))]
185 pub sub_account_api_key: String,
186 #[builder(setter(into), default)]
191 pub recv_window: Option<i64>,
192}
193
194impl GetIpRestrictionForASubAccountApiKeyParams {
195 #[must_use]
203 pub fn builder(
204 email: String,
205 sub_account_api_key: String,
206 ) -> GetIpRestrictionForASubAccountApiKeyParamsBuilder {
207 GetIpRestrictionForASubAccountApiKeyParamsBuilder::default()
208 .email(email)
209 .sub_account_api_key(sub_account_api_key)
210 }
211}
212
213#[async_trait]
214impl ApiManagementApi for ApiManagementApiClient {
215 async fn add_ip_restriction_for_sub_account_api_key(
216 &self,
217 params: AddIpRestrictionForSubAccountApiKeyParams,
218 ) -> anyhow::Result<RestApiResponse<models::AddIpRestrictionForSubAccountApiKeyResponse>> {
219 let AddIpRestrictionForSubAccountApiKeyParams {
220 email,
221 sub_account_api_key,
222 status,
223 ip_address,
224 recv_window,
225 } = params;
226
227 let mut query_params = BTreeMap::new();
228 let body_params = BTreeMap::new();
229
230 query_params.insert("email".to_string(), json!(email));
231
232 query_params.insert("subAccountApiKey".to_string(), json!(sub_account_api_key));
233
234 query_params.insert("status".to_string(), json!(status));
235
236 if let Some(rw) = ip_address {
237 query_params.insert("ipAddress".to_string(), json!(rw));
238 }
239
240 if let Some(rw) = recv_window {
241 query_params.insert("recvWindow".to_string(), json!(rw));
242 }
243
244 send_request::<models::AddIpRestrictionForSubAccountApiKeyResponse>(
245 &self.configuration,
246 "/sapi/v2/sub-account/subAccountApi/ipRestriction",
247 reqwest::Method::POST,
248 query_params,
249 body_params,
250 if HAS_TIME_UNIT {
251 self.configuration.time_unit
252 } else {
253 None
254 },
255 true,
256 )
257 .await
258 }
259
260 async fn delete_ip_list_for_a_sub_account_api_key(
261 &self,
262 params: DeleteIpListForASubAccountApiKeyParams,
263 ) -> anyhow::Result<RestApiResponse<models::DeleteIpListForASubAccountApiKeyResponse>> {
264 let DeleteIpListForASubAccountApiKeyParams {
265 email,
266 sub_account_api_key,
267 ip_address,
268 recv_window,
269 } = params;
270
271 let mut query_params = BTreeMap::new();
272 let body_params = BTreeMap::new();
273
274 query_params.insert("email".to_string(), json!(email));
275
276 query_params.insert("subAccountApiKey".to_string(), json!(sub_account_api_key));
277
278 query_params.insert("ipAddress".to_string(), json!(ip_address));
279
280 if let Some(rw) = recv_window {
281 query_params.insert("recvWindow".to_string(), json!(rw));
282 }
283
284 send_request::<models::DeleteIpListForASubAccountApiKeyResponse>(
285 &self.configuration,
286 "/sapi/v1/sub-account/subAccountApi/ipRestriction/ipList",
287 reqwest::Method::DELETE,
288 query_params,
289 body_params,
290 if HAS_TIME_UNIT {
291 self.configuration.time_unit
292 } else {
293 None
294 },
295 true,
296 )
297 .await
298 }
299
300 async fn get_ip_restriction_for_a_sub_account_api_key(
301 &self,
302 params: GetIpRestrictionForASubAccountApiKeyParams,
303 ) -> anyhow::Result<RestApiResponse<models::GetIpRestrictionForASubAccountApiKeyResponse>> {
304 let GetIpRestrictionForASubAccountApiKeyParams {
305 email,
306 sub_account_api_key,
307 recv_window,
308 } = params;
309
310 let mut query_params = BTreeMap::new();
311 let body_params = BTreeMap::new();
312
313 query_params.insert("email".to_string(), json!(email));
314
315 query_params.insert("subAccountApiKey".to_string(), json!(sub_account_api_key));
316
317 if let Some(rw) = recv_window {
318 query_params.insert("recvWindow".to_string(), json!(rw));
319 }
320
321 send_request::<models::GetIpRestrictionForASubAccountApiKeyResponse>(
322 &self.configuration,
323 "/sapi/v1/sub-account/subAccountApi/ipRestriction",
324 reqwest::Method::GET,
325 query_params,
326 body_params,
327 if HAS_TIME_UNIT {
328 self.configuration.time_unit
329 } else {
330 None
331 },
332 true,
333 )
334 .await
335 }
336}
337
338#[cfg(all(test, feature = "sub_account"))]
339mod tests {
340 use super::*;
341 use crate::TOKIO_SHARED_RT;
342 use crate::{errors::ConnectorError, models::DataFuture, models::RestApiRateLimit};
343 use async_trait::async_trait;
344 use std::collections::HashMap;
345
346 struct DummyRestApiResponse<T> {
347 inner: Box<dyn FnOnce() -> DataFuture<Result<T, ConnectorError>> + Send + Sync>,
348 status: u16,
349 headers: HashMap<String, String>,
350 rate_limits: Option<Vec<RestApiRateLimit>>,
351 }
352
353 impl<T> From<DummyRestApiResponse<T>> for RestApiResponse<T> {
354 fn from(dummy: DummyRestApiResponse<T>) -> Self {
355 Self {
356 data_fn: dummy.inner,
357 status: dummy.status,
358 headers: dummy.headers,
359 rate_limits: dummy.rate_limits,
360 }
361 }
362 }
363
364 struct MockApiManagementApiClient {
365 force_error: bool,
366 }
367
368 #[async_trait]
369 impl ApiManagementApi for MockApiManagementApiClient {
370 async fn add_ip_restriction_for_sub_account_api_key(
371 &self,
372 _params: AddIpRestrictionForSubAccountApiKeyParams,
373 ) -> anyhow::Result<RestApiResponse<models::AddIpRestrictionForSubAccountApiKeyResponse>>
374 {
375 if self.force_error {
376 return Err(ConnectorError::ConnectorClientError {
377 msg: "ResponseError".to_string(),
378 code: None,
379 }
380 .into());
381 }
382
383 let resp_json: Value = serde_json::from_str(r#"{"status":"2","ipList":["69.210.67.14","8.34.21.10"],"updateTime":1636371437000,"apiKey":"k5V49ldtn4tszj6W3hystegdfvmGbqDzjmkCtpTvC0G74WhK7yd4rfCTo4lShf"}"#).unwrap();
384 let dummy_response: models::AddIpRestrictionForSubAccountApiKeyResponse =
385 serde_json::from_value(resp_json.clone()).expect(
386 "should parse into models::AddIpRestrictionForSubAccountApiKeyResponse",
387 );
388
389 let dummy = DummyRestApiResponse {
390 inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
391 status: 200,
392 headers: HashMap::new(),
393 rate_limits: None,
394 };
395
396 Ok(dummy.into())
397 }
398
399 async fn delete_ip_list_for_a_sub_account_api_key(
400 &self,
401 _params: DeleteIpListForASubAccountApiKeyParams,
402 ) -> anyhow::Result<RestApiResponse<models::DeleteIpListForASubAccountApiKeyResponse>>
403 {
404 if self.force_error {
405 return Err(ConnectorError::ConnectorClientError {
406 msg: "ResponseError".to_string(),
407 code: None,
408 }
409 .into());
410 }
411
412 let resp_json: Value = serde_json::from_str(r#"{"ipRestrict":"true","ipList":["69.210.67.14","8.34.21.10"],"updateTime":1636371437000,"apiKey":"k5V49ldtn4tszj6W3hystegdfvmGbqDzjmkCtpTvC0G74WhK7yd4rfCTo4lShf"}"#).unwrap();
413 let dummy_response: models::DeleteIpListForASubAccountApiKeyResponse =
414 serde_json::from_value(resp_json.clone())
415 .expect("should parse into models::DeleteIpListForASubAccountApiKeyResponse");
416
417 let dummy = DummyRestApiResponse {
418 inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
419 status: 200,
420 headers: HashMap::new(),
421 rate_limits: None,
422 };
423
424 Ok(dummy.into())
425 }
426
427 async fn get_ip_restriction_for_a_sub_account_api_key(
428 &self,
429 _params: GetIpRestrictionForASubAccountApiKeyParams,
430 ) -> anyhow::Result<RestApiResponse<models::GetIpRestrictionForASubAccountApiKeyResponse>>
431 {
432 if self.force_error {
433 return Err(ConnectorError::ConnectorClientError {
434 msg: "ResponseError".to_string(),
435 code: None,
436 }
437 .into());
438 }
439
440 let resp_json: Value = serde_json::from_str(r#"{"ipRestrict":"true","ipList":["69.210.67.14","8.34.21.10"],"updateTime":1636371437000,"apiKey":"k5V49ldtn4tszj6W3hystegdfvmGbqDzjmkCtpTvC0G74WhK7yd4rfCTo4lShf"}"#).unwrap();
441 let dummy_response: models::GetIpRestrictionForASubAccountApiKeyResponse =
442 serde_json::from_value(resp_json.clone()).expect(
443 "should parse into models::GetIpRestrictionForASubAccountApiKeyResponse",
444 );
445
446 let dummy = DummyRestApiResponse {
447 inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
448 status: 200,
449 headers: HashMap::new(),
450 rate_limits: None,
451 };
452
453 Ok(dummy.into())
454 }
455 }
456
457 #[test]
458 fn add_ip_restriction_for_sub_account_api_key_required_params_success() {
459 TOKIO_SHARED_RT.block_on(async {
460 let client = MockApiManagementApiClient { force_error: false };
461
462 let params = AddIpRestrictionForSubAccountApiKeyParams::builder("sub-account-email@email.com".to_string(),"sub_account_api_key_example".to_string(),"status_example".to_string(),).build().unwrap();
463
464 let resp_json: Value = serde_json::from_str(r#"{"status":"2","ipList":["69.210.67.14","8.34.21.10"],"updateTime":1636371437000,"apiKey":"k5V49ldtn4tszj6W3hystegdfvmGbqDzjmkCtpTvC0G74WhK7yd4rfCTo4lShf"}"#).unwrap();
465 let expected_response : models::AddIpRestrictionForSubAccountApiKeyResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::AddIpRestrictionForSubAccountApiKeyResponse");
466
467 let resp = client.add_ip_restriction_for_sub_account_api_key(params).await.expect("Expected a response");
468 let data_future = resp.data();
469 let actual_response = data_future.await.unwrap();
470 assert_eq!(actual_response, expected_response);
471 });
472 }
473
474 #[test]
475 fn add_ip_restriction_for_sub_account_api_key_optional_params_success() {
476 TOKIO_SHARED_RT.block_on(async {
477 let client = MockApiManagementApiClient { force_error: false };
478
479 let params = AddIpRestrictionForSubAccountApiKeyParams::builder("sub-account-email@email.com".to_string(),"sub_account_api_key_example".to_string(),"status_example".to_string(),).ip_address("ip_address_example".to_string()).recv_window(5000).build().unwrap();
480
481 let resp_json: Value = serde_json::from_str(r#"{"status":"2","ipList":["69.210.67.14","8.34.21.10"],"updateTime":1636371437000,"apiKey":"k5V49ldtn4tszj6W3hystegdfvmGbqDzjmkCtpTvC0G74WhK7yd4rfCTo4lShf"}"#).unwrap();
482 let expected_response : models::AddIpRestrictionForSubAccountApiKeyResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::AddIpRestrictionForSubAccountApiKeyResponse");
483
484 let resp = client.add_ip_restriction_for_sub_account_api_key(params).await.expect("Expected a response");
485 let data_future = resp.data();
486 let actual_response = data_future.await.unwrap();
487 assert_eq!(actual_response, expected_response);
488 });
489 }
490
491 #[test]
492 fn add_ip_restriction_for_sub_account_api_key_response_error() {
493 TOKIO_SHARED_RT.block_on(async {
494 let client = MockApiManagementApiClient { force_error: true };
495
496 let params = AddIpRestrictionForSubAccountApiKeyParams::builder(
497 "sub-account-email@email.com".to_string(),
498 "sub_account_api_key_example".to_string(),
499 "status_example".to_string(),
500 )
501 .build()
502 .unwrap();
503
504 match client
505 .add_ip_restriction_for_sub_account_api_key(params)
506 .await
507 {
508 Ok(_) => panic!("Expected an error"),
509 Err(err) => {
510 assert_eq!(err.to_string(), "Connector client error: ResponseError");
511 }
512 }
513 });
514 }
515
516 #[test]
517 fn delete_ip_list_for_a_sub_account_api_key_required_params_success() {
518 TOKIO_SHARED_RT.block_on(async {
519 let client = MockApiManagementApiClient { force_error: false };
520
521 let params = DeleteIpListForASubAccountApiKeyParams::builder("sub-account-email@email.com".to_string(),"sub_account_api_key_example".to_string(),"ip_address_example".to_string(),).build().unwrap();
522
523 let resp_json: Value = serde_json::from_str(r#"{"ipRestrict":"true","ipList":["69.210.67.14","8.34.21.10"],"updateTime":1636371437000,"apiKey":"k5V49ldtn4tszj6W3hystegdfvmGbqDzjmkCtpTvC0G74WhK7yd4rfCTo4lShf"}"#).unwrap();
524 let expected_response : models::DeleteIpListForASubAccountApiKeyResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::DeleteIpListForASubAccountApiKeyResponse");
525
526 let resp = client.delete_ip_list_for_a_sub_account_api_key(params).await.expect("Expected a response");
527 let data_future = resp.data();
528 let actual_response = data_future.await.unwrap();
529 assert_eq!(actual_response, expected_response);
530 });
531 }
532
533 #[test]
534 fn delete_ip_list_for_a_sub_account_api_key_optional_params_success() {
535 TOKIO_SHARED_RT.block_on(async {
536 let client = MockApiManagementApiClient { force_error: false };
537
538 let params = DeleteIpListForASubAccountApiKeyParams::builder("sub-account-email@email.com".to_string(),"sub_account_api_key_example".to_string(),"ip_address_example".to_string(),).recv_window(5000).build().unwrap();
539
540 let resp_json: Value = serde_json::from_str(r#"{"ipRestrict":"true","ipList":["69.210.67.14","8.34.21.10"],"updateTime":1636371437000,"apiKey":"k5V49ldtn4tszj6W3hystegdfvmGbqDzjmkCtpTvC0G74WhK7yd4rfCTo4lShf"}"#).unwrap();
541 let expected_response : models::DeleteIpListForASubAccountApiKeyResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::DeleteIpListForASubAccountApiKeyResponse");
542
543 let resp = client.delete_ip_list_for_a_sub_account_api_key(params).await.expect("Expected a response");
544 let data_future = resp.data();
545 let actual_response = data_future.await.unwrap();
546 assert_eq!(actual_response, expected_response);
547 });
548 }
549
550 #[test]
551 fn delete_ip_list_for_a_sub_account_api_key_response_error() {
552 TOKIO_SHARED_RT.block_on(async {
553 let client = MockApiManagementApiClient { force_error: true };
554
555 let params = DeleteIpListForASubAccountApiKeyParams::builder(
556 "sub-account-email@email.com".to_string(),
557 "sub_account_api_key_example".to_string(),
558 "ip_address_example".to_string(),
559 )
560 .build()
561 .unwrap();
562
563 match client
564 .delete_ip_list_for_a_sub_account_api_key(params)
565 .await
566 {
567 Ok(_) => panic!("Expected an error"),
568 Err(err) => {
569 assert_eq!(err.to_string(), "Connector client error: ResponseError");
570 }
571 }
572 });
573 }
574
575 #[test]
576 fn get_ip_restriction_for_a_sub_account_api_key_required_params_success() {
577 TOKIO_SHARED_RT.block_on(async {
578 let client = MockApiManagementApiClient { force_error: false };
579
580 let params = GetIpRestrictionForASubAccountApiKeyParams::builder("sub-account-email@email.com".to_string(),"sub_account_api_key_example".to_string(),).build().unwrap();
581
582 let resp_json: Value = serde_json::from_str(r#"{"ipRestrict":"true","ipList":["69.210.67.14","8.34.21.10"],"updateTime":1636371437000,"apiKey":"k5V49ldtn4tszj6W3hystegdfvmGbqDzjmkCtpTvC0G74WhK7yd4rfCTo4lShf"}"#).unwrap();
583 let expected_response : models::GetIpRestrictionForASubAccountApiKeyResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetIpRestrictionForASubAccountApiKeyResponse");
584
585 let resp = client.get_ip_restriction_for_a_sub_account_api_key(params).await.expect("Expected a response");
586 let data_future = resp.data();
587 let actual_response = data_future.await.unwrap();
588 assert_eq!(actual_response, expected_response);
589 });
590 }
591
592 #[test]
593 fn get_ip_restriction_for_a_sub_account_api_key_optional_params_success() {
594 TOKIO_SHARED_RT.block_on(async {
595 let client = MockApiManagementApiClient { force_error: false };
596
597 let params = GetIpRestrictionForASubAccountApiKeyParams::builder("sub-account-email@email.com".to_string(),"sub_account_api_key_example".to_string(),).recv_window(5000).build().unwrap();
598
599 let resp_json: Value = serde_json::from_str(r#"{"ipRestrict":"true","ipList":["69.210.67.14","8.34.21.10"],"updateTime":1636371437000,"apiKey":"k5V49ldtn4tszj6W3hystegdfvmGbqDzjmkCtpTvC0G74WhK7yd4rfCTo4lShf"}"#).unwrap();
600 let expected_response : models::GetIpRestrictionForASubAccountApiKeyResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetIpRestrictionForASubAccountApiKeyResponse");
601
602 let resp = client.get_ip_restriction_for_a_sub_account_api_key(params).await.expect("Expected a response");
603 let data_future = resp.data();
604 let actual_response = data_future.await.unwrap();
605 assert_eq!(actual_response, expected_response);
606 });
607 }
608
609 #[test]
610 fn get_ip_restriction_for_a_sub_account_api_key_response_error() {
611 TOKIO_SHARED_RT.block_on(async {
612 let client = MockApiManagementApiClient { force_error: true };
613
614 let params = GetIpRestrictionForASubAccountApiKeyParams::builder(
615 "sub-account-email@email.com".to_string(),
616 "sub_account_api_key_example".to_string(),
617 )
618 .build()
619 .unwrap();
620
621 match client
622 .get_ip_restriction_for_a_sub_account_api_key(params)
623 .await
624 {
625 Ok(_) => panic!("Expected an error"),
626 Err(err) => {
627 assert_eq!(err.to_string(), "Connector client error: ResponseError");
628 }
629 }
630 });
631 }
632}