freedom_api/api/post/
satellite.rs

1use reqwest::Response;
2use serde::Serialize;
3
4use crate::{api::Api, error::Error};
5
6#[derive(Debug, Clone, PartialEq, Serialize)]
7#[serde(rename_all = "camelCase")]
8pub struct Satellite {
9    name: String,
10    description: Option<String>,
11    norad_cat_id: u32,
12    configuration: String,
13}
14
15pub fn new<C>(client: &C) -> SatelliteBuilder<'_, C, NoName> {
16    SatelliteBuilder {
17        client,
18        state: NoName,
19    }
20}
21
22pub struct SatelliteBuilder<'a, C, S> {
23    pub(crate) client: &'a C,
24    state: S,
25}
26
27pub struct NoName;
28
29impl<'a, C> SatelliteBuilder<'a, C, NoName> {
30    pub fn name(self, name: impl Into<String>) -> SatelliteBuilder<'a, C, NoConfig> {
31        SatelliteBuilder {
32            client: self.client,
33            state: NoConfig { name: name.into() },
34        }
35    }
36}
37
38pub struct NoConfig {
39    name: String,
40}
41
42impl<'a, C> SatelliteBuilder<'a, C, NoConfig> {
43    pub fn satellite_configuration_url(
44        self,
45        url: impl Into<String>,
46    ) -> SatelliteBuilder<'a, C, NoNorad> {
47        SatelliteBuilder {
48            client: self.client,
49            state: NoNorad {
50                name: self.state.name,
51                configuration: url.into(),
52            },
53        }
54    }
55}
56
57impl<'a, C> SatelliteBuilder<'a, C, NoConfig>
58where
59    C: Api,
60{
61    pub fn satellite_configuration_id(
62        self,
63        id: impl Into<i32>,
64    ) -> SatelliteBuilder<'a, C, NoNorad> {
65        let configuration = self
66            .client
67            .path_to_url(format!("satellite_configurations/{}", id.into()))
68            .to_string();
69
70        self.satellite_configuration_url(configuration)
71    }
72}
73
74pub struct NoNorad {
75    name: String,
76    configuration: String,
77}
78
79impl<'a, C> SatelliteBuilder<'a, C, NoNorad> {
80    pub fn norad_id(self, norad_id: u32) -> SatelliteBuilder<'a, C, Satellite> {
81        let state = Satellite {
82            name: self.state.name,
83            description: None,
84            norad_cat_id: norad_id,
85            configuration: self.state.configuration,
86        };
87
88        SatelliteBuilder {
89            client: self.client,
90            state,
91        }
92    }
93}
94
95impl<C> SatelliteBuilder<'_, C, Satellite> {
96    pub fn description(mut self, description: impl Into<String>) -> Self {
97        self.state.description = Some(description.into());
98        self
99    }
100}
101
102impl<C> SatelliteBuilder<'_, C, Satellite>
103where
104    C: Api,
105{
106    pub async fn send(self) -> Result<Response, Error> {
107        let client = self.client;
108
109        let url = client.path_to_url("satellites");
110        client.post(url, self.state).await
111    }
112}