From 9cf56ead0c33b8adca296f8ed730adb29876a9e5 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Sat, 5 Oct 2024 09:42:20 -0700 Subject: [PATCH] feat: allow invalidating `list_keys` cache (#42) --- src/models/apis.rs | 25 +++++++++++++++++++++++++ src/services/apis.rs | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/src/models/apis.rs b/src/models/apis.rs index 0b96094..8a73d8c 100644 --- a/src/models/apis.rs +++ b/src/models/apis.rs @@ -17,6 +17,9 @@ pub struct ListKeysRequest { /// The pagination cursor indicating the last key that was returned. pub cursor: Option, + + /// Whether to revalidate the cache for this request. + pub revalidate_cache: Option, } impl ListKeysRequest { @@ -37,6 +40,7 @@ impl ListKeysRequest { /// assert_eq!(r.limit, None); /// assert_eq!(r.cursor, None); /// assert_eq!(r.owner_id, None); + /// assert_eq!(r.revalidate_cache, None); /// ``` #[must_use] pub fn new>(api_id: T) -> Self { @@ -45,6 +49,7 @@ impl ListKeysRequest { owner_id: None, limit: None, cursor: None, + revalidate_cache: None, } } @@ -110,6 +115,26 @@ impl ListKeysRequest { self.owner_id = Some(owner_id.into()); self } + + /// Sets the flag for revalidating the cache for this request. + /// + /// # Arguments + /// - `revalidate_cache`: Whether to revalidate the cache with this request. + /// + /// # Returns + /// Self for chained calls. + /// + /// # Example + /// ``` + /// # use unkey::models::ListKeysRequest; + /// let r = ListKeysRequest::new("test").set_revalidate_cache(true); + /// + /// assert_eq!(r.revalidate_cache.unwrap(), true); + /// ``` + pub fn set_revalidate_cache(mut self, revalidate_cache: bool) -> Self { + self.revalidate_cache = Some(revalidate_cache); + self + } } /// An incoming paginated list keys response. diff --git a/src/services/apis.rs b/src/services/apis.rs index 4083591..d0dadd6 100644 --- a/src/services/apis.rs +++ b/src/services/apis.rs @@ -36,6 +36,10 @@ impl ApiService { .query_insert("apiId", &req.api_id) .query_insert("limit", &req.limit.unwrap_or(100).to_string()); + if let Some(revalidate) = &req.revalidate_cache { + route.query_insert("revalidateKeysCache", &revalidate.to_string()); + } + if let Some(owner) = &req.owner_id { route.query_insert("ownerId", owner); }