dinio_sdk/api/sinkhole_roles.rs
1//! `/api/v1/sinkhole-roles` (`../core/src/api/sinkhole_roles.ts`). Rules are read with
2//! their list ([`SinkholeLists::get`](crate::api::SinkholeLists::get) with
3//! `with_roles = true`); there is no list route. Access follows the list.
4
5use reqwest::Method;
6use uuid::Uuid;
7
8use crate::client::Client;
9use crate::error::Result;
10use crate::models::{NewSinkholeRole, SinkholeRole, SinkholeRolePatch};
11
12/// Sinkhole rule routes, from [`Client::sinkhole_roles`] (Core calls rules "roles").
13///
14/// Only the list's owner (or an Admin) may read or change rules, also for a shared list.
15/// To address rules by pattern instead of uuid, see
16/// [`ops::select_sinkhole_roles`](crate::ops::select_sinkhole_roles).
17pub struct SinkholeRoles<'a> {
18 client: &'a Client,
19}
20
21impl<'a> SinkholeRoles<'a> {
22 pub(crate) fn new(client: &'a Client) -> Self {
23 Self { client }
24 }
25
26 /// One rule.
27 ///
28 /// `GET /api/v1/sinkhole-roles/:uuid`
29 ///
30 /// # Errors
31 ///
32 /// * [`ErrorCode::NotFound`](crate::ErrorCode::NotFound) if there is no such rule.
33 /// * [`ErrorCode::Forbidden`](crate::ErrorCode::Forbidden) if its list belongs to
34 /// another user.
35 pub async fn get(&self, uuid: Uuid) -> Result<SinkholeRole> {
36 let request = self
37 .client
38 .request(Method::GET, &["sinkhole-roles", &uuid.to_string()])?;
39 Ok(self.client.send(request).await?.0)
40 }
41
42 /// Adds a rule to a list; Relay applies it to every linked client. Returns the rule
43 /// with the pattern as stored (names lowercased, no trailing dot).
44 ///
45 /// `POST /api/v1/sinkhole-roles`
46 ///
47 /// # Errors
48 ///
49 /// * [`ErrorCode::Validation`](crate::ErrorCode::Validation) if the pattern does not
50 /// fit the type.
51 /// * [`ErrorCode::NotFound`](crate::ErrorCode::NotFound) if the list does not exist.
52 /// * [`ErrorCode::Forbidden`](crate::ErrorCode::Forbidden) if it belongs to another
53 /// user.
54 pub async fn create(&self, role: &NewSinkholeRole) -> Result<SinkholeRole> {
55 let request = self
56 .client
57 .request(Method::POST, &["sinkhole-roles"])?
58 .json(role);
59 Ok(self.client.send(request).await?.0)
60 }
61
62 /// Changes a rule's type and/or pattern; fields left `None` stay as they are.
63 ///
64 /// `PATCH /api/v1/sinkhole-roles/:uuid`
65 ///
66 /// # Errors
67 ///
68 /// * [`ErrorCode::NotFound`](crate::ErrorCode::NotFound) if there is no such rule.
69 /// * [`ErrorCode::Forbidden`](crate::ErrorCode::Forbidden) if its list belongs to
70 /// another user.
71 /// * [`ErrorCode::Validation`](crate::ErrorCode::Validation) if the result would not
72 /// fit together (e.g. type `Wildcard` with a pattern without `*.`).
73 pub async fn update(&self, uuid: Uuid, patch: &SinkholeRolePatch) -> Result<SinkholeRole> {
74 let request = self
75 .client
76 .request(Method::PATCH, &["sinkhole-roles", &uuid.to_string()])?
77 .json(patch);
78 Ok(self.client.send(request).await?.0)
79 }
80
81 /// Removes a rule from its list.
82 ///
83 /// `DELETE /api/v1/sinkhole-roles/:uuid`
84 ///
85 /// # Errors
86 ///
87 /// * [`ErrorCode::NotFound`](crate::ErrorCode::NotFound) if there is no such rule.
88 /// * [`ErrorCode::Forbidden`](crate::ErrorCode::Forbidden) if its list belongs to
89 /// another user.
90 pub async fn delete(&self, uuid: Uuid) -> Result<()> {
91 let request = self
92 .client
93 .request(Method::DELETE, &["sinkhole-roles", &uuid.to_string()])?;
94 self.client.send_empty(request).await
95 }
96}