From 0a9a1bc1cab904d6ffd7ad2029054f8693750d53 Mon Sep 17 00:00:00 2001 From: Daniel Spofford <868014+danielspofford@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:31:47 -0600 Subject: [PATCH] add products_v2 --- src/api/mod.rs | 6 + src/api/products_v2.rs | 133 +++++++++++++++++++++ tests/fixtures/products-v2-create-201.json | 9 ++ tests/fixtures/products-v2-get-200.json | 9 ++ tests/fixtures/products-v2-update-200.json | 9 ++ tests/products_v2.rs | 118 ++++++++++++++++++ 6 files changed, 284 insertions(+) create mode 100644 src/api/products_v2.rs create mode 100644 tests/fixtures/products-v2-create-201.json create mode 100644 tests/fixtures/products-v2-get-200.json create mode 100644 tests/fixtures/products-v2-update-200.json create mode 100644 tests/products_v2.rs diff --git a/src/api/mod.rs b/src/api/mod.rs index e66537f..5ac9f38 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -16,6 +16,7 @@ pub mod firmwares; pub mod organization_users; pub mod product_users; pub mod products; +pub mod products_v2; pub mod releases; pub mod signing_keys; @@ -44,6 +45,7 @@ pub use firmwares::FirmwaresApi; pub use organization_users::OrganizationUsersApi; pub use product_users::ProductUsersApi; pub use products::ProductApi; +pub use products_v2::ProductsV2Api; pub use releases::ReleasesApi; pub use reqwest::Body; pub use signing_keys::SigningKeysApi; @@ -355,6 +357,10 @@ impl Api { ProductApi(self) } + pub fn products_v2(&self) -> ProductsV2Api { + ProductsV2Api(self) + } + pub fn product_users(&self) -> ProductUsersApi { ProductUsersApi(self) } diff --git a/src/api/products_v2.rs b/src/api/products_v2.rs new file mode 100644 index 0000000..e78c7e8 --- /dev/null +++ b/src/api/products_v2.rs @@ -0,0 +1,133 @@ +use reqwest::Method; +use serde::{Deserialize, Serialize}; + +use crate::{json_body, Api}; + +use super::Error; +use snafu::ResultExt; + +#[derive(Debug, Deserialize, Serialize)] +pub struct ProductV2 { + pub inserted_at: String, + pub name: String, + pub organization_prn: String, + pub prn: String, + pub updated_at: String, +} + +#[derive(Debug, Serialize)] +pub struct CreateProductV2Params { + pub name: String, + pub organization_prn: String, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct CreateProductV2Response { + pub product: ProductV2, +} + +#[derive(Debug, Serialize)] +pub struct GetProductV2Params { + pub prn: String, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct GetProductV2Response { + pub product: ProductV2, +} + +#[derive(Debug, Serialize)] +pub struct ListProductsV2Params { + pub limit: Option, + pub order: Option, + pub search: String, + pub page: Option, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct ListProductsV2Response { + pub products: Vec, + pub next_page: Option, +} + +#[derive(Debug, Serialize)] +pub struct UpdateProductV2Params { + pub prn: String, + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default)] + pub name: Option, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct UpdateProductV2Response { + pub product: ProductV2, +} + +pub struct DeleteProductV2Params { + pub product_prn: String, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct DeleteProductV2Response {} + +pub struct ProductsV2Api<'a>(pub &'a Api); + +impl<'a> ProductsV2Api<'a> { + pub async fn create( + &'a self, + params: CreateProductV2Params, + ) -> Result, Error> { + self.0 + .execute(Method::POST, "/products", Some(json_body!(¶ms))) + .await + } + + pub async fn get( + &'a self, + params: GetProductV2Params, + ) -> Result, Error> { + let product_prn: String = params.prn; + self.0 + .execute(Method::GET, format!("/products/{product_prn}"), None) + .await + } + + pub async fn list( + &'a self, + params: ListProductsV2Params, + ) -> Result, Error> { + let search_string = params.search; + self.0 + .execute( + Method::GET, + format!("/products?search={search_string}"), + None, + ) + .await + } + + pub async fn update( + &'a self, + params: UpdateProductV2Params, + ) -> Result, Error> { + let product_prn: &String = ¶ms.prn; + + self.0 + .execute( + Method::PATCH, + format!("/products/{product_prn}"), + Some(json_body!(¶ms)), + ) + .await + } + + pub async fn delete( + &'a self, + params: DeleteProductV2Params, + ) -> Result, Error> { + let product_prn: String = params.product_prn; + self.0 + .execute(Method::DELETE, format!("/products/{product_prn}"), None) + .await + } +} diff --git a/tests/fixtures/products-v2-create-201.json b/tests/fixtures/products-v2-create-201.json new file mode 100644 index 0000000..1fbd2f0 --- /dev/null +++ b/tests/fixtures/products-v2-create-201.json @@ -0,0 +1,9 @@ +{ + "product": { + "inserted_at": "2000-01-01T00:00:00Z", + "name": "name", + "organization_prn": "organization_prn", + "prn": "prn", + "updated_at": "2000-01-01T00:00:00Z" + } +} diff --git a/tests/fixtures/products-v2-get-200.json b/tests/fixtures/products-v2-get-200.json new file mode 100644 index 0000000..1fbd2f0 --- /dev/null +++ b/tests/fixtures/products-v2-get-200.json @@ -0,0 +1,9 @@ +{ + "product": { + "inserted_at": "2000-01-01T00:00:00Z", + "name": "name", + "organization_prn": "organization_prn", + "prn": "prn", + "updated_at": "2000-01-01T00:00:00Z" + } +} diff --git a/tests/fixtures/products-v2-update-200.json b/tests/fixtures/products-v2-update-200.json new file mode 100644 index 0000000..1fbd2f0 --- /dev/null +++ b/tests/fixtures/products-v2-update-200.json @@ -0,0 +1,9 @@ +{ + "product": { + "inserted_at": "2000-01-01T00:00:00Z", + "name": "name", + "organization_prn": "organization_prn", + "prn": "prn", + "updated_at": "2000-01-01T00:00:00Z" + } +} diff --git a/tests/products_v2.rs b/tests/products_v2.rs new file mode 100644 index 0000000..0b1105f --- /dev/null +++ b/tests/products_v2.rs @@ -0,0 +1,118 @@ +mod common; + +use common::API_KEY; +use mockito::{mock, server_url as mock_server_url}; +use peridio_sdk::api::products_v2::CreateProductV2Params; +use peridio_sdk::api::products_v2::GetProductV2Params; +use peridio_sdk::api::products_v2::UpdateProductV2Params; +use peridio_sdk::api::Api; +use peridio_sdk::api::ApiOptions; + +#[tokio::test] +async fn create_product() { + let expected_name = "name"; + let expected_organization_prn = "organization_prn"; + + let api = Api::new(ApiOptions { + api_key: API_KEY.into(), + endpoint: Some(mock_server_url()), + ca_bundle_path: None, + }); + + let m = mock("POST", &*format!("/products")) + .with_status(201) + .with_header("content-type", "application/json") + .with_body_from_file("tests/fixtures/products-v2-create-201.json") + .create(); + + let params = CreateProductV2Params { + name: expected_name.to_string(), + organization_prn: expected_organization_prn.to_string(), + }; + + 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!(), + } + + m.assert(); +} + +#[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 { + api_key: API_KEY.into(), + endpoint: Some(mock_server_url()), + ca_bundle_path: None, + }); + + let m = mock("GET", &*format!("/products/{expected_prn}")) + .with_status(200) + .with_header("content-type", "application/json") + .with_body_from_file("tests/fixtures/products-v2-get-200.json") + .create(); + + let params = GetProductV2Params { + prn: expected_prn.to_string(), + }; + + 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!(), + } + + m.assert(); +} + +#[tokio::test] +async fn update_product() { + let expected_name = "name"; + let expected_organization_prn = "organization_prn"; + let expected_prn = "prn"; + + let api = Api::new(ApiOptions { + api_key: API_KEY.into(), + endpoint: Some(mock_server_url()), + ca_bundle_path: None, + }); + + let m = mock("PATCH", &*format!("/products/{expected_prn}")) + .with_status(200) + .with_header("content-type", "application/json") + .with_body_from_file("tests/fixtures/products-v2-update-200.json") + .create(); + + let params = UpdateProductV2Params { + prn: expected_prn.to_string(), + name: Some(expected_name.to_string()), + }; + + 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() + ); + } + _ => panic!(), + } + + m.assert(); +}