Skip to content

Fix query parameter for saveAsKind #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/annotations/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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<S: Into<String>>(mut self, url: S) -> Self {
self.url = Some(url.into());
Expand Down
63 changes: 53 additions & 10 deletions src/datasets/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryKind>,
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,
}
}
Expand All @@ -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<DateTime<Utc>>,
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
);
}
}
1 change: 1 addition & 0 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl Client {
}
}

#[derive(Debug)]
pub(crate) struct Response {
inner: reqwest::Response,
method: http::Method,
Expand Down
Loading