aboutsummaryrefslogtreecommitdiff
path: root/lib/Web/OpenWeatherMap/Client.hs
blob: d9fdb1890cf3e578fecf6870ed71088999e4b1ca (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
{-|
High-level client functions perfoming requests to OpenWeatherMap API.
-}
module Web.OpenWeatherMap.Client
  ( getWeather
  , getForecast
  ) where

import Network.HTTP.Client.TLS (newTlsManager)
import Servant.Client
  ( BaseUrl(BaseUrl)
  , ClientEnv
  , ClientError
  , Scheme(Https)
  , mkClientEnv
  , runClientM
  )

import qualified Web.OpenWeatherMap.API as API
import Web.OpenWeatherMap.Types.CurrentWeather (CurrentWeather)
import Web.OpenWeatherMap.Types.ForecastWeather (ForecastWeather)
import Web.OpenWeatherMap.Types.Location (Location)

-- | Make a request to OpenWeatherMap API
--   and return current weather in given location.
getWeather ::
     String -- ^ API key.
  -> Location
  -> IO (Either ClientError CurrentWeather)
getWeather appid loc = defaultEnv >>= runClientM (API.currentWeather appid loc)

-- | Make a request to OpenWeatherMap API
--   and return forecast weather in given location.
getForecast ::
     String -- ^ API key.
  -> Location
  -> IO (Either ClientError ForecastWeather)
getForecast appid loc =
  defaultEnv >>= runClientM (API.forecastWeather appid loc)

defaultEnv :: IO ClientEnv
defaultEnv = do
  manager <- newTlsManager
  return $ mkClientEnv manager baseUrl

baseUrl :: BaseUrl
baseUrl = BaseUrl Https "api.openweathermap.org" 443 "/data/2.5"