Skip to content

Commit

Permalink
Merge pull request #8 from peridio/spoff/eng-1145
Browse files Browse the repository at this point in the history
add products_v2
  • Loading branch information
danielspofford authored Oct 9, 2023
2 parents b9981c1 + 0a9a1bc commit 08de371
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -355,6 +357,10 @@ impl Api {
ProductApi(self)
}

pub fn products_v2(&self) -> ProductsV2Api {
ProductsV2Api(self)
}

pub fn product_users(&self) -> ProductUsersApi {
ProductUsersApi(self)
}
Expand Down
133 changes: 133 additions & 0 deletions src/api/products_v2.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
pub order: Option<String>,
pub search: String,
pub page: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ListProductsV2Response {
pub products: Vec<ProductV2>,
pub next_page: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct UpdateProductV2Params {
pub prn: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub name: Option<String>,
}

#[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<Option<CreateProductV2Response>, Error> {
self.0
.execute(Method::POST, "/products", Some(json_body!(&params)))
.await
}

pub async fn get(
&'a self,
params: GetProductV2Params,
) -> Result<Option<GetProductV2Response>, 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<Option<ListProductsV2Response>, 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<Option<UpdateProductV2Response>, Error> {
let product_prn: &String = &params.prn;

self.0
.execute(
Method::PATCH,
format!("/products/{product_prn}"),
Some(json_body!(&params)),
)
.await
}

pub async fn delete(
&'a self,
params: DeleteProductV2Params,
) -> Result<Option<DeleteProductV2Response>, Error> {
let product_prn: String = params.product_prn;
self.0
.execute(Method::DELETE, format!("/products/{product_prn}"), None)
.await
}
}
9 changes: 9 additions & 0 deletions tests/fixtures/products-v2-create-201.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 9 additions & 0 deletions tests/fixtures/products-v2-get-200.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 9 additions & 0 deletions tests/fixtures/products-v2-update-200.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
118 changes: 118 additions & 0 deletions tests/products_v2.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 08de371

Please sign in to comment.