Skip to content

Commit 0f9c119

Browse files
committed
add cost interface and unit test
1 parent 6791340 commit 0f9c119

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

src/interface/cost.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! # Cost
2+
//!
3+
//! `interface::cost` consists of functions for interfacing with the Ruddr Cost period endpoints.
4+
use crate::client::client;
5+
use crate::model::{cost, types};
6+
7+
/// Retrieves a specific Ruddr Cost period object by id, and deserializes it to the corresponding model struct.
8+
/// https://ruddr.readme.io/reference/get-a-cost-period
9+
/// ```ignore
10+
/// let cost = cost(&client, types::UUID::from("b3a100b0-8e71-4f39-9d96-32f11838aa8c")).await?;
11+
/// ```
12+
pub async fn cost(
13+
client: &client::Client,
14+
id: types::UUID,
15+
) -> Result<cost::Cost, Box<dyn std::error::Error>> {
16+
// retrieve cost target period
17+
Ok(client
18+
.read::<cost::Cost>(&format!("cost-periods/{id}"), "")
19+
.await?)
20+
}
21+
22+
/// Retrieves all Ruddr Cost period objects by filters, and deserializes it to the corresponding vector of model structs.
23+
/// https://ruddr.readme.io/reference/list-cost-periods
24+
/// ```ignore
25+
/// let costs = costs(
26+
/// &client,
27+
/// Some(types::UUID::from("ec5543de-3b0f-47a0-b8ef-a6e18dc4b885")),
28+
/// ).await?;
29+
/// ```
30+
pub async fn costs(
31+
client: &client::Client,
32+
member_id: Option<types::UUID>,
33+
) -> Result<cost::Costs, Box<dyn std::error::Error>> {
34+
// initialize params
35+
let mut params = String::from("limit=100");
36+
37+
// optional parameters for LIST
38+
if member_id.is_some() {
39+
params = format!("{params}&memberId={}", member_id.unwrap())
40+
}
41+
42+
// retrieve clients
43+
Ok(client.read::<cost::Costs>("cost-periods", &params).await?)
44+
}
45+
46+
#[cfg(test)]
47+
mod tests;

src/interface/cost/tests.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use super::*;
2+
3+
#[test]
4+
fn test_cost() {
5+
let test = async {
6+
let client = client::Client::new(Some("abcdefghi123456789"))
7+
.expect("client with token could not be constructed");
8+
assert_eq!(
9+
cost(
10+
&client,
11+
types::UUID::from("b3a100b0-8e71-4f39-9d96-32f11838aa8c")
12+
)
13+
.await
14+
.unwrap_err()
15+
.to_string(),
16+
"client read response failed",
17+
"cost retrieval did not fail on auth",
18+
)
19+
};
20+
let rt = tokio::runtime::Runtime::new().unwrap();
21+
rt.block_on(test);
22+
}
23+
24+
#[test]
25+
fn test_costs() {
26+
let test = async {
27+
let client = client::Client::new(Some("abcdefghi123456789"))
28+
.expect("client with token could not be constructed");
29+
assert_eq!(
30+
costs(
31+
&client,
32+
Some(types::UUID::from("ec5543de-3b0f-47a0-b8ef-a6e18dc4b885")),
33+
)
34+
.await
35+
.unwrap_err()
36+
.to_string(),
37+
"client read response failed",
38+
"costs retrieval did not fail on auth",
39+
)
40+
};
41+
let rt = tokio::runtime::Runtime::new().unwrap();
42+
rt.block_on(test);
43+
}

src/interface/utilization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub async fn utilization(
2020
}
2121

2222
/// Retrieves all Ruddr Utilization target period objects by filters, and deserializes it to the corresponding vector of model structs.
23-
/// https://ruddr.readme.io/reference/list-utilizations
23+
/// https://ruddr.readme.io/reference/list-utilization-target-periods
2424
/// ```ignore
2525
/// let utilizations = utilizations(
2626
/// &client,

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod client {
44
}
55
pub mod interface {
66
pub mod allocation;
7+
pub mod cost;
78
pub mod customer;
89
pub mod expense_item;
910
pub mod expense_report;

0 commit comments

Comments
 (0)