-
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 #10 from fussybeaver/ND-include-parameter
User example and include parameter support
- Loading branch information
Showing
9 changed files
with
272 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ src/endpoints/*_swagger.rs | |
/*.json | ||
/templates/api.mustache | ||
/Cargo.lock | ||
/pagerduty-models-codegen.iml |
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
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,82 @@ | ||
//! This example will fetch all users and check their notification rules | ||
|
||
use futures_util::TryStreamExt; | ||
use praiya::Praiya; | ||
|
||
use praiya::api::users::ListUsersParamsBuilder; | ||
use praiya::models::{ContactMethod, ContactMethodTypeEnum, NotificationRule, User, UserRoleEnum}; | ||
use praiya::ParamsBuilder; | ||
|
||
// Replace with appropriate values... | ||
const PAGERDUTY_TOKEN: &str = ""; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), praiya::errors::Error> { | ||
let pagerduty = Praiya::new(PAGERDUTY_TOKEN); | ||
|
||
let params = { | ||
let mut option = ListUsersParamsBuilder::new(); | ||
let include = vec!["notification_rules"]; | ||
|
||
option.include(include); | ||
option.build() | ||
}; | ||
|
||
let users: Vec<User> = pagerduty | ||
.users() | ||
.list_users(params) | ||
.try_collect() | ||
.await | ||
.expect("failed to fetch users"); | ||
|
||
let default_contact_types = vec![ | ||
ContactMethodTypeEnum::EMAIL_CONTACT_METHOD, | ||
ContactMethodTypeEnum::SMS_CONTACT_METHOD, | ||
ContactMethodTypeEnum::PHONE_CONTACT_METHOD, | ||
]; | ||
|
||
for user in &users { | ||
match user { | ||
User { | ||
role: Some(role), | ||
notification_rules: Some(rules), | ||
summary: Some(summary), | ||
.. | ||
} if !vec![ | ||
UserRoleEnum::READ_ONLY_USER, | ||
UserRoleEnum::READ_ONLY_LIMITED_USER, | ||
] | ||
.iter() | ||
.any(|r| r == role) | ||
&& default_contact_types.iter().all(|c| { | ||
rules.iter().any(|r| match r { | ||
NotificationRule { | ||
contact_method: Some(ContactMethod { _type, .. }), | ||
.. | ||
} if c == _type => true, | ||
_ => false, | ||
}) | ||
}) => | ||
{ | ||
println!("User is valid: {}", &summary); | ||
} | ||
User { | ||
summary: Some(summary), | ||
role: Some(role), | ||
notification_rules: Some(notification_rules), | ||
.. | ||
} => println!( | ||
"User is not a responder, or missing notification rules: {:?}, {:?}, {:?}", | ||
summary, | ||
role, | ||
notification_rules | ||
.iter() | ||
.map(|n| n.contact_method.as_ref().unwrap()._type.as_ref()) | ||
.collect::<Vec<&str>>() | ||
), | ||
_ => println!("Failed to parse user {:?}", user), | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
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
Oops, something went wrong.