module Web.OpenWeatherMap.Client
( Location(..)
, getWeather
) where
import Network.HTTP.Client (defaultManagerSettings, newManager)
import Servant.Client
( BaseUrl(BaseUrl)
, ClientEnv
, ClientError
, ClientM
, Scheme(Http)
, mkClientEnv
, runClientM
)
import qualified Web.OpenWeatherMap.API as API
import Web.OpenWeatherMap.Types.CurrentWeather (CurrentWeather)
data Location
= Name String
| Coord Double
Double
getWeather ::
String
-> Location
-> IO (Either ClientError CurrentWeather)
getWeather appid loc = defaultEnv >>= runClientM (api loc appid)
api ::
Location
-> String
-> 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 $ mkClientEnv manager baseUrl
baseUrl :: BaseUrl
baseUrl = BaseUrl Http "api.openweathermap.org" 80 "/data/2.5"