opensky_api 0.1.4

A simple Rust wrapper library around the OpenSky Network API
Documentation
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
use serde::Deserialize;
use std::sync::Arc;

pub mod errors;
use errors::Error;

#[derive(Debug, Deserialize)]
struct InnerOpenSkyStates {
    pub time: u64,
    pub states: Vec<InnerStateVector>,
}

#[derive(Debug, Deserialize)]
struct ShortInnerOpenSkyStates {
    pub time: u64,
    pub states: Vec<ShortInnerStateVector>,
}

#[derive(Debug)]
pub struct OpenSkyStates {
    pub time: u64,
    pub states: Vec<StateVector>,
}

impl OpenSkyStates {
    fn from_inner(inner: InnerOpenSkyStates) -> Self {
        let mut states = Vec::with_capacity(inner.states.len());

        for inner in inner.states {
            states.push(StateVector::from_inner(inner));
        }

        Self {
            time: inner.time,
            states,
        }
    }

    fn from_short_inner(inner: ShortInnerOpenSkyStates) -> Self {
        let mut states = Vec::with_capacity(inner.states.len());

        for inner in inner.states {
            states.push(StateVector::from_short_inner(inner));
        }

        Self {
            time: inner.time,
            states,
        }
    }
}

// May Ferris forgive me.
// This needed to be done because the OpenSky API returns the state vectors as lists, and not
// objects. So this needed to be done for deserialization
#[derive(Debug, Deserialize)]
struct InnerStateVector(
    String,
    Option<String>,
    String,
    Option<u64>,
    u64,
    Option<f32>,
    Option<f32>,
    Option<f32>,
    bool,
    Option<f32>,
    Option<f32>,
    Option<f32>,
    Option<Vec<u64>>,
    Option<f32>,
    Option<String>,
    bool,
    u8,
    u32,
);

// I am very close to writing my own Json parser, because serde_json does not seem to be extremely
// well made for deserializing things that act this way. This is required, because in certain API
// accesses, the last undocumented 17th field is actually not provided. This will probably be
// temporary, but is so far required.
#[derive(Debug, Deserialize)]
struct ShortInnerStateVector(
    String,
    Option<String>,
    String,
    Option<u64>,
    u64,
    Option<f32>,
    Option<f32>,
    Option<f32>,
    bool,
    Option<f32>,
    Option<f32>,
    Option<f32>,
    Option<Vec<u64>>,
    Option<f32>,
    Option<String>,
    bool,
    u8,
);

#[derive(Debug)]
pub struct StateVector {
    pub icao24: String,
    pub callsign: Option<String>,
    pub origin_country: String,
    pub time_position: Option<u64>,
    pub last_contact: u64,
    pub longitude: Option<f32>,
    pub latitude: Option<f32>,
    pub baro_altitude: Option<f32>,
    pub on_ground: bool,
    pub velocity: Option<f32>,
    pub true_track: Option<f32>,
    pub vertical_rate: Option<f32>,
    pub sensors: Option<Vec<u64>>,
    pub geo_altitude: Option<f32>,
    pub squawk: Option<String>,
    pub spi: bool,
    pub position_source: u8,
    /// There is an undocumented extra field in StateVectors, for now it will be read, and just
    /// ignored. This will be updated when the API reference begins to list this field
    pub undocumented: Option<u32>,
}

/*
struct InnerFlight(
    String,
    u64,
    Option<String>,
    u64,
    Option<String>,
    Option<String>,
    u32,
    u32,
    u32,
    u32,
    u16,
    u16,
);

#[derive(Debug, Deserialize)]
pub struct Flight {
    pub icao24: String,
    #[serde(rename(deserialize = "firstSeen"))]
    pub first_seen: u64,
    #[serde(rename(deserialize = "firstSeen"))]
    pub est_departure_airport: Option<String>,
    #[serde(rename(deserialize = "firstSeen"))]
    pub last_seen: u64,
    #[serde(rename(deserialize = "firstSeen"))]
    pub est_arrival_airport: Option<String>,
    pub callsign: Option<String>,
    #[serde(rename(deserialize = "firstSeen"))]
    pub est_departure_airport_horiz_distance: u32,
    #[serde(rename(deserialize = "firstSeen"))]
    pub est_departure_airport_vert_distance: u32,
    #[serde(rename(deserialize = "firstSeen"))]
    pub est_arrival_airport_horiz_distance: u32,
    #[serde(rename(deserialize = "firstSeen"))]
    pub est_arrival_airport_vert_distance: u32,
    #[serde(rename(deserialize = "firstSeen"))]
    pub departure_airport_candidates_count: u16,
    #[serde(rename(deserialize = "firstSeen"))]
    pub arrival_airport_candidates_count: u16,
}

impl Flight {
    fn from_inner(i_f: InnerFlight) -> Self {
        Self {
            icao24: i_f.0,
            first_seen: i_f.1,
            est_departure_airport: i_f.2,
            last_seen: i_f.3,
            est_arrival_airport: i_f.4,
            callsign: i_f.5,
            est_departure_airport_horiz_distance: i_f.6,
            est_departure_airport_vert_distance: i_f.7,
            est_arrival_airport_horiz_distance: i_f.8,
            est_arrival_airport_vert_distance: i_f.9,
            departure_airport_candidates_count: i_f.10,
            arrival_airport_candidates_count: i_f.11,
        }
    }
}
*/

impl StateVector {
    fn from_inner(isv: InnerStateVector) -> Self {
        Self {
            icao24: isv.0,
            callsign: isv.1,
            origin_country: isv.2,
            time_position: isv.3,
            last_contact: isv.4,
            longitude: isv.5,
            latitude: isv.6,
            baro_altitude: isv.7,
            on_ground: isv.8,
            velocity: isv.9,
            true_track: isv.10,
            vertical_rate: isv.11,
            sensors: isv.12,
            geo_altitude: isv.13,
            squawk: isv.14,
            spi: isv.15,
            position_source: isv.16,
            undocumented: Some(isv.17),
        }
    }

    fn from_short_inner(isv: ShortInnerStateVector) -> Self {
        Self {
            icao24: isv.0,
            callsign: isv.1,
            origin_country: isv.2,
            time_position: isv.3,
            last_contact: isv.4,
            longitude: isv.5,
            latitude: isv.6,
            baro_altitude: isv.7,
            on_ground: isv.8,
            velocity: isv.9,
            true_track: isv.10,
            vertical_rate: isv.11,
            sensors: isv.12,
            geo_altitude: isv.13,
            squawk: isv.14,
            spi: isv.15,
            position_source: isv.16,
            undocumented: None,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct BoundingBox {
    pub lat_min: f32,
    pub lat_max: f32,
    pub long_min: f32,
    pub long_max: f32,
}

impl BoundingBox {
    pub fn new(lat_min: f32, lat_max: f32, long_min: f32, long_max: f32) -> Self {
        Self {
            lat_min,
            lat_max,
            long_min,
            long_max,
        }
    }
}

#[derive(Debug, Clone)]
pub struct StateRequest {
    login: Option<Arc<(String, String)>>,
    bbox: Option<BoundingBox>,
    time: Option<u64>,
    icao24_addresses: Vec<String>,
    serials: Vec<u64>,
}

#[derive(Debug, Clone)]
struct FlightsRequest {
    login: Option<Arc<(String, String)>>,
    begin: u64,
    end: u64,
    icao24_address: Option<String>,
}

#[derive(Debug, Clone)]
struct ArrivalsRequest {}

/*
impl FlightsRequest {
    pub async fn send(&self) -> Result<(), Error> {
        let login_part = if let Some(login) = &self.login {
            format!("{}:{}@", login.0, login.1)
        } else {
            String::new()
        };

        let mut args = String::new();

        args.push_str(&format!("?begin={}&end={}", self.begin, self.end));

        let endpoint = "all";

        let url = format!(
            "https://{}opensky-network.org/api/flights/{}{}",
            login_part, endpoint, args
        );

        println!("url = {}", url);

        let res = reqwest::get(url).await?;

        match res.status() {
            reqwest::StatusCode::OK => {
                let bytes = res.bytes().await?.to_vec();

                let string = String::from_utf8(bytes).unwrap();

                println!("{}", &string[0..200]);

                unimplemented!();

                Ok(())
            }
            status => Err(Error::Http(status)),
        }
    }
}
*/

impl StateRequest {
    pub async fn send(&self) -> Result<OpenSkyStates, Error> {
        let login_part = if let Some(login) = &self.login {
            format!("{}:{}@", login.0, login.1)
        } else {
            String::new()
        };

        let mut args = String::new();

        if let Some(time) = self.time {
            if args.is_empty() {
                args.push('?');
            }

            args.push_str(&format!("time={}", time));
        }

        if let Some(bbox) = self.bbox {
            if args.is_empty() {
                args.push('?');
            } else {
                args.push('&');
            }

            args.push_str(&format!(
                "lamin={}&lomin={}&lamax={}&lomax={}",
                bbox.lat_min, bbox.long_min, bbox.lat_max, bbox.long_max
            ));
        }

        if !self.icao24_addresses.is_empty() {
            if args.is_empty() {
                args.push('?');
            } else {
                args.push('&');
            }

            if let Some(first) = self.icao24_addresses.get(0) {
                args.push_str(&format!("icao24={}", first));
            }

            for icao24 in self.icao24_addresses.iter().skip(1) {
                args.push_str(&format!("&icao24={}", icao24));
            }
        }

        // If serial numbers are provided determines which endpoint we use
        let endpoint = if !self.serials.is_empty() {
            if args.is_empty() {
                args.push('?');
            } else {
                args.push('&');
            }

            if let Some(first) = self.serials.get(0) {
                args.push_str(&format!("serials={}", first));
            }

            for serial in self.serials.iter().skip(1) {
                args.push_str(&format!("&serials={}", serial));
            }

            "own"
        } else {
            "all"
        };

        let url = format!(
            "https://{}opensky-network.org/api/states/{}{}",
            login_part, endpoint, args
        );

        let res = reqwest::get(url).await?;

        match res.status() {
            reqwest::StatusCode::OK => {
                let bytes = res.bytes().await?.to_vec();

                Ok(if self.time.is_some() {
                    let short_inner_states: ShortInnerOpenSkyStates =
                        serde_json::from_slice(&bytes)?;

                    OpenSkyStates::from_short_inner(short_inner_states)
                } else {
                    // Retry for random API deviations
                    if let Ok(inner_states) = serde_json::from_slice(&bytes) {
                        OpenSkyStates::from_inner(inner_states)
                    } else {
                        let short_inner_states: ShortInnerOpenSkyStates =
                            serde_json::from_slice(&bytes)?;

                        OpenSkyStates::from_short_inner(short_inner_states)
                    }
                })
            }
            status => Err(Error::Http(status)),
        }
    }
}

pub struct StateRequestBuilder {
    inner: StateRequest,
}

/*
struct FlightsRequestBuilder {
    inner: FlightsRequest,
}

impl FlightsRequestBuilder {
    fn new(login: Option<Arc<(String, String)>>, begin: u64, end: u64) -> Self {
        Self {
            inner: FlightsRequest {
                login,
                begin,
                end,
                icao24_address: None,
            },
        }
    }

    /// This method is redundant, but can be used to reuse the same FlightsRequestBuilder multiple
    /// times to create different requests. This sets the beginning and end of the flight request
    /// interval. The beginning and ending times are numbers that represent times in seconds since
    /// the Unix Epoch.
    ///
    /// The interval must not span greater than 2 hours, otherwise the request will fail.
    ///
    pub fn in_interval(&mut self, begin: u64, end: u64) -> &mut Self {
        self.inner.begin = begin;
        self.inner.end = end;

        self
    }

    /// This method can be used to filter the flight data by a specific aircraft. The aircraft
    /// ICAO24 address is in hex string representation.
    ///
    pub fn by_aircraft(&mut self, address: String) -> &mut Self {
        self.inner.icao24_address = Some(address);

        self
    }

    /// Consumes this FlightsRequestBuilder and returns a new FlightsRequest. If this
    /// FlightsRequestBuilder could be used again effectively, then the finish() method should
    /// be called instead because that will allow this to be reused.
    ///
    pub fn consume(self) -> FlightsRequest {
        self.inner
    }

    /// Returns the FlightsRequest that this FlightsRequestBuilder has created. This clones the inner
    /// FlightsRequest. If this FlightsRequestBuilder will be only used once, the consume() method
    /// should be used instead which will only move the inner value instead of calling clone()
    ///
    pub fn finish(&self) -> FlightsRequest {
        self.inner.clone()
    }

    /// Consumes this FlightsRequestBuilder and sends the request to the API.
    pub async fn send(self) -> Result<(), Error> {
        self.inner.send().await
    }
}
*/

impl StateRequestBuilder {
    fn new(login: Option<Arc<(String, String)>>) -> Self {
        Self {
            inner: StateRequest {
                login,
                bbox: None,
                time: None,
                icao24_addresses: Vec::new(),
                serials: Vec::new(),
            },
        }
    }

    /// Adds the provided bounding box to the request. This will only get states that are within
    /// that bounding box. This will overwrite any previously specified bounding box.
    ///
    pub fn with_bbox(mut self, bbox: BoundingBox) -> Self {
        self.inner.bbox = Some(bbox);

        self
    }

    /// Specifies the time at which to get the data. The validity of this timestamp depends on how
    /// much access the user has to historical data.
    ///
    /// This time is specified as the time in seconds since the Unix Epoch.
    ///
    pub fn at_time(mut self, timestamp: u64) -> Self {
        self.inner.time = Some(timestamp);

        self
    }

    /// Adds an ICAO24 transponder address represented by a hex string (e.g. abc9f3) to filter the
    /// request by. Calling this function multiple times will append more addresses which will be
    /// included in the returned data.
    ///
    /// If this function is never called, it will provide data for all aircraft.
    ///
    pub fn with_icao24(mut self, address: String) -> Self {
        self.inner.icao24_addresses.push(address);

        self
    }

    /// Adds a serial number of a sensor that you own. This must be owned by you and registered in
    /// order to not return an HTTP error 403 (Forbidden). Requests from your own sensors are not
    /// ratelimited.
    ///
    /// Calling this function multiple times will append more serial numbers of receiviers which
    /// provide the returned data.
    ///
    pub fn with_serial(mut self, serial: u64) -> Self {
        self.inner.serials.push(serial);

        self
    }

    /// Consumes this StateRequestBuilder and returns a new StateRequest. If this
    /// StateRequestBuilder could be used again effectively, then the finish() method should
    /// be called instead because that will allow this to be reused.
    ///
    pub fn consume(self) -> StateRequest {
        self.inner
    }

    /// Returns the StateRequest that this StateRequestBuilder has created. This clones the inner
    /// StateRequest. If this StateRequestBuilder will be only used once, the consume() method
    /// should be used instead which will only move the inner value instead of calling clone()
    ///
    pub fn finish(&self) -> StateRequest {
        self.inner.clone()
    }

    /// Consumes this StateRequestBuilder and sends the request to the API.
    pub async fn send(self) -> Result<OpenSkyStates, Error> {
        self.inner.send().await
    }
}

pub struct OpenSkyApi {
    login: Option<Arc<(String, String)>>,
}

impl OpenSkyApi {
    /// Creates a new anonymous OpenSkyApi instance
    pub fn new() -> Self {
        Self { login: None }
    }

    /// Creates a new OpenSkyApi instance with the provided username and password
    pub fn with_login(username: String, password: String) -> Self {
        Self {
            login: Some(Arc::new((username, password))),
        }
    }

    /// Creates a new StateRequestBuilder which can be used to create StateRequests
    pub fn get_states(&self) -> StateRequestBuilder {
        StateRequestBuilder::new(self.login.clone())
    }

    /*
    /// Creates a new FlightsRequestBuilder using the given time interval. The beginning
    /// and ending times are numbers that represent times in seconds since the Unix Epoch.
    ///
    /// The interval must not span greater than 2 hours, otherwise the request will fail.
    ///
    pub fn get_flights(&self, begin: u64, end: u64) -> FlightsRequestBuilder {
        FlightsRequestBuilder::new(self.login.clone(), begin, end)
    }
    */
}

impl From<StateRequestBuilder> for StateRequest {
    fn from(srb: StateRequestBuilder) -> Self {
        srb.consume()
    }
}

/*
impl From<FlightsRequestBuilder> for FlightsRequest {
    fn from(frb: FlightsRequestBuilder) -> Self {
        frb.consume()
    }
}
*/