1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::{
    collections::HashMap,
    fmt::{Display, Formatter},
    str::FromStr,
};

use super::Api;
use crate::errors::ClockworkError;

use anchor_lang::{prelude::*, AnchorDeserialize};

pub const SEED_REQUEST: &[u8] = b"request";

/**
 * Request
 */

#[account]
#[derive(Debug)]
pub struct Request {
    pub api: Pubkey,
    pub caller: Pubkey,
    pub created_at: u64,
    pub fee_amount: u64,
    pub headers: HashMap<String, String>,
    pub id: String,
    pub method: HttpMethod,
    pub route: String,
    pub url: String,
    pub workers: Vec<Pubkey>,
}

impl Request {
    pub fn pubkey(api: Pubkey, caller: Pubkey, id: String) -> Pubkey {
        Pubkey::find_program_address(
            &[SEED_REQUEST, api.as_ref(), caller.as_ref(), id.as_bytes()],
            &crate::ID,
        )
        .0
    }
}

/**
 * RequestAccount
 */

pub trait RequestAccount {
    fn pubkey(&self) -> Pubkey;

    fn init(
        &mut self,
        api: &Account<Api>,
        caller: Pubkey,
        created_at: u64,
        fee_amount: u64,
        headers: HashMap<String, String>,
        id: String,
        method: HttpMethod,
        route: String,
        workers: Vec<Pubkey>,
    ) -> Result<()>;
}

impl RequestAccount for Account<'_, Request> {
    fn pubkey(&self) -> Pubkey {
        Request::pubkey(self.api, self.caller, self.id.clone())
    }

    fn init(
        &mut self,
        api: &Account<Api>,
        caller: Pubkey,
        created_at: u64,
        fee_amount: u64,
        headers: HashMap<String, String>,
        id: String,
        method: HttpMethod,
        route: String,
        workers: Vec<Pubkey>,
    ) -> Result<()> {
        self.api = api.key();
        self.caller = caller;
        self.created_at = created_at;
        self.fee_amount = fee_amount;
        self.headers = headers;
        self.id = id;
        self.method = method;
        self.route = route.clone();
        self.url = api.clone().base_url.to_owned() + route.as_str();
        self.workers = workers;
        Ok(())
    }
}

/**
 * HttpMethod
 */

#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug, PartialEq)]
pub enum HttpMethod {
    Get,
    Post,
}

impl Display for HttpMethod {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        match *self {
            HttpMethod::Get => write!(f, "GET"),
            HttpMethod::Post => write!(f, "POST"),
        }
    }
}

impl FromStr for HttpMethod {
    type Err = Error;

    fn from_str(input: &str) -> std::result::Result<HttpMethod, Self::Err> {
        match input.to_uppercase().as_str() {
            "GET" => Ok(HttpMethod::Get),
            "POST" => Ok(HttpMethod::Post),
            _ => Err(ClockworkError::InvalidHttpMethod.into()),
        }
    }
}