Skip to content

Commit

Permalink
use Borrow trait to allow passing values as well
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Dec 2, 2023
1 parent cc0ee04 commit ccf5e81
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 58 deletions.
17 changes: 12 additions & 5 deletions src/alert_group_setting.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use http::Method;
use serde_derive::{Deserialize, Serialize};
use std::borrow::Borrow;
use typed_builder::TypedBuilder;

use crate::client::*;
Expand Down Expand Up @@ -134,13 +135,13 @@ impl Client {
/// See <https://mackerel.io/api-docs/entry/alert-group-settings#create>.
pub async fn create_alert_group_setting(
&self,
alert_group_setting_value: &AlertGroupSettingValue,
alert_group_setting_value: impl Borrow<AlertGroupSettingValue>,
) -> Result<AlertGroupSetting> {
self.request(
Method::POST,
"/api/v0/alert-group-settings",
query_params![],
request_body!(alert_group_setting_value),
request_body!(alert_group_setting_value.borrow()),
response_body!(..),
)
.await
Expand Down Expand Up @@ -169,13 +170,13 @@ impl Client {
pub async fn update_alert_group_setting(
&self,
alert_group_setting_id: impl Into<AlertGroupSettingId>,
alert_group_setting_value: &AlertGroupSettingValue,
alert_group_setting_value: impl Borrow<AlertGroupSettingValue>,
) -> Result<AlertGroupSetting> {
self.request(
Method::PUT,
format_url!("/api/v0/alert-group-settings/{}", alert_group_setting_id),
query_params![],
request_body!(alert_group_setting_value),
request_body!(alert_group_setting_value.borrow()),
response_body!(..),
)
.await
Expand Down Expand Up @@ -264,6 +265,12 @@ mod client_tests {
request = value_json_example(),
response = entity_json_example(),
};
assert_eq!(
test_client!(server)
.create_alert_group_setting(value_example())
.await,
Ok(entity_example()),
);
assert_eq!(
test_client!(server)
.create_alert_group_setting(&value_example())
Expand Down Expand Up @@ -303,7 +310,7 @@ mod client_tests {
};
assert_eq!(
test_client!(server)
.update_alert_group_setting("setting0", &value_example())
.update_alert_group_setting("setting0", value_example())
.await,
Ok(entity_example()),
);
Expand Down
17 changes: 12 additions & 5 deletions src/aws_integration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use http::Method;
use serde_derive::{Deserialize, Serialize};
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::borrow::Borrow;
use std::collections::HashMap;
use strum::{Display, EnumString};
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -288,13 +289,13 @@ impl Client {
/// See <https://mackerel.io/api-docs/entry/aws-integration#create>.
pub async fn create_aws_integration(
&self,
aws_integration_value: &AWSIntegrationValue,
aws_integration_value: impl Borrow<AWSIntegrationValue>,
) -> Result<AWSIntegration> {
self.request(
Method::POST,
"/api/v0/aws-integrations",
query_params![],
request_body!(aws_integration_value),
request_body!(aws_integration_value.borrow()),
response_body!(..),
)
.await
Expand Down Expand Up @@ -323,13 +324,13 @@ impl Client {
pub async fn update_aws_integration(
&self,
aws_integration_id: impl Into<AWSIntegrationId>,
aws_integration_value: &AWSIntegrationValue,
aws_integration_value: impl Borrow<AWSIntegrationValue>,
) -> Result<AWSIntegration> {
self.request(
Method::PUT,
format_url!("/api/v0/aws-integrations/{}", aws_integration_id),
query_params![],
request_body!(aws_integration_value),
request_body!(aws_integration_value.borrow()),
response_body!(..),
)
.await
Expand Down Expand Up @@ -462,6 +463,12 @@ mod client_tests {
request = value_json_example(),
response = entity_json_example(),
};
assert_eq!(
test_client!(server)
.create_aws_integration(value_example())
.await,
Ok(entity_example()),
);
assert_eq!(
test_client!(server)
.create_aws_integration(&value_example())
Expand Down Expand Up @@ -499,7 +506,7 @@ mod client_tests {
};
assert_eq!(
test_client!(server)
.update_aws_integration("aws0", &value_example())
.update_aws_integration("aws0", value_example())
.await,
Ok(entity_example()),
);
Expand Down
12 changes: 10 additions & 2 deletions src/channel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use http::Method;
use serde_derive::{Deserialize, Serialize};
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::borrow::Borrow;
use std::collections::HashMap;
use strum::{Display, EnumString};

Expand Down Expand Up @@ -433,12 +434,15 @@ impl Client {
/// Creates a new channel.
///
/// See <https://mackerel.io/api-docs/entry/channels#create>.
pub async fn create_channel(&self, channel_value: &ChannelValue) -> Result<Channel> {
pub async fn create_channel(
&self,
channel_value: impl Borrow<ChannelValue>,
) -> Result<Channel> {
self.request(
Method::POST,
"/api/v0/channels",
query_params![],
request_body!(channel_value),
request_body!(channel_value.borrow()),
response_body!(..),
)
.await
Expand Down Expand Up @@ -521,6 +525,10 @@ mod client_tests {
request = value_json_example(),
response = entity_json_example(),
};
assert_eq!(
test_client!(server).create_channel(value_example()).await,
Ok(entity_example()),
);
assert_eq!(
test_client!(server).create_channel(&value_example()).await,
Ok(entity_example()),
Expand Down
18 changes: 13 additions & 5 deletions src/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use chrono::{DateTime, Duration, Utc};
use http::Method;
use serde_derive::{Deserialize, Serialize};
use serde_with::{skip_serializing_none, DurationSeconds};
use std::borrow::Borrow;
use typed_builder::TypedBuilder;

use crate::client::*;
Expand Down Expand Up @@ -415,12 +416,15 @@ impl Client {
/// Creates a new dashboard.
///
/// See <https://mackerel.io/api-docs/entry/dashboards#create>.
pub async fn create_dashboard(&self, dashboard_value: &DashboardValue) -> Result<Dashboard> {
pub async fn create_dashboard(
&self,
dashboard_value: impl Borrow<DashboardValue>,
) -> Result<Dashboard> {
self.request(
Method::POST,
"/api/v0/dashboards",
query_params![],
request_body!(dashboard_value),
request_body!(dashboard_value.borrow()),
response_body!(..),
)
.await
Expand All @@ -446,13 +450,13 @@ impl Client {
pub async fn update_dashboard(
&self,
dashboard_id: impl Into<DashboardId>,
dashboard_value: &DashboardValue,
dashboard_value: impl Borrow<DashboardValue>,
) -> Result<Dashboard> {
self.request(
Method::PUT,
format_url!("/api/v0/dashboards/{}", dashboard_id),
query_params![],
request_body!(dashboard_value),
request_body!(dashboard_value.borrow()),
response_body!(..),
)
.await
Expand Down Expand Up @@ -592,6 +596,10 @@ mod client_tests {
request = value_json_example(),
response = entity_json_example(),
};
assert_eq!(
test_client!(server).create_dashboard(value_example()).await,
Ok(entity_example()),
);
assert_eq!(
test_client!(server)
.create_dashboard(&value_example())
Expand Down Expand Up @@ -629,7 +637,7 @@ mod client_tests {
};
assert_eq!(
test_client!(server)
.update_dashboard("dashboard0", &value_example())
.update_dashboard("dashboard0", value_example())
.await,
Ok(entity_example()),
);
Expand Down
18 changes: 13 additions & 5 deletions src/downtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use chrono::{DateTime, Utc};
use http::Method;
use serde_derive::{Deserialize, Serialize};
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::borrow::Borrow;
use strum::{Display, EnumString};
use typed_builder::TypedBuilder;

Expand Down Expand Up @@ -288,12 +289,15 @@ impl Client {
/// Creates a new downtime.
///
/// See <https://mackerel.io/api-docs/entry/downtimes#create>.
pub async fn create_downtime(&self, downtime_value: &DowntimeValue) -> Result<Downtime> {
pub async fn create_downtime(
&self,
downtime_value: impl Borrow<DowntimeValue>,
) -> Result<Downtime> {
self.request(
Method::POST,
"/api/v0/downtimes",
query_params![],
request_body!(downtime_value),
request_body!(downtime_value.borrow()),
response_body!(..),
)
.await
Expand All @@ -305,13 +309,13 @@ impl Client {
pub async fn update_downtime(
&self,
downtime_id: impl Into<DowntimeId>,
downtime_value: &DowntimeValue,
downtime_value: impl Borrow<DowntimeValue>,
) -> Result<Downtime> {
self.request(
Method::PUT,
format_url!("/api/v0/downtimes/{}", downtime_id),
query_params![],
request_body!(downtime_value),
request_body!(downtime_value.borrow()),
response_body!(..),
)
.await
Expand Down Expand Up @@ -399,6 +403,10 @@ mod client_tests {
request = value_json_example(),
response = entity_json_example(),
};
assert_eq!(
test_client!(server).create_downtime(value_example()).await,
Ok(entity_example()),
);
assert_eq!(
test_client!(server).create_downtime(&value_example()).await,
Ok(entity_example()),
Expand All @@ -415,7 +423,7 @@ mod client_tests {
};
assert_eq!(
test_client!(server)
.update_downtime("downtime0", &value_example())
.update_downtime("downtime0", value_example())
.await,
Ok(entity_example()),
);
Expand Down
17 changes: 12 additions & 5 deletions src/graph_annotation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use chrono::{DateTime, Utc};
use http::Method;
use serde_derive::{Deserialize, Serialize};
use std::borrow::Borrow;
use typed_builder::TypedBuilder;

use crate::client::*;
Expand Down Expand Up @@ -136,13 +137,13 @@ impl Client {
/// See <https://mackerel.io/api-docs/entry/graph-annotations#create>.
pub async fn create_graph_annotation(
&self,
graph_annotation_value: &GraphAnnotationValue,
graph_annotation_value: impl Borrow<GraphAnnotationValue>,
) -> Result<GraphAnnotation> {
self.request(
Method::POST,
"/api/v0/graph-annotations",
query_params![],
request_body!(graph_annotation_value),
request_body!(graph_annotation_value.borrow()),
response_body!(..),
)
.await
Expand All @@ -154,13 +155,13 @@ impl Client {
pub async fn update_graph_annotation(
&self,
graph_annontation_id: impl Into<GraphAnnotationId>,
graph_annotation_value: &GraphAnnotationValue,
graph_annotation_value: impl Borrow<GraphAnnotationValue>,
) -> Result<GraphAnnotation> {
self.request(
Method::PUT,
format_url!("/api/v0/graph-annotations/{}", graph_annontation_id),
query_params![],
request_body!(graph_annotation_value),
request_body!(graph_annotation_value.borrow()),
response_body!(..),
)
.await
Expand Down Expand Up @@ -267,6 +268,12 @@ mod client_tests {
request = value_json_example(),
response = entity_json_example(),
};
assert_eq!(
test_client!(server)
.create_graph_annotation(value_example())
.await,
Ok(entity_example()),
);
assert_eq!(
test_client!(server)
.create_graph_annotation(&value_example())
Expand All @@ -285,7 +292,7 @@ mod client_tests {
};
assert_eq!(
test_client!(server)
.update_graph_annotation("annotation0", &value_example())
.update_graph_annotation("annotation0", value_example())
.await,
Ok(entity_example()),
);
Expand Down
Loading

0 comments on commit ccf5e81

Please sign in to comment.