Skip to main content

max / makenotwork

2.8 KB · 85 lines History Blame Raw
1 //! Custom domains: attach one to a creator, prove ownership, take it away.
2
3 use super::{MnwApiClient, empty_response, json_response};
4 use serde::{Deserialize, Serialize};
5
6 /// Custom domain info.
7 #[derive(Debug, Clone, Deserialize, Serialize)]
8 pub(crate) struct DomainInfo {
9 pub id: String,
10 pub domain: String,
11 pub verified: bool,
12 pub verification_token: String,
13 pub instructions: Option<String>,
14 }
15
16 /// Domain verification result.
17 #[derive(Debug, Deserialize)]
18 #[allow(dead_code)] // wire contract: every field the endpoint returns, read or not
19 pub(crate) struct DomainVerifyResult {
20 pub verified: bool,
21 pub message: String,
22 }
23
24 impl MnwApiClient {
25 pub(crate) async fn get_domain(&self, user_id: &str) -> anyhow::Result<Option<DomainInfo>> {
26 let url = format!("{}/api/internal/creator/domain", self.base_url);
27 let resp = self
28 .http
29 .get(&url)
30 .bearer_auth(&self.service_token)
31 .header("X-MNW-Actor", self.actor_header())
32 .query(&[("user_id", user_id)])
33 .send()
34 .await?;
35 let val: serde_json::Value = json_response(resp, "get_domain").await?;
36 if val.is_null() {
37 return Ok(None);
38 }
39 Ok(serde_json::from_value(val).ok())
40 }
41
42 pub(crate) async fn add_domain(
43 &self,
44 user_id: &str,
45 domain: &str,
46 ) -> anyhow::Result<DomainInfo> {
47 let url = format!("{}/api/internal/creator/domain", self.base_url);
48 let resp = self
49 .http
50 .post(&url)
51 .bearer_auth(&self.service_token)
52 .header("X-MNW-Actor", self.actor_header())
53 .json(&serde_json::json!({"user_id": user_id, "domain": domain}))
54 .send()
55 .await?;
56 json_response(resp, "add_domain").await
57 }
58
59 pub(crate) async fn verify_domain(&self, user_id: &str) -> anyhow::Result<DomainVerifyResult> {
60 let url = format!("{}/api/internal/creator/domain/verify", self.base_url);
61 let resp = self
62 .http
63 .post(&url)
64 .bearer_auth(&self.service_token)
65 .header("X-MNW-Actor", self.actor_header())
66 .query(&[("user_id", user_id)])
67 .send()
68 .await?;
69 json_response(resp, "verify_domain").await
70 }
71
72 pub(crate) async fn remove_domain(&self, user_id: &str) -> anyhow::Result<()> {
73 let url = format!("{}/api/internal/creator/domain", self.base_url);
74 let resp = self
75 .http
76 .delete(&url)
77 .bearer_auth(&self.service_token)
78 .header("X-MNW-Actor", self.actor_header())
79 .query(&[("user_id", user_id)])
80 .send()
81 .await?;
82 empty_response(resp, "remove_domain").await
83 }
84 }
85