-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add support for namePrefix
, tags
and project
filters
#54
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,69 @@ pub struct Features { | |
pub features: Vec<Feature>, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Clone)] | ||
pub struct TagFilter { | ||
pub name: String, | ||
pub value: String, | ||
} | ||
|
||
impl TagFilter { | ||
teqm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn format(&self) -> String { | ||
format!("{}:{}", self.name, self.value) | ||
} | ||
} | ||
|
||
#[derive(Serialize, Debug)] | ||
teqm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
struct FeaturesQuery { | ||
pub project: Option<String>, | ||
#[serde(rename = "namePrefix")] | ||
pub name_prefix: Option<String>, | ||
#[serde(rename = "tag")] | ||
pub tags: Option<Vec<String>>, | ||
} | ||
|
||
impl Features { | ||
pub fn endpoint(api_url: &str) -> String { | ||
format!("{}/client/features", api_url) | ||
} | ||
|
||
#[cfg(feature = "surf")] | ||
pub fn query( | ||
project: Option<String>, | ||
name_prefix: Option<String>, | ||
tags: &Option<Vec<TagFilter>>, | ||
) -> impl Serialize + std::fmt::Debug { | ||
FeaturesQuery { | ||
project, | ||
name_prefix, | ||
tags: tags.as_ref().map(|tags| tags.iter().map(|tag| tag.format()).collect()), | ||
teqm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
#[cfg(not(feature = "surf"))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these parts should be written against the local http client trait, so that both surf and reqwest users gain them. That trait may need tweaking to accomodate your needs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find a "nice" way of implementing it 🤔 I didn't want to polute the trait with the logic of features query, so instead I made it that the query is already present in the url when it's passed to the http layer. wdyt is it fine to do it that way? |
||
pub fn query( | ||
project: Option<String>, | ||
name_prefix: Option<String>, | ||
tags: &Option<Vec<TagFilter>>, | ||
) -> impl Serialize + std::fmt::Debug { | ||
let mut query = vec![]; | ||
|
||
if let Some(project) = project { | ||
query.push(("project", project)) | ||
} | ||
|
||
if let Some(name_prefix) = name_prefix { | ||
query.push(("namePrefix", name_prefix)) | ||
} | ||
|
||
if let Some(tags) = tags { | ||
for tag in tags.iter() { | ||
query.push(("tag", tag.format())) | ||
} | ||
} | ||
|
||
query | ||
} | ||
} | ||
|
||
#[derive(Clone, Serialize, Deserialize, Debug)] | ||
|
@@ -133,7 +192,11 @@ pub struct MetricsBucket { | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Registration; | ||
use super::{Features, Registration, TagFilter}; | ||
#[cfg(feature = "surf")] | ||
use serde_qs::to_string; | ||
#[cfg(not(feature = "surf"))] | ||
use serde_urlencoded::to_string; | ||
|
||
#[test] | ||
fn parse_reference_doc() -> Result<(), serde_json::Error> { | ||
|
@@ -209,7 +272,7 @@ mod tests { | |
] | ||
} | ||
"#; | ||
let parsed: super::Features = serde_json::from_str(data)?; | ||
let parsed: Features = serde_json::from_str(data)?; | ||
assert_eq!(1, parsed.version); | ||
Ok(()) | ||
} | ||
|
@@ -224,4 +287,35 @@ mod tests { | |
..Default::default() | ||
}; | ||
} | ||
|
||
#[test] | ||
fn test_features_query() { | ||
let query = Features::query( | ||
Some("myproject".into()), | ||
Some("prefix".into()), | ||
&Some(vec![ | ||
TagFilter { | ||
name: "simple".into(), | ||
value: "taga".into(), | ||
}, | ||
TagFilter { | ||
name: "simple".into(), | ||
value: "tagb".into(), | ||
}, | ||
]), | ||
); | ||
|
||
let serialized = to_string(&query).unwrap(); | ||
|
||
#[cfg(feature = "surf")] | ||
assert_eq!( | ||
"project=myproject&namePrefix=prefix&tag[0]=simple%3Ataga&tag[1]=simple%3Atagb", | ||
serialized | ||
); | ||
#[cfg(not(feature = "surf"))] | ||
assert_eq!( | ||
"project=myproject&namePrefix=prefix&tag=simple%3Ataga&tag=simple%3Atagb", | ||
serialized | ||
); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.