module Data.Geolocation.Reverse where
import Control.Lens
import Network.Wreq
import Data.Aeson
import Data.Aeson.Types
import Data.Aeson.Lens (key)
import Data.Geolocation.Reverse.Types
import Data.Geolocation.Reverse.Providers
defaultReverseGeoProvider :: ReverseGeoProvider
defaultReverseGeoProvider = openStreetMap
getLocationInfo
:: Latitude
-> Longitude
-> ReverseGeoProvider
-> IO (Maybe ParsedLocationInfo)
getLocationInfo lat lon (jsonkey,url,parser) =
case url lat lon of
Nothing -> return Nothing
Just ga -> do
r <- get ga
let addr = r ^? responseBody . key jsonkey
case addr of
Just (Object o) -> return . flip parseMaybe o . return
$ parser o
_ -> return Nothing
getLocationInfoDef :: Latitude -> Longitude -> IO (Maybe ParsedLocationInfo)
getLocationInfoDef lat lon = getLocationInfo lat lon defaultReverseGeoProvider