1use crate::imp::{core::*, prelude::*, request::Request, utils::Header};
2
3#[derive(Debug)]
4pub(crate) struct Route {
5 channel: ChannelOwner,
6 request: Weak<Request>
7}
8
9impl Route {
10 pub(crate) fn try_new(ctx: &Context, channel: ChannelOwner) -> Result<Self, Error> {
11 let Initializer { request } = serde_json::from_value(channel.initializer.clone())?;
12 let request = get_object!(ctx, &request.guid, Request)?;
13 Ok(Self { channel, request })
14 }
15
16 pub(crate) fn request(&self) -> Weak<Request> { self.request.clone() }
17
18 pub(crate) async fn abort(&self, err_code: Option<&str>) -> Result<(), Arc<Error>> {
19 let mut args = HashMap::new();
20 if let Some(x) = err_code {
21 args.insert("errCode", x);
22 }
23 let _ = send_message!(self, "abort", args);
24 Ok(())
25 }
26
27 pub(crate) async fn fulfill(&self, args: FulfillArgs<'_, '_>) -> ArcResult<()> {
28 let _ = send_message!(self, "fulfill", args);
29 Ok(())
30 }
31
32 pub(crate) async fn r#continue(&self, args: ContinueArgs<'_, '_, '_>) -> ArcResult<()> {
33 let _ = send_message!(self, "continue", args);
34 Ok(())
35 }
36}
37
38impl RemoteObject for Route {
39 fn channel(&self) -> &ChannelOwner { &self.channel }
40 fn channel_mut(&mut self) -> &mut ChannelOwner { &mut self.channel }
41}
42
43#[derive(Debug, Deserialize)]
44#[serde(rename_all = "camelCase")]
45struct Initializer {
46 request: OnlyGuid
47}
48
49#[skip_serializing_none]
50#[derive(Serialize)]
51#[serde(rename_all = "camelCase")]
52pub(crate) struct FulfillArgs<'a, 'b> {
53 body: &'a str,
54 is_base64: bool,
55 pub(crate) status: Option<i32>,
56 pub(crate) headers: Option<Vec<Header>>,
57 pub(crate) content_type: Option<&'b str>
58}
59
60impl<'a, 'b> FulfillArgs<'a, 'b> {
61 pub(crate) fn new(body: &'a str, is_base64: bool) -> Self {
62 Self {
63 body,
64 is_base64,
65 status: None,
66 headers: None,
67 content_type: None
68 }
69 }
70}
71
72#[skip_serializing_none]
73#[derive(Serialize, Default)]
74#[serde(rename_all = "camelCase")]
75pub(crate) struct ContinueArgs<'a, 'b, 'c> {
76 pub(crate) url: Option<&'a str>,
77 pub(crate) method: Option<&'b str>,
78 pub(crate) headers: Option<Vec<Header>>,
79 pub(crate) post_data: Option<&'c str>
80}