aws_sdk_computeoptimizer/operation/get_lambda_function_recommendations/
paginator.rs

1// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
2/// Paginator for [`GetLambdaFunctionRecommendations`](crate::operation::get_lambda_function_recommendations::GetLambdaFunctionRecommendations)
3pub struct GetLambdaFunctionRecommendationsPaginator {
4    handle: std::sync::Arc<crate::client::Handle>,
5    builder: crate::operation::get_lambda_function_recommendations::builders::GetLambdaFunctionRecommendationsInputBuilder,
6    stop_on_duplicate_token: bool,
7}
8
9impl GetLambdaFunctionRecommendationsPaginator {
10    /// Create a new paginator-wrapper
11    pub(crate) fn new(
12        handle: std::sync::Arc<crate::client::Handle>,
13        builder: crate::operation::get_lambda_function_recommendations::builders::GetLambdaFunctionRecommendationsInputBuilder,
14    ) -> Self {
15        Self {
16            handle,
17            builder,
18            stop_on_duplicate_token: true,
19        }
20    }
21
22    /// Set the page size
23    ///
24    /// _Note: this method will override any previously set value for `max_results`_
25    pub fn page_size(mut self, limit: i32) -> Self {
26        self.builder.max_results = ::std::option::Option::Some(limit);
27        self
28    }
29
30    /// Create a flattened paginator
31    ///
32    /// This paginator automatically flattens results using `lambda_function_recommendations`. Queries to the underlying service
33    /// are dispatched lazily.
34    pub fn items(self) -> crate::operation::get_lambda_function_recommendations::paginator::GetLambdaFunctionRecommendationsPaginatorItems {
35        crate::operation::get_lambda_function_recommendations::paginator::GetLambdaFunctionRecommendationsPaginatorItems(self)
36    }
37
38    /// Stop paginating when the service returns the same pagination token twice in a row.
39    ///
40    /// Defaults to true.
41    ///
42    /// For certain operations, it may be useful to continue on duplicate token. For example,
43    /// if an operation is for tailing a log file in real-time, then continuing may be desired.
44    /// This option can be set to `false` to accommodate these use cases.
45    pub fn stop_on_duplicate_token(mut self, stop_on_duplicate_token: bool) -> Self {
46        self.stop_on_duplicate_token = stop_on_duplicate_token;
47        self
48    }
49
50    /// Create the pagination stream
51    ///
52    /// _Note:_ No requests will be dispatched until the stream is used
53    /// (e.g. with the [`.next().await`](aws_smithy_async::future::pagination_stream::PaginationStream::next) method).
54    pub fn send(
55        self,
56    ) -> ::aws_smithy_async::future::pagination_stream::PaginationStream<
57        ::std::result::Result<
58            crate::operation::get_lambda_function_recommendations::GetLambdaFunctionRecommendationsOutput,
59            ::aws_smithy_runtime_api::client::result::SdkError<
60                crate::operation::get_lambda_function_recommendations::GetLambdaFunctionRecommendationsError,
61                ::aws_smithy_runtime_api::client::orchestrator::HttpResponse,
62            >,
63        >,
64    > {
65        // Move individual fields out of self for the borrow checker
66        let builder = self.builder;
67        let handle = self.handle;
68        let runtime_plugins = crate::operation::get_lambda_function_recommendations::GetLambdaFunctionRecommendations::operation_runtime_plugins(
69            handle.runtime_plugins.clone(),
70            &handle.conf,
71            ::std::option::Option::None,
72        )
73        .with_operation_plugin(crate::sdk_feature_tracker::paginator::PaginatorFeatureTrackerRuntimePlugin::new());
74        ::aws_smithy_async::future::pagination_stream::PaginationStream::new(::aws_smithy_async::future::pagination_stream::fn_stream::FnStream::new(
75            move |tx| {
76                ::std::boxed::Box::pin(async move {
77                    // Build the input for the first time. If required fields are missing, this is where we'll produce an early error.
78                    let mut input = match builder
79                        .build()
80                        .map_err(::aws_smithy_runtime_api::client::result::SdkError::construction_failure)
81                    {
82                        ::std::result::Result::Ok(input) => input,
83                        ::std::result::Result::Err(e) => {
84                            let _ = tx.send(::std::result::Result::Err(e)).await;
85                            return;
86                        }
87                    };
88                    loop {
89                        let resp = crate::operation::get_lambda_function_recommendations::GetLambdaFunctionRecommendations::orchestrate(
90                            &runtime_plugins,
91                            input.clone(),
92                        )
93                        .await;
94                        // If the input member is None or it was an error
95                        let done = match resp {
96                            ::std::result::Result::Ok(ref resp) => {
97                                let new_token = crate::lens::reflens_get_lambda_function_recommendations_output_output_next_token(resp);
98                                // Pagination is exhausted when the next token is an empty string
99                                let is_empty = new_token.map(|token| token.is_empty()).unwrap_or(true);
100                                if !is_empty && new_token == input.next_token.as_ref() && self.stop_on_duplicate_token {
101                                    true
102                                } else {
103                                    input.next_token = new_token.cloned();
104                                    is_empty
105                                }
106                            }
107                            ::std::result::Result::Err(_) => true,
108                        };
109                        if tx.send(resp).await.is_err() {
110                            // receiving end was dropped
111                            return;
112                        }
113                        if done {
114                            return;
115                        }
116                    }
117                })
118            },
119        ))
120    }
121}
122
123/// Flattened paginator for `GetLambdaFunctionRecommendationsPaginator`
124///
125/// This is created with [`.items()`](GetLambdaFunctionRecommendationsPaginator::items)
126pub struct GetLambdaFunctionRecommendationsPaginatorItems(GetLambdaFunctionRecommendationsPaginator);
127
128impl GetLambdaFunctionRecommendationsPaginatorItems {
129    /// Create the pagination stream
130    ///
131    /// _Note_: No requests will be dispatched until the stream is used
132    /// (e.g. with the [`.next().await`](aws_smithy_async::future::pagination_stream::PaginationStream::next) method).
133    ///
134    /// To read the entirety of the paginator, use [`.collect::<Result<Vec<_>, _>()`](aws_smithy_async::future::pagination_stream::PaginationStream::collect).
135    pub fn send(
136        self,
137    ) -> ::aws_smithy_async::future::pagination_stream::PaginationStream<
138        ::std::result::Result<
139            crate::types::LambdaFunctionRecommendation,
140            ::aws_smithy_runtime_api::client::result::SdkError<
141                crate::operation::get_lambda_function_recommendations::GetLambdaFunctionRecommendationsError,
142                ::aws_smithy_runtime_api::client::orchestrator::HttpResponse,
143            >,
144        >,
145    > {
146        ::aws_smithy_async::future::pagination_stream::TryFlatMap::new(self.0.send()).flat_map(|page| {
147            crate::lens::lens_get_lambda_function_recommendations_output_output_lambda_function_recommendations(page)
148                .unwrap_or_default()
149                .into_iter()
150        })
151    }
152}