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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use chrono::{DateTime, Utc};
use reqwest::{Client, RequestBuilder};
use serde::{Deserialize, Serialize};
use std::error::Error;

/// Client to upload personal weather station observations to windy.com
#[derive(Clone)]
pub struct WindyStation {
    /// User's API key.
    api_key: String,

    /// Request client.
    client: Client,

    /// Base url.
    base_url: String,
}

impl WindyStation {
    /// Creates a new API instance with the default client.
    pub fn new(api_key: String) -> Self {
        Self::with_client(api_key, Client::new())
    }

    /// Creates a new API instance with the specified client and API key.
    pub fn with_client(api_key: String, client: Client) -> Self {
        WindyStation {
            api_key,
            client,
            base_url: "https://stations.windy.com/pws/update".to_string(),
        }
    }

    /// Register the specified stations.
    pub async fn register_stations(&self, stations: &[Station]) -> Result<(), Box<dyn Error>> {
        #[derive(Serialize)]
        struct RegisterStationsRequest<'a> {
            stations: &'a [Station],
        }

        let request = RegisterStationsRequest { stations };

        self.post_request_builder()
            .json(&request)
            .send()
            .await?
            .error_for_status()
            .map(|_response| ())
            .map_err(|e| e.into())
    }

    /// Records the specified observations.
    pub async fn record_observations(
        &self,
        observations: &[Observation],
    ) -> Result<(), Box<dyn Error>> {
        #[derive(Serialize)]
        struct RecordObservationsRequest<'a> {
            observations: &'a [Observation],
        }

        let request = RecordObservationsRequest { observations };

        self.post_request_builder()
            .json(&request)
            .send()
            .await?
            .error_for_status()
            .map(|_response| ())
            .map_err(|e| e.into())
    }

    fn post_request_builder(&self) -> RequestBuilder {
        self.client
            .post(&format!("{}/{}", self.base_url, self.api_key))
    }
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Station {
    /// Identifies the station if multiple stations are registered with an account.
    #[serde(rename = "station")]
    pub id: u32,

    /// Data sharing policy
    pub visibility: StationVisibility,

    /// Name of the station
    pub name: String,

    /// North–south position on the Earth`s surface, in degrees.
    pub latitude: f32,

    /// East-west position on the Earth's surface, in degrees.
    pub longitude: f32,

    /// Elevation above sea level (reference geoid), in meters.
    pub elevation: u32,

    /// Temperature sensor height above the surface, in meters.
    #[serde(rename = "tempheight")]
    pub temp_height: u32,

    /// Wind sensor height above the surface, in meters.
    #[serde(rename = "windheight")]
    pub wind_height: u32,
}

/// Defines data sharing policy.
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub enum StationVisibility {
    /// Data is shared publicly with windy and its partners.
    Open,

    /// Data is only shared with windy, but is available publicly.
    #[serde(rename = "Only Windy")]
    OnlyWindy,

    /// Data is private to the user's account.
    Private,
}

/// An observation recorded by the station.
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Default)]
pub struct Observation {
    /// Station identifier.
    #[serde(rename = "station")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub station_id: Option<u32>,

    /// Time of the measurement.
    /// When not present, current server time is used.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time: Option<DateTime<Utc>>,

    /// Air temperature, in celsius.
    #[serde(rename = "temp")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,

    /// Wind speed, in m/s.
    #[serde(rename = "wind")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub wind_speed: Option<f32>,

    /// Wind direction, in degrees.
    #[serde(rename = "winddir")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub wind_direction: Option<u16>,

    /// Wind gust, in m/s.
    #[serde(rename = "gust")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub wind_gust: Option<f32>,

    /// Relative humidity, in %.
    #[serde(rename = "rh")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub relative_humidity: Option<f32>,

    /// Dew point, in celsius.
    #[serde(rename = "dewpoint")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dew_point: Option<f32>,

    /// Atmospheric pressure, in Pa.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pressure: Option<f32>,

    /// Precipitation over the past hour, in mm.
    #[serde(rename = "precip")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub precipitation: Option<f32>,

    /// UV index.
    #[serde(rename = "uv")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uv_index: Option<u8>,
}

#[cfg(test)]
mod tests {
    use crate::{Observation, Station, StationVisibility, WindyStation};
    use chrono::{FixedOffset, TimeZone, Utc};
    use mockito::mock;
    use reqwest::Client;
    use std::error::Error;
    use std::fs::read_to_string;

    #[tokio::test]
    async fn register_stations() -> Result<(), Box<dyn Error>> {
        let _ = env_logger::try_init();

        let body = read_to_string("test/request/register_stations.json")?;

        let mock = mock("POST", "/test-api-key")
            .with_status(200)
            .with_header("content-type", "text/html; charset=utf-8")
            .with_body(read_to_string("test/response/default.txt")?)
            .match_body(body.as_str())
            .create();

        {
            get_api()
                .register_stations(&[Station {
                    id: 0,
                    visibility: StationVisibility::Open,
                    name: "test-station".to_string(),
                    latitude: 49.282730,
                    longitude: -123.120735,
                    elevation: 62,
                    temp_height: 1,
                    wind_height: 2,
                }])
                .await?;
        }

        mock.assert();

        Ok(())
    }

    #[tokio::test]
    async fn simple_report() -> Result<(), Box<dyn Error>> {
        let _ = env_logger::try_init();

        let body = read_to_string("test/request/simple_report.json")?;

        let mock = mock("POST", "/test-api-key")
            .with_status(200)
            .with_header("content-type", "text/html; charset=utf-8")
            .with_body(read_to_string("test/response/default.txt")?)
            .match_body(body.as_str())
            .create();

        {
            get_api()
                .record_observations(&[Observation {
                    temperature: Some(-1.2_f32),
                    relative_humidity: Some(99_f32),
                    ..Default::default()
                }])
                .await?;
        }

        mock.assert();

        Ok(())
    }

    #[tokio::test]
    async fn complete_report() -> Result<(), Box<dyn Error>> {
        let _ = env_logger::try_init();

        let body = read_to_string("test/request/complete_report.json")?;

        let mock = mock("POST", "/test-api-key")
            .with_status(200)
            .with_header("content-type", "text/html; charset=utf-8")
            .with_body(read_to_string("test/response/default.txt")?)
            .match_body(body.as_str())
            .create();

        {
            get_api()
                .record_observations(&[Observation {
                    station_id: Some(1),
                    time: Some(
                        FixedOffset::west(4 * 3600)
                            .ymd(2014, 10, 23)
                            .and_hms_micro(20, 3, 41, 636000)
                            .with_timezone(&Utc),
                    ),
                    temperature: Some(-1.2),
                    wind_speed: Some(25.0),
                    wind_direction: Some(182),
                    wind_gust: Some(35.0),
                    relative_humidity: Some(96.0),
                    dew_point: Some(1.0),
                    pressure: Some(1021000.0),
                    precipitation: Some(2.4),
                    uv_index: Some(1),
                }])
                .await?;
        }

        mock.assert();

        Ok(())
    }

    fn get_api() -> WindyStation {
        WindyStation {
            api_key: "test-api-key".to_string(),
            client: Client::new(),
            base_url: mockito::server_url(),
        }
    }
}