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
use serde::Deserialize;
use serde::Serialize;
use crate::mir::authorization::HttpAuthorization;
use crate::mir::implementation::validation::Validation;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct HttpEndpoint {
  pub name: String,
  pub description: String,
  pub path: String,
  pub auth: Option<HttpAuthorization>,
  pub method: HttpMethod,
  pub request: Option<Request>,
  pub response: Option<Response>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum HttpMethod {
  GET,
  POST,
  PUT,
  DELETE,
  PATCH,
  HEAD,
  OPTIONS,
  TRACE,
  CUSTOM(String),
}

impl Default for HttpMethod {
  fn default() -> Self {
    HttpMethod::GET
  }
}

impl HttpMethod {
  pub fn from(method: &str) -> Self {
    match method.to_lowercase().as_str() {
      "get" => HttpMethod::GET,
      "post" => HttpMethod::POST,
      "put" => HttpMethod::PUT,
      "delete" => HttpMethod::DELETE,
      "patch" => HttpMethod::PATCH,
      "head" => HttpMethod::HEAD,
      "options" => HttpMethod::OPTIONS,
      "trace" => HttpMethod::TRACE,
      _ => HttpMethod::CUSTOM(method.to_string()),
    }
  }
}

impl HttpEndpoint {
  pub fn new(name: String) -> Self {
    HttpEndpoint {
      name,
      ..Default::default()
    }
  }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct Request {
  pub name: String,
  pub pre_validate: Option<Validation>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct Response {
  pub name: String,
  pub post_validate: Option<Validation>,
}