From 29331142283dfd0443338cc15f480fc737918fd0 Mon Sep 17 00:00:00 2001 From: "Heinz N. Gies" Date: Thu, 27 Feb 2025 10:47:21 +0100 Subject: [PATCH] Fix query parameter for saveAsKind Signed-off-by: Heinz N. Gies --- src/annotations/model.rs | 1 - src/client.rs | 3 +- src/datasets/model.rs | 63 +++++++++++++++++++++++++++++++++------- src/http.rs | 1 + 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/annotations/model.rs b/src/annotations/model.rs index 37f39df..2c4d16f 100644 --- a/src/annotations/model.rs +++ b/src/annotations/model.rs @@ -5,7 +5,6 @@ use url::Url; /// An annotation. #[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] #[serde(rename_all = "camelCase")] - pub struct Annotation { /// Unique ID of the annotation pub id: String, diff --git a/src/client.rs b/src/client.rs index d878e5e..7ec3680 100644 --- a/src/client.rs +++ b/src/client.rs @@ -100,7 +100,7 @@ impl Client { /// Get client version. #[must_use] - pub fn version(&self) -> &str { + pub fn version(&self) -> &'static str { env!("CARGO_PKG_VERSION") } @@ -286,7 +286,6 @@ impl Builder { /// Add an URL to the client. This is only meant for testing purposes, you /// don't need to set it. - #[doc(hidden)] #[must_use] pub fn with_url>(mut self, url: S) -> Self { self.url = Some(url.into()); diff --git a/src/datasets/model.rs b/src/datasets/model.rs index b0c9848..ca253aa 100644 --- a/src/datasets/model.rs +++ b/src/datasets/model.rs @@ -276,21 +276,32 @@ impl Query { } } +// We need to pass a reference for serde compatibility. +#[allow(clippy::trivially_copy_pass_by_ref)] +fn is_false(b: &bool) -> bool { + !*b +} + // QueryParams is the part of `QueryOptions` that is added to the request url. #[derive(Serialize, Debug, Default)] pub(crate) struct QueryParams { - #[serde(rename = "nocache")] + #[serde(rename = "nocache", skip_serializing_if = "is_false")] pub no_cache: bool, - #[serde(rename = "saveAsKind")] - pub save: bool, + #[serde(rename = "saveAsKind", skip_serializing_if = "Option::is_none")] + pub save: Option, pub format: AplResultFormat, } impl From<&QueryOptions> for QueryParams { fn from(options: &QueryOptions) -> Self { + let save = if options.save { + Some(QueryKind::Apl) + } else { + None + }; Self { no_cache: options.no_cache, - save: options.save, + save, format: options.format, } } @@ -299,7 +310,7 @@ impl From<&QueryOptions> for QueryParams { // This is a configuration that just happens to have many flags. #[allow(clippy::struct_excessive_bools)] /// The optional parameters to APL query methods. -#[derive(Debug, Default, Serialize, Clone)] +#[derive(Debug, Default, Clone)] pub struct QueryOptions { /// The start time of the query. pub start_time: Option>, @@ -314,10 +325,6 @@ pub struct QueryOptions { pub no_cache: bool, /// Save the query on the server, if set to `true`. The ID of the saved query /// is returned with the query result as part of the response. - // NOTE: The server automatically sets the query kind to "apl" for queries - // going // to the "/_apl" query endpoint. This allows us to set any value - // for the // `saveAsKind` query param. For user experience, we use a bool - // here instead of forcing the user to set the value to `query.APL`. pub save: bool, /// Format specifies the format of the APL query. Defaults to Legacy. pub format: AplResultFormat, @@ -435,7 +442,7 @@ impl Serialize for AggregationOp { struct AggregationOpVisitor; -impl<'de> Visitor<'de> for AggregationOpVisitor { +impl Visitor<'_> for AggregationOpVisitor { type Value = AggregationOp; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -813,4 +820,40 @@ mod test { enum_repr ); } + + #[test] + fn test_kind_false() { + let query = QueryParams { + no_cache: false, + save: None, + format: AplResultFormat::Tabular, + }; + let json_repr = r#"{"format":"tabular"}"#; + assert_eq!( + serde_json::to_string(&query).expect("json error"), + json_repr + ); + + let query = QueryParams { + no_cache: true, + save: None, + format: AplResultFormat::Tabular, + }; + let json_repr = r#"{"nocache":true,"format":"tabular"}"#; + assert_eq!( + serde_json::to_string(&query).expect("json error"), + json_repr + ); + + let query = QueryParams { + no_cache: false, + save: Some(QueryKind::Apl), + format: AplResultFormat::Tabular, + }; + let json_repr = r#"{"saveAsKind":"apl","format":"tabular"}"#; + assert_eq!( + serde_json::to_string(&query).expect("json error"), + json_repr + ); + } } diff --git a/src/http.rs b/src/http.rs index 900d587..cc5a85b 100644 --- a/src/http.rs +++ b/src/http.rs @@ -179,6 +179,7 @@ impl Client { } } +#[derive(Debug)] pub(crate) struct Response { inner: reqwest::Response, method: http::Method,