Skip to content

Commit

Permalink
products_v2: support archived and drop org
Browse files Browse the repository at this point in the history
  • Loading branch information
danielspofford committed Oct 10, 2023
1 parent 08de371 commit ff7e8bf
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
35 changes: 34 additions & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,23 @@ impl Api {
P: AsRef<str> + Display,
T: DeserializeOwned,
{
self.execute_with_headers(method, path, body, vec![]).await
self.execute_with_params_and_headers(method, path, body, vec![], vec![])
.await
}

async fn execute_with_params<P, T>(
&self,
method: Method,
path: P,
body: Option<BodyType>,
params: Vec<(String, String)>,
) -> Result<Option<T>, Error>
where
P: AsRef<str> + Display,
T: DeserializeOwned,
{
self.execute_with_params_and_headers(method, path, body, params, vec![])
.await
}

async fn execute_with_headers<P, T>(
Expand All @@ -257,6 +273,22 @@ impl Api {
body: Option<BodyType>,
headers: Vec<(String, String)>,
) -> Result<Option<T>, Error>
where
P: AsRef<str> + Display,
T: DeserializeOwned,
{
self.execute_with_params_and_headers(method, path, body, vec![], headers)
.await
}

async fn execute_with_params_and_headers<P, T>(
&self,
method: Method,
path: P,
body: Option<BodyType>,
params: Vec<(String, String)>,
headers: Vec<(String, String)>,
) -> Result<Option<T>, Error>
where
P: AsRef<str> + Display,
T: DeserializeOwned,
Expand All @@ -273,6 +305,7 @@ impl Api {
let req_builder = self
.http
.request(method.clone(), endpoint)
.query(&params)
.header("Authorization", format!("Token {}", &self.api_key))
.headers(hmap);

Expand Down
25 changes: 18 additions & 7 deletions src/api/products_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use snafu::ResultExt;

#[derive(Debug, Deserialize, Serialize)]
pub struct ProductV2 {
pub archived: bool,
pub inserted_at: String,
pub name: String,
pub organization_prn: String,
pub prn: String,
pub updated_at: String,
}
Expand Down Expand Up @@ -56,6 +56,9 @@ pub struct UpdateProductV2Params {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub archived: Option<bool>,
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -96,13 +99,21 @@ impl<'a> ProductsV2Api<'a> {
&'a self,
params: ListProductsV2Params,
) -> Result<Option<ListProductsV2Response>, Error> {
let search_string = params.search;
let mut query_params = vec![("search".to_string(), params.search)];

if let Some(limit) = params.limit {
query_params.push(("limit".to_string(), limit.to_string()))
}
if let Some(order) = params.order {
query_params.push(("order".to_string(), order))
}

if let Some(page) = params.page {
query_params.push(("page".to_string(), page))
}

self.0
.execute(
Method::GET,
format!("/products?search={search_string}"),
None,
)
.execute_with_params(Method::GET, "/products".to_string(), None, query_params)
.await
}

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/products-v2-create-201.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"product": {
"archived": false,
"inserted_at": "2000-01-01T00:00:00Z",
"name": "name",
"organization_prn": "organization_prn",
"prn": "prn",
"updated_at": "2000-01-01T00:00:00Z"
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/products-v2-get-200.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"product": {
"archived": false,
"inserted_at": "2000-01-01T00:00:00Z",
"name": "name",
"organization_prn": "organization_prn",
"prn": "prn",
"updated_at": "2000-01-01T00:00:00Z"
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/products-v2-update-200.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"product": {
"archived": true,
"inserted_at": "2000-01-01T00:00:00Z",
"name": "name",
"organization_prn": "organization_prn",
"prn": "prn",
"updated_at": "2000-01-01T00:00:00Z"
}
Expand Down
17 changes: 3 additions & 14 deletions tests/products_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ async fn create_product() {
match api.products_v2().create(params).await.unwrap() {
Some(product) => {
assert_eq!(product.product.name, expected_name.to_string());
assert_eq!(
product.product.organization_prn,
expected_organization_prn.to_string()
);
}
_ => panic!(),
}
Expand All @@ -47,7 +43,6 @@ async fn create_product() {
#[tokio::test]
async fn get_product() {
let expected_name = "name";
let expected_organization_prn = "organization_prn";
let expected_prn = "prn";

let api = Api::new(ApiOptions {
Expand All @@ -69,10 +64,6 @@ async fn get_product() {
match api.products_v2().get(params).await.unwrap() {
Some(product) => {
assert_eq!(product.product.name, expected_name.to_string());
assert_eq!(
product.product.organization_prn,
expected_organization_prn.to_string()
);
}
_ => panic!(),
}
Expand All @@ -82,8 +73,8 @@ async fn get_product() {

#[tokio::test]
async fn update_product() {
let expected_archived = true;
let expected_name = "name";
let expected_organization_prn = "organization_prn";
let expected_prn = "prn";

let api = Api::new(ApiOptions {
Expand All @@ -101,15 +92,13 @@ async fn update_product() {
let params = UpdateProductV2Params {
prn: expected_prn.to_string(),
name: Some(expected_name.to_string()),
archived: Some(true),
};

match api.products_v2().update(params).await.unwrap() {
Some(product) => {
assert_eq!(product.product.name, expected_name.to_string());
assert_eq!(
product.product.organization_prn,
expected_organization_prn.to_string()
);
assert_eq!(product.product.archived, expected_archived);
}
_ => panic!(),
}
Expand Down

0 comments on commit ff7e8bf

Please sign in to comment.