relay_actions/actions/invitations/
single.rs1use serde::{Deserialize, Serialize};
2
3use super::InvitationView;
4use crate::{
5 actions::Action, cache::CacheManager, request::get_signed, sinks::progress::ProgressSink,
6 storage::Storage,
7};
8use relay_lib::prelude::Address;
9
10#[derive(Serialize)]
11struct SingleReq {
12 pub code: String,
13}
14
15#[derive(Deserialize)]
16struct SingleResp {
17 pub invitation: InvitationView,
18}
19
20pub struct GetSingle {
21 pub address: Address,
22 pub code: String,
23}
24
25impl Action for GetSingle {
26 type Output = InvitationView;
27
28 fn execute(
29 &self,
30 storage: &mut Storage,
31 cache: &mut CacheManager,
32 progress: &mut dyn ProgressSink,
33 ) -> Self::Output {
34 let req = SingleReq {
35 code: self.code.clone(),
36 };
37
38 progress.step("Fetching invitation", "Fetched invitation");
39 let resp = get_signed(
40 progress,
41 storage,
42 cache,
43 &self.address,
44 "/invitations/single",
45 req,
46 )
47 .unwrap_or_else(|e| {
48 progress.error(&format!("{:#?}", e));
49 progress.abort("Failed to send list request");
50 });
51
52 if !resp.status().is_success() {
53 progress.abort(&format!(
54 "Failed to fetch invitation. HTTP {}",
55 resp.status().as_u16()
56 ));
57 }
58
59 let parsed = resp.json::<SingleResp>().unwrap_or_else(|e| {
60 progress.error(&format!("{:#?}", e));
61 progress.abort("Failed to parse single invitation response");
62 });
63
64 progress.done();
65 parsed.invitation
66 }
67}