-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from peridio/spoff/eng-1145
add products_v2
- Loading branch information
Showing
6 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(¶ms))) | ||
.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 = ¶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<Option<DeleteProductV2Response>, Error> { | ||
let product_prn: String = params.product_prn; | ||
self.0 | ||
.execute(Method::DELETE, format!("/products/{product_prn}"), None) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |