IQAir api access crate
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
use serde_derive::Deserialize;
use serde_derive::Serialize;
use time::OffsetDateTime;
use url_escape::QUERY;

#[derive(Debug, Deserialize)]
struct Dat {
    data: Data,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Data {
    pub city: String,
    pub state: String,
    pub country: String,
    pub location: Location,
    pub current: Current,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Location {
    /// Lat, Lon
    pub coordinates: (f64, f64),
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Current {
    pub pollution: Pollution,
    pub weather: Weather,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Pollution {
    #[serde(rename = "ts")]
    #[serde(with = "time::serde::iso8601")]
    pub timestamp: OffsetDateTime,
    // AQI value based on US EPA standard
    #[serde(rename = "aqius")]
    pub aqi_us: u32,
    /// main pollutant for US AQI
    #[serde(rename = "mainus")]
    pub main_us: String,
    #[serde(rename = "aqicn")]
    pub aqi_cn: u32,
    /// main pollutant for CN AQI
    #[serde(rename = "maincn")]
    pub main_cn: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Weather {
    #[serde(rename = "ts")]
    #[serde(with = "time::serde::iso8601")]
    pub timestamp: OffsetDateTime,
    #[serde(rename = "tp")]
    /// temperature in Celsius
    pub temp: i32,
    #[serde(rename = "pr")]
    /// atmospheric pressure in hPa
    pub pressure: i32,
    #[serde(rename = "hu")]
    /// humidity %
    pub humidity: u16,
    #[serde(rename = "ws")]
    /// wind speed (m/s)
    pub wind_speed: f32,
    #[serde(rename = "wd")]
    /// wind direction, as an angle of 360° (N=0, E=90, S=180, W=270)
    pub wind_direction: i16,
    #[serde(rename = "ic")]
    /// weather icon code, see below for icon index
    pub icon: Icon,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub enum Icon {
    #[serde(rename = "01d")]
    ClearDaySky,
    #[serde(rename = "01n")]
    ClearNightSky,
    #[serde(rename = "02d")]
    FewDayClouds,
    #[serde(rename = "02n")]
    FewNightClouds,
    #[serde(rename = "03d")]
    ScatteredClouds,
    #[serde(rename = "04d")]
    BrokenClouds,
    #[serde(rename = "09d")]
    ShowerRain,
    #[serde(rename = "10d")]
    DayRain,
    #[serde(rename = "10n")]
    NightRain,
    #[serde(rename = "11d")]
    Thunderstorm,
    #[serde(rename = "13d")]
    Snow,
    #[serde(rename = "50d")]
    Mist,
    #[serde(untagged)]
    Other(String),
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Error {
    Success,
    ///  when minute/monthly limit is reached.
    CallLimitReached,
    /// when API key is expired.
    ApiKeyExpired,
    /// returned when using wrong API key.
    IncorrectApiKey,
    /// when service is unable to locate IP address of request.
    IpLocationFailed,
    /// when there is no nearest station within specified radius.
    NoNearestStation,
    /// when call requests a feature that is not available in chosen subscription plan.
    FeatureNotAvailable,
    /// when more than 10 calls per second are made.
    TooManyRequests,
    #[serde(skip)]
    Io(std::io::Error),
    #[serde(skip)]
    Ureq(ureq::Error),
    #[serde(skip)]
    Serde(serde_json::Error),
}
impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use Error::*;
        match self {
            Success => unreachable!(),
            CallLimitReached => f.write_str("minute/monthly limit is reached"),
            ApiKeyExpired => f.write_str("API key is expired"),
            Error::Ureq(ureq::Error::StatusCode(403)) | IncorrectApiKey => {
                f.write_str("using wrong API key")
            }
            IpLocationFailed => f.write_str("service is unable to locate IP address of request"),
            NoNearestStation => f.write_str("there is no nearest station within specified radius"),
            FeatureNotAvailable => f.write_str(
                "call requests a feature that is not available in chosen subscription plan",
            ),
            TooManyRequests => f.write_str("more than 10 calls per second are made"),
            Error::Io(error) => write!(f, "{error}"),
            Error::Ureq(error) => write!(f, "{error}"),
            Error::Serde(error) => write!(f, "{error}"),
        }
    }
}
impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(match self {
            Error::Io(error) => error,
            Error::Ureq(error) => error,
            Error::Serde(error) => error,
            _ => return None,
        })
    }
}

fn decode_resp(result: &str) -> std::result::Result<Data, Error> {
    #[derive(Deserialize)]
    struct Status {
        status: Error,
    }
    let Status { status } = serde_json::from_str::<Status>(&result).map_err(Error::Serde)?;
    if !matches!(status, Error::Success) {
        return Err(status);
    }
    serde_json::from_str::<Dat>(&result)
        .map_err(Error::Serde)
        .map(|Dat { data }| data)
}

fn from_uri(uri: &str) -> std::result::Result<Data, Error> {
    let mut result = String::with_capacity(50);
    std::io::Read::read_to_string(
        &mut ureq::get(dbg!(url_escape::encode(uri, QUERY).into_owned()))
            .call()
            .map_err(Error::Ureq)?
            .body_mut()
            .as_reader(),
        &mut result,
    )
    .map_err(Error::Io)?;
    decode_resp(&result)
}

pub fn by_location(
    city: &str,
    state: &str,
    country: &str,
    key: &str,
) -> std::result::Result<Data, Error> {
    from_uri(&format!(
        "https://api.airvisual.com/v2/city?city={city}&state={state}&country={country}&key={key}"
    ))
}

/// Get pollution data for nearest city.
pub fn nearest(key: &str) -> std::result::Result<Data, Error> {
    from_uri(&format!(
        "https://api.airvisual.com/v2/nearest_city?key={key}"
    ))
}