blob: 7804ef29cd0b96f49c6268f2868f5f10bfbdff17 (
plain)
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
|
{-|
High-level client functions perfoming requests to OpenWeatherMap API.
-}
module Web.OpenWeatherMap.Client (
Location(..),
getWeather
) where
import Network.HTTP.Client (newManager, defaultManagerSettings)
import Servant.Client (ClientEnv(..), runClientM, ServantError)
import Servant.Common.BaseUrl (BaseUrl(..), Scheme(..))
import Servant.Common.Req (ClientM)
import Web.OpenWeatherMap.Types.CurrentWeather (CurrentWeather)
import qualified Web.OpenWeatherMap.API as API
-- | Various way to specify location.
data Location
= Name String -- ^ City name.
| Coord Double Double -- ^ Geographic coordinates: latitude and longitude.
-- | Make a request to OpenWeatherMap API
-- and return current weather in given location.
getWeather
:: String -- ^ API key.
-> Location
-> IO (Either ServantError CurrentWeather)
getWeather appid loc =
defaultEnv >>= runClientM (api loc appid)
api
:: Location
-> String -- ^ API key.
-> ClientM CurrentWeather
api (Name city) = API.weatherByName (Just city) . Just
api (Coord lat lon) = API.weatherByCoord (Just lat) (Just lon) . Just
defaultEnv :: IO ClientEnv
defaultEnv = do
manager <- newManager defaultManagerSettings
return $ ClientEnv manager baseUrl
-- XXX openweathermap.org does not support HTTPS,
-- XXX appid is passed in clear text. Oops.
baseUrl :: BaseUrl
baseUrl = BaseUrl Http "api.openweathermap.org" 80 "/data/2.5"
|