reactive_graph_remotes_model/
instance_address.rs

1use serde::Deserialize;
2use serde::Serialize;
3use std::hash::Hash;
4use std::hash::Hasher;
5use typed_builder::TypedBuilder;
6
7pub const DEFAULT_HOSTNAME: &str = "localhost";
8pub const DEFAULT_PORT: u16 = 31415;
9pub const DEFAULT_USER_AGENT: &str = "reactive_graph_client";
10pub const DEFAULT_ENDPOINT_GRAPHQL: &str = "/graphql";
11pub const DEFAULT_ENDPOINT_DYNAMIC_GRAPH: &str = "/dynamic_graph";
12pub const DEFAULT_ENDPOINT_RUNTIME: &str = "/runtime/graphql";
13pub const DEFAULT_ENDPOINT_PLUGIN: &str = "/plugin/graphql";
14
15#[derive(Clone, Debug, Eq, Deserialize, Serialize, TypedBuilder)]
16#[serde(rename_all = "camelCase")]
17pub struct InstanceAddress {
18    /// The hostname of the GraphQL server.
19    #[builder(default = DEFAULT_HOSTNAME.to_owned())]
20    pub hostname: String,
21
22    /// The port of the GraphQL server.
23    #[builder(default = DEFAULT_PORT)]
24    #[serde(default = "default_port", skip_serializing_if = "is_default_port")]
25    pub port: u16,
26
27    /// If true, HTTPS will be used.
28    #[builder(default = false)]
29    #[serde(default = "bool::default", skip_serializing_if = "is_default")]
30    pub secure: bool,
31
32    /// The user agent.
33    #[builder(default = DEFAULT_USER_AGENT.to_owned())]
34    #[serde(default = "default_user_agent", skip_serializing_if = "is_default_user_agent")]
35    pub user_agent: String,
36
37    /// The relative URL of the GraphQL endpoint, by default "/graphql".
38    #[builder(default = DEFAULT_ENDPOINT_GRAPHQL.to_owned())]
39    #[serde(default = "default_endpoint_graphql", skip_serializing_if = "is_default_endpoint_graphql")]
40    pub endpoint_graphql: String,
41
42    /// The relative URL of the GraphQL endpoint, by default "/graphql".
43    #[builder(default = DEFAULT_ENDPOINT_DYNAMIC_GRAPH.to_owned())]
44    #[serde(default = "default_endpoint_dynamic_graph", skip_serializing_if = "is_default_endpoint_dynamic_graph")]
45    pub endpoint_dynamic_graph: String,
46
47    /// The relative URL of the GraphQL endpoint, by default "/graphql".
48    #[builder(default = DEFAULT_ENDPOINT_RUNTIME.to_owned())]
49    #[serde(default = "default_endpoint_runtime", skip_serializing_if = "is_default_endpoint_runtime")]
50    pub endpoint_runtime: String,
51
52    /// The relative URL of the GraphQL endpoint, by default "/graphql".
53    #[builder(default = DEFAULT_ENDPOINT_PLUGIN.to_owned())]
54    #[serde(default = "default_endpoint_plugin", skip_serializing_if = "is_default_endpoint_plugin")]
55    pub endpoint_plugin: String,
56
57    /// The authentication token.
58    #[builder(default)]
59    pub bearer: Option<String>,
60}
61
62impl InstanceAddress {
63    pub fn new(hostname: String, port: u16, secure: bool) -> InstanceAddress {
64        InstanceAddress {
65            hostname,
66            port,
67            secure,
68            user_agent: DEFAULT_USER_AGENT.to_string(),
69            endpoint_graphql: DEFAULT_ENDPOINT_GRAPHQL.to_string(),
70            endpoint_dynamic_graph: DEFAULT_ENDPOINT_DYNAMIC_GRAPH.to_string(),
71            endpoint_runtime: DEFAULT_ENDPOINT_RUNTIME.to_string(),
72            endpoint_plugin: DEFAULT_ENDPOINT_PLUGIN.to_string(),
73            bearer: None,
74        }
75    }
76
77    pub fn protocol(&self) -> String {
78        if self.secure { "https".to_string() } else { "http".to_string() }
79    }
80
81    pub fn base_url(&self) -> String {
82        format!("{}://{}:{}", self.protocol(), self.hostname, self.port)
83    }
84
85    pub fn url_reactive_graph(&self) -> String {
86        format!("{}{}", self.base_url(), self.endpoint_graphql)
87    }
88
89    pub fn url_dynamic_graph(&self) -> String {
90        format!("{}{}", self.base_url(), self.endpoint_dynamic_graph)
91    }
92
93    pub fn url_reactive_graph_runtime(&self) -> String {
94        format!("{}{}", self.base_url(), self.endpoint_runtime)
95    }
96
97    pub fn url_reactive_graph_plugins(&self) -> String {
98        format!("{}{}", self.base_url(), self.endpoint_plugin)
99    }
100}
101
102// An InstanceAddress is equals if hostname, port and secure are equal
103impl PartialEq<InstanceAddress> for InstanceAddress {
104    fn eq(&self, other: &InstanceAddress) -> bool {
105        self.hostname == other.hostname && self.port == other.port && self.secure == other.secure
106    }
107}
108
109impl Hash for InstanceAddress {
110    fn hash<H: Hasher>(&self, state: &mut H) {
111        self.hostname.hash(state);
112        self.port.hash(state);
113        self.secure.hash(state);
114    }
115}
116
117impl Default for InstanceAddress {
118    fn default() -> Self {
119        InstanceAddress {
120            hostname: DEFAULT_HOSTNAME.to_string(),
121            port: DEFAULT_PORT,
122            secure: false,
123            user_agent: DEFAULT_USER_AGENT.to_string(),
124            endpoint_graphql: DEFAULT_ENDPOINT_GRAPHQL.to_string(),
125            endpoint_dynamic_graph: DEFAULT_ENDPOINT_DYNAMIC_GRAPH.to_string(),
126            endpoint_runtime: DEFAULT_ENDPOINT_RUNTIME.to_string(),
127            endpoint_plugin: DEFAULT_ENDPOINT_PLUGIN.to_string(),
128            bearer: None,
129        }
130    }
131}
132
133fn default_port() -> u16 {
134    DEFAULT_PORT
135}
136
137fn is_default_port(port: &u16) -> bool {
138    DEFAULT_PORT == *port
139}
140
141fn default_user_agent() -> String {
142    DEFAULT_USER_AGENT.to_owned()
143}
144
145fn is_default_user_agent(user_agent: &String) -> bool {
146    DEFAULT_USER_AGENT == user_agent
147}
148
149fn default_endpoint_graphql() -> String {
150    DEFAULT_ENDPOINT_GRAPHQL.to_owned()
151}
152
153fn is_default_endpoint_graphql(endpoint: &String) -> bool {
154    DEFAULT_ENDPOINT_GRAPHQL == endpoint
155}
156
157fn default_endpoint_dynamic_graph() -> String {
158    DEFAULT_ENDPOINT_DYNAMIC_GRAPH.to_owned()
159}
160
161fn is_default_endpoint_dynamic_graph(endpoint: &String) -> bool {
162    DEFAULT_ENDPOINT_DYNAMIC_GRAPH == endpoint
163}
164
165fn default_endpoint_runtime() -> String {
166    DEFAULT_ENDPOINT_RUNTIME.to_owned()
167}
168
169fn is_default_endpoint_runtime(endpoint: &String) -> bool {
170    DEFAULT_ENDPOINT_RUNTIME == endpoint
171}
172
173fn default_endpoint_plugin() -> String {
174    DEFAULT_ENDPOINT_PLUGIN.to_owned()
175}
176
177fn is_default_endpoint_plugin(endpoint: &String) -> bool {
178    DEFAULT_ENDPOINT_PLUGIN == endpoint
179}
180
181fn is_default<T: Default + PartialEq>(t: &T) -> bool {
182    t == &T::default()
183}