endpoint_libs/model/
endpoint.rs

1use crate::model::{Field, Type};
2use serde::*;
3
4/// `EndpointSchema` is a struct that represents a single endpoint in the API.
5#[derive(Debug, Serialize, Deserialize, Default)]
6pub struct EndpointSchema {
7    /// The name of the endpoint (e.g. `UserListSymbols`)
8    pub name: String,
9
10    /// The method code of the endpoint (e.g. `10020`)
11    pub code: u32,
12
13    /// A list of parameters that the endpoint accepts (e.g. "symbol" of type `String`)
14    pub parameters: Vec<Field>,
15
16    /// A list of fields that the endpoint returns
17    pub returns: Vec<Field>,
18
19    /// The type of the stream response (if any)
20    #[serde(default)]
21    pub stream_response: Option<Type>,
22
23    /// A description of the endpoint added by `with_description` method
24    #[serde(default)]
25    pub description: String,
26
27    /// The JSON schema of the endpoint (`Default::default()`)
28    #[serde(default)]
29    pub json_schema: serde_json::Value,
30
31    // Allowed roles for this endoint ["EnumRole::EnumVariant"]
32    pub roles: Vec<String>,
33}
34
35impl EndpointSchema {
36    /// Creates a new `EndpointSchema` with the given name, method code, parameters and returns.
37    pub fn new(
38        name: impl Into<String>,
39        code: u32,
40        parameters: Vec<Field>,
41        returns: Vec<Field>,
42    ) -> Self {
43        Self {
44            name: name.into(),
45            code,
46            parameters,
47            returns,
48            stream_response: None,
49            description: "".to_string(),
50            json_schema: Default::default(),
51            roles: Vec::new(),
52        }
53    }
54
55    /// Adds a stream response type field to the endpoint.
56    pub fn with_stream_response_type(mut self, stream_response: Type) -> Self {
57        self.stream_response = Some(stream_response);
58        self
59    }
60
61    /// Adds a description field to the endpoint.
62    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
63        self.description = desc.into();
64        self
65    }
66
67    /// Adds allowed roles to the endpoint.
68    pub fn with_roles(mut self, roles: Vec<String>) -> Self {
69        self.roles = roles;
70        self
71    }
72}