aboutsummaryrefslogtreecommitdiff
path: root/lib/Web/OpenWeatherMap/API.hs
blob: 516845cfe09cc619fc9c6fca3a8d9d393390a6e4 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
{-|
Direct API functions.
For API key (a.k.a appid) refer to <http://openweathermap.org/appid>.
-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}

module Web.OpenWeatherMap.API
  ( weatherByName
  , weatherByCoord
  , forecastByName
  , forecastByCoord
  ) where

import Data.Proxy (Proxy(..))

import Servant.API ((:<|>)(..), (:>), Get, JSON, QueryParam', Required, Strict)
import Servant.Client (ClientM, client)

import Web.OpenWeatherMap.Types.CurrentWeather (CurrentWeather)
import Web.OpenWeatherMap.Types.ForecastWeather (ForecastWeather)

type QueryParam = QueryParam' '[ Required, Strict]

type GetCurrentWeather = AppId :> Get '[ JSON] CurrentWeather

type GetForecastWeather = AppId :> Get '[ JSON] ForecastWeather

type AppId = QueryParam "appid" String

type Current
   = "weather" :> (QueryParam "q" String :> GetCurrentWeather :<|> QueryParam "lat" Double :> QueryParam "lon" Double :> GetCurrentWeather)

type Forecast
   = "forecast" :> (QueryParam "q" String :> GetForecastWeather :<|> QueryParam "lat" Double :> QueryParam "lon" Double :> GetForecastWeather)

type API = Current :<|> Forecast

-- | Request current weather in the city.
weatherByName ::
     String -- ^ City name, e. g. \"Moscow\" or \"Moscow,ru\".
  -> String -- ^ API key.
  -> ClientM CurrentWeather
-- | Request current weather at the geographic coordinates (in decimal degrees).
weatherByCoord ::
     Double -- ^ Latitude, e. g. 55.7522200 for Moscow.
  -> Double -- ^ Longitude, e. g. 37.6155600 for Moscow.
  -> String -- ^ API key.
  -> ClientM CurrentWeather
-- | Request forecast weather in the city.
forecastByName ::
     String -- ^ City name, e. g. \"Moscow\" or \"Moscow,ru\".
  -> String -- ^ API key.
  -> ClientM ForecastWeather
-- | Request current weather at the geographic coordinates (in decimal degrees).
forecastByCoord ::
     Double -- ^ Latitude, e. g. 55.7522200 for Moscow.
  -> Double -- ^ Longitude, e. g. 37.6155600 for Moscow.
  -> String -- ^ API key.
  -> ClientM ForecastWeather
(weatherByName :<|> weatherByCoord) :<|> (forecastByName :<|> forecastByCoord) =
  client (Proxy :: Proxy API)