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
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate reqwest;
extern crate time;
extern crate url;

mod weather_types;

use weather_types::*;
use url::Url;

static API_BASE: &str = "https://api.openweathermap.org/data/2.5/";

#[derive(Debug)]
pub enum LocationSpecifier<'a>{
    CityAndCountryName {city: &'a str, country: &'a str},
    CityId(&'a str),
    Coordinates {lat: f32, lon: f32},
    ZipCode {zip: &'a str, country: &'a str},

    // The following location specifiers are used to specify multiple cities or a region
    BoundingBox {lon_left: f32, lat_bottom: f32, lon_right: f32, lat_top: f32, zoom: f32},
    Circle {lat: f32, lon: f32, count: u16},
    CityIds(Vec<&'a str>),
}

impl<'a> LocationSpecifier<'a> {
    pub fn format(&'a self) -> Vec<(String, String)> {
        match &self {
            LocationSpecifier::CityAndCountryName {city, country} => {
                if *country == "" {
                    return vec![("q".to_string(), city.to_string())];
                } else {
                    return vec![("q".to_string(), format!("{},{}", city, country))];
                }
            }
            LocationSpecifier::CityId(id) => {
                return vec![("id".to_string(), id.to_string())];
            }
            LocationSpecifier::Coordinates {lat, lon} => {
                return vec![("lat".to_string(), format!("{}",lat)), 
                            ("lon".to_string(), format!("{}",lon))];
            }
            LocationSpecifier::ZipCode {zip, country} => {
                if *country == "" {
                    return vec![("zip".to_string(), zip.to_string())];
                } else {
                    return vec![("zip".to_string(), format!("{},{}", zip, country))];
                }
            }
            LocationSpecifier::BoundingBox {lon_left, lat_bottom, lon_right, lat_top, zoom} => {
                return vec![("bbox".to_string(), format!("{},{},{},{},{}", lon_left, lat_bottom, lon_right, lat_top, zoom))];
            }
            LocationSpecifier::Circle {lat, lon, count} => {
                return vec![("lat".to_string(), format!("{}", lat)), 
                            ("lon".to_string(), format!("{}", lon)), 
                            ("cnt".to_string(), format!("{}", count))];
            }
            LocationSpecifier::CityIds(ids) => {
                let mut locations: String = "".to_string();
                for loc in ids { locations += loc; }
                return vec![("id".to_string(), locations.to_string())];
            }
        }
    }
}

fn get<T>(url: &str) -> Result<T, ErrorReport> where T: serde::de::DeserializeOwned {
    let res = reqwest::get(url).unwrap().text().unwrap();
    let data: T = match serde_json::from_str(&res) {
        Ok(val) => {val},
        Err(_) => {
            let err_report: ErrorReport = match serde_json::from_str(res.as_str()) {
                Ok(report) => {report},
                Err(_) => {
                    return Err(ErrorReport{cod: 0, message: format!("Got unexpected response: {:?}", res)});
                }
            };
            return Err(err_report);
        }
    };
    Ok(data)
}

pub fn get_current_weather(location: LocationSpecifier, key: &str) -> Result<WeatherReportCurrent, ErrorReport> {
	let mut base = String::from(API_BASE);
	let mut params = location.format();

	base.push_str("weather");
	params.push(("APPID".to_string(), key.to_string()));

	let url = Url::parse_with_params(&base, params).unwrap();
    get(&url.as_str())
}

pub fn get_5_day_forecast(location: LocationSpecifier, key: &str) -> Result<WeatherReport5Day, ErrorReport> {
    let mut base = String::from(API_BASE);
	let mut params = location.format();

	base.push_str("forecast");
	params.push(("APPID".to_string(), key.to_string()));

	let url = Url::parse_with_params(&base, params).unwrap();
    get(&url.as_str())
}

pub fn get_16_day_forecast(location: LocationSpecifier, key: &str, len: u8) -> Result<WeatherReport16Day, ErrorReport> {
    if len > 16 || len == 0 {
        return Err(ErrorReport{cod: 0, message: format!("Only support 1 to 16 day forecasts but {:?} requested", len)});
    }
    let mut base = String::from(API_BASE);
	let mut params = location.format();

	base.push_str("forecast/daily");
	params.push(("cnt".to_string(), format!("{}", len)));
	params.push(("APPID".to_string(), key.to_string()));

	let url = Url::parse_with_params(&base, params).unwrap();
    get(&url.as_str())
}

pub fn get_historical_data(location: LocationSpecifier, key: &str, start: time::Timespec, end: time::Timespec) -> Result<WeatherReportHistorical, ErrorReport> {
    let mut base = String::from(API_BASE);
	let mut params = location.format();

	base.push_str("history/city");
	params.push(("type".to_string(), "hour".to_string()));
	params.push(("start".to_string(), format!("{}", start.sec)));
	params.push(("end".to_string(), format!("{}", end.sec)));
	params.push(("APPID".to_string(), key.to_string()));

	let url = Url::parse_with_params(&base, params).unwrap();
    get(&url.as_str())
}

pub fn get_accumulated_temperature_data(location: LocationSpecifier, key: &str, start: time::Timespec, end: time::Timespec, threshold: u32) -> Result<WeatherAccumulatedTemperature, ErrorReport> {
    let mut base = String::from(API_BASE);
	let mut params = location.format();

	base.push_str("history/accumulated_temperature");
	params.push(("type".to_string(), "hour".to_string()));
	params.push(("start".to_string(), format!("{}", start.sec)));
	params.push(("end".to_string(), format!("{}", end.sec)));
	params.push(("threshold".to_string(), format!("{}", threshold)));
	params.push(("APPID".to_string(), key.to_string()));

	let url = Url::parse_with_params(&base, params).unwrap();
    get(&url.as_str())
}

pub fn get_accumulated_precipitation_data(location: LocationSpecifier, key: &str, start: time::Timespec, end: time::Timespec, threshold: u32) -> Result<WeatherAccumulatedPrecipitation, ErrorReport> {
    let mut base = String::from(API_BASE);
	let mut params = location.format();

	base.push_str("history/accumulated_precipitation");
	params.push(("type".to_string(), "hour".to_string()));
	params.push(("start".to_string(), format!("{}", start.sec)));
	params.push(("end".to_string(), format!("{}", end.sec)));
	params.push(("threshold".to_string(), format!("{}", threshold)));
	params.push(("APPID".to_string(), key.to_string()));

	let url = Url::parse_with_params(&base, params).unwrap();
    get(&url.as_str())
}

pub fn get_current_uv_index(location: LocationSpecifier, key: &str) -> Result<UvIndex, ErrorReport> {
    let mut base = String::from(API_BASE);
	let mut params = location.format();

	base.push_str("uvi");
	params.push(("APPID".to_string(), key.to_string()));

	let url = Url::parse_with_params(&base, params).unwrap();
    get(&url.as_str())
}

pub fn get_forecast_uv_index(location: LocationSpecifier, key: &str, len: u8) -> Result<ForecastUvIndex, ErrorReport> {
    if len > 8 || len == 0 {
        return Err(ErrorReport{cod: 0, message: format!("Only support 1 to 8 day forecasts but {:?} requested", len)});
    }
    let mut base = String::from(API_BASE);
	let mut params = location.format();

	base.push_str("uvi/forecast");
	params.push(("cnt".to_string(), format!("{}", len)));
	params.push(("APPID".to_string(), key.to_string()));

	let url = Url::parse_with_params(&base, params).unwrap();
    get(&url.as_str())
}

pub fn get_historical_uv_index(location: LocationSpecifier, key: &str, start: time::Timespec, end: time::Timespec) -> Result<HistoricalUvIndex, ErrorReport> {
    let mut base = String::from(API_BASE);
	let mut params = location.format();

	base.push_str("uvi/history");
	params.push(("start".to_string(), format!("{}", start.sec)));
	params.push(("end".to_string(), format!("{}", end.sec)));
	params.push(("APPID".to_string(), key.to_string()));

	let url = Url::parse_with_params(&base, params).unwrap();
    get(&url.as_str())
}