Skip to content

Commit

Permalink
🚧 Introduce SearchRequest for Marktplaats
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Sep 4, 2024
1 parent eaa5cae commit 03a6692
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module_name_repetitions = "allow"

[dependencies]
anyhow = { version = "1.0.79", features = ["backtrace"] }
bon = "2.1.1"
chrono = { version = "0.4.38", features = ["serde"] }
chrono-humanize = "0.2.3"
clap = { version = "4.4.16", features = ["cargo", "derive", "env"] }
Expand All @@ -51,6 +52,7 @@ rust_decimal_macros = "1.35.0"
sentry = { version = "0.34.0", default-features = false, features = ["anyhow", "backtrace", "contexts", "panic", "reqwest", "rustls", "tracing"] }
serde = "1.0.204"
serde_json = "1.0.120"
serde_qs = "0.13.0"
thiserror = "1.0.63"
tokio = { version = "1.35.1", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.40"
Expand Down
80 changes: 80 additions & 0 deletions src/marktplaats.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub mod listing;

use bon::builder;
use serde::Serialize;

use crate::prelude::*;

#[must_use]
Expand All @@ -26,3 +29,80 @@ impl Marktplaats {
.with_context(|| format!("failed to read search response for `{query}`"))
}
}

#[must_use]
#[builder]
#[derive(Serialize)]
pub struct SearchRequest<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub query: Option<&'a str>,

#[serde(skip_serializing_if = "Option::is_none")]
pub offset: Option<usize>,

#[serde(rename = "sortBy", skip_serializing_if = "Option::is_none")]
pub sort_by: Option<SortBy>,

#[serde(rename = "sortOrder", skip_serializing_if = "Option::is_none")]
pub sort_order: Option<SortOrder>,

#[serde(
rename = "searchInTitleAndDescription",
skip_serializing_if = "Option::is_none"
)]
pub search_in_title_and_description: Option<bool>,

#[serde(rename = "sellerIds")]
#[builder(default)]
pub seller_ids: &'a [u32],
}

#[must_use]
#[derive(Serialize)]
pub enum SortBy {
#[serde(rename = "OPTIMIZED")]
Optimized,

#[serde(rename = "SORT_INDEX")]
SortIndex,

#[serde(rename = "PRICE")]
Price,
}

#[must_use]
#[derive(Serialize)]
pub enum SortOrder {
#[serde(rename = "INCREASING")]
Increasing,

#[serde(rename = "DECREASING")]
Decreasing,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn seller_ids_ok() -> Result {
let request = SearchRequest::builder().seller_ids(&[42, 43]).build();
assert_eq!(
serde_qs::to_string(&request)?,
"sellerIds[0]=42&sellerIds[1]=43"
);
Ok(())
}

#[test]
fn search_in_title_and_description_ok() -> Result {
let request = SearchRequest::builder()
.search_in_title_and_description(true)
.build();
assert_eq!(
serde_qs::to_string(&request)?,
"searchInTitleAndDescription=true"
);
Ok(())
}
}

0 comments on commit 03a6692

Please sign in to comment.