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
32impl EndpointSchema {
33    /// Creates a new `EndpointSchema` with the given name, method code, parameters and returns.
34    pub fn new(
35        name: impl Into<String>,
36        code: u32,
37        parameters: Vec<Field>,
38        returns: Vec<Field>,
39    ) -> Self {
40        Self {
41            name: name.into(),
42            code,
43            parameters,
44            returns,
45            stream_response: None,
46            description: "".to_string(),
47            json_schema: Default::default(),
48        }
49    }
50
51    /// Adds a stream response type field to the endpoint.
52    pub fn with_stream_response_type(mut self, stream_response: Type) -> Self {
53        self.stream_response = Some(stream_response);
54        self
55    }
56
57    /// Adds a description field to the endpoint.
58    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
59        self.description = desc.into();
60        self
61    }
62}