|
12 | 12 | // |
13 | 13 | // SPDX-License-Identifier: Apache-2.0 |
14 | 14 | //! # Common API helpers |
15 | | -use crate::api::error::KeystoneApiError; |
16 | | -use crate::api::types::ScopeProject; |
| 15 | +use serde::Serialize; |
| 16 | +use url::Url; |
| 17 | + |
| 18 | +use crate::api::KeystoneApiError; |
| 19 | +use crate::api::types::{Link, ScopeProject}; |
| 20 | +use crate::config::Config; |
17 | 21 | use crate::keystone::ServiceState; |
18 | 22 | use crate::resource::{ |
19 | 23 | ResourceApi, |
@@ -117,15 +121,76 @@ pub async fn find_project_from_scope( |
117 | 121 | Ok(project) |
118 | 122 | } |
119 | 123 |
|
| 124 | +/// Prepare the links for the paginated resource collection. |
| 125 | +pub fn build_pagination_links<T, Q>( |
| 126 | + config: &Config, |
| 127 | + data: &[T], |
| 128 | + query: &Q, |
| 129 | + collection_url: &str, |
| 130 | +) -> Result<Option<Vec<Link>>, KeystoneApiError> |
| 131 | +where |
| 132 | + T: ResourceIdentifier, |
| 133 | + Q: QueryParameterPagination + Clone + Serialize, |
| 134 | +{ |
| 135 | + Ok(match &query.get_limit() { |
| 136 | + Some(limit) => { |
| 137 | + if (data.len() as u64) >= *limit |
| 138 | + && let Some(last_id) = data.last().map(|x| x.get_id()) |
| 139 | + { |
| 140 | + let mut url = Url::parse( |
| 141 | + config |
| 142 | + .default |
| 143 | + .public_endpoint |
| 144 | + .as_ref() |
| 145 | + .map_or("http://localhost", |v| v), |
| 146 | + )?; |
| 147 | + url.set_path(collection_url); |
| 148 | + let mut new_query = query.clone(); |
| 149 | + |
| 150 | + new_query.set_marker(last_id); |
| 151 | + url.set_query(Some(&serde_urlencoded::to_string(&new_query)?)); |
| 152 | + |
| 153 | + let next_page_url = format!( |
| 154 | + "{}{}", |
| 155 | + url.path(), |
| 156 | + url.query().map(|q| format!("?{}", q)).unwrap_or_default() |
| 157 | + ); |
| 158 | + Some(vec![Link { |
| 159 | + rel: String::from("next"), |
| 160 | + href: next_page_url, |
| 161 | + }]) |
| 162 | + } else { |
| 163 | + None |
| 164 | + } |
| 165 | + } |
| 166 | + None => None, |
| 167 | + }) |
| 168 | +} |
| 169 | + |
| 170 | +/// Resource query parameters pagination extension trait. |
| 171 | +pub trait QueryParameterPagination { |
| 172 | + /// Get the page limit. |
| 173 | + fn get_limit(&self) -> Option<u64>; |
| 174 | + /// Set the pagination marker. |
| 175 | + fn set_marker(&mut self, marker: String) -> &mut Self; |
| 176 | +} |
| 177 | + |
| 178 | +/// Trait for the resource to expose the unique identifier that can be used for building the |
| 179 | +/// marker pagination. |
| 180 | +pub trait ResourceIdentifier { |
| 181 | + /// Get the unique resource identifier. |
| 182 | + fn get_id(&self) -> String; |
| 183 | +} |
| 184 | + |
120 | 185 | #[cfg(test)] |
121 | 186 | mod tests { |
| 187 | + use rstest::rstest; |
122 | 188 | use sea_orm::DatabaseConnection; |
123 | 189 | use std::sync::Arc; |
124 | 190 |
|
125 | 191 | use super::*; |
126 | 192 |
|
127 | 193 | use crate::config::Config; |
128 | | - |
129 | 194 | use crate::keystone::Service; |
130 | 195 | use crate::policy::MockPolicyFactory; |
131 | 196 | use crate::provider::Provider; |
@@ -197,4 +262,87 @@ mod tests { |
197 | 262 | } |
198 | 263 | } |
199 | 264 | } |
| 265 | + |
| 266 | + /// Fake resource for pagination testing |
| 267 | + struct FakeResource { |
| 268 | + pub id: String, |
| 269 | + } |
| 270 | + |
| 271 | + /// Fake query params for pagination testing |
| 272 | + #[derive(Clone, Default, Serialize)] |
| 273 | + struct FakeQueryParams { |
| 274 | + pub marker: Option<String>, |
| 275 | + pub limit: Option<u64>, |
| 276 | + } |
| 277 | + |
| 278 | + impl ResourceIdentifier for FakeResource { |
| 279 | + fn get_id(&self) -> String { |
| 280 | + self.id.clone() |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + impl QueryParameterPagination for FakeQueryParams { |
| 285 | + fn get_limit(&self) -> Option<u64> { |
| 286 | + self.limit |
| 287 | + } |
| 288 | + |
| 289 | + fn set_marker(&mut self, marker: String) -> &mut Self { |
| 290 | + self.marker = Some(marker); |
| 291 | + self |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + /// Parameterized pagination test |
| 296 | + #[rstest] |
| 297 | + #[case(5, FakeQueryParams::default(), None)] |
| 298 | + #[case(5, FakeQueryParams{marker: Some("x".into()), limit: None}, None)] |
| 299 | + #[case(5, FakeQueryParams{marker: Some("x".into()), limit: Some(6)}, None)] |
| 300 | + #[case(5, FakeQueryParams{marker: Some("x".into()), limit: Some(5)}, Some(vec![ |
| 301 | + Link { |
| 302 | + rel: "next".into(), |
| 303 | + href: "/foo/bar?marker=4&limit=5".into() |
| 304 | + }]) |
| 305 | + )] |
| 306 | + #[case(5, FakeQueryParams{marker: Some("x".into()), limit: Some(3)}, Some(vec![ |
| 307 | + Link { |
| 308 | + rel: "next".into(), |
| 309 | + href: "/foo/bar?marker=4&limit=3".into() |
| 310 | + }]) |
| 311 | + )] |
| 312 | + #[case(5, FakeQueryParams{marker: Some("x".into()), limit: Some(1)}, Some(vec![ |
| 313 | + Link { |
| 314 | + rel: "next".into(), |
| 315 | + href: "/foo/bar?marker=4&limit=1".into() |
| 316 | + }]) |
| 317 | + )] |
| 318 | + #[case(5, FakeQueryParams{marker: Some("x".into()), limit: Some(0)}, Some(vec![ |
| 319 | + Link { |
| 320 | + rel: "next".into(), |
| 321 | + href: "/foo/bar?marker=4&limit=0".into() |
| 322 | + }]) |
| 323 | + )] |
| 324 | + #[case(0, FakeQueryParams{marker: Some("x".into()), limit: Some(6)}, None)] |
| 325 | + #[case(0, FakeQueryParams{marker: None, limit: Some(6)}, None)] |
| 326 | + #[case(5, FakeQueryParams{marker: None, limit: Some(5)}, Some(vec![ |
| 327 | + Link { |
| 328 | + rel: "next".into(), |
| 329 | + href: "/foo/bar?marker=4&limit=5".into() |
| 330 | + }]) |
| 331 | + )] |
| 332 | + fn test_pagination( |
| 333 | + #[case] cnt: usize, |
| 334 | + #[case] query: FakeQueryParams, |
| 335 | + #[case] expected: Option<Vec<Link>>, |
| 336 | + ) { |
| 337 | + assert_eq!( |
| 338 | + build_pagination_links( |
| 339 | + &Config::default(), |
| 340 | + Vec::from_iter((0..cnt).map(|x| FakeResource { id: x.to_string() })).as_slice(), |
| 341 | + &query, |
| 342 | + "foo/bar", |
| 343 | + ) |
| 344 | + .unwrap(), |
| 345 | + expected |
| 346 | + ); |
| 347 | + } |
200 | 348 | } |
0 commit comments