diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2017-06-07 16:24:05 +0300 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2017-06-07 16:35:08 +0300 |
commit | 6f6e3d8ec76c59d6f403725e9b2a22b8f6680714 (patch) | |
tree | 45382bc0e03032ffa09b7b63afdc45e243e60f4f /lib/Web/OpenWeatherMap/Client.hs | |
download | openweathermap-6f6e3d8ec76c59d6f403725e9b2a22b8f6680714.tar.gz |
Initial version 0.0.00.0.0
Diffstat (limited to 'lib/Web/OpenWeatherMap/Client.hs')
-rw-r--r-- | lib/Web/OpenWeatherMap/Client.hs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/Web/OpenWeatherMap/Client.hs b/lib/Web/OpenWeatherMap/Client.hs new file mode 100644 index 0000000..7804ef2 --- /dev/null +++ b/lib/Web/OpenWeatherMap/Client.hs @@ -0,0 +1,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" + |