module Web.API.MapQuest.Geocoding (request, GeoQuery(..), Coords(..))where
import Data.List (intersperse)
import Data.Monoid ((<>))
import Network.HTTP.Req
import qualified Data.Text as T
import qualified Data.ByteString.Lazy as LBS
import GHC.Generics
import Control.Monad.Catch
import Data.Aeson
import Data.Aeson.Types (Parser, parseMaybe)
apiRootPath :: Url 'Http
apiRootPath = http "www.mapquestapi.com" /: "geocoding" /: "v1" /: "address"
instance MonadHttp IO where
handleHttpException = throwM
request ::
T.Text
-> GeoQuery
-> IO (Maybe (Coords Float))
request apikey q = do
r <- req GET apiRootPath NoReqBody lbsResponse opts'
return $ decoder1 $ responseBody r where
opts' =
("key" =: apikey) <>
("outFormat" =: ("json" :: T.Text)) <>
("location" =: renderGeoQuery q)
decoder1 :: LBS.ByteString -> Maybe (Coords Float)
decoder1 dat = do
r <- decode dat
flip parseMaybe r $ \obj -> do
locp <- decodeLocation obj
decodeLatLong locp
decodeLocation :: FromJSON a => Object -> Parser a
decodeLocation obj = do
(res0 : _) <- obj .: "results"
(loc0 : _) <- res0 .: "locations"
return loc0
decodeLatLong :: Object -> Parser (Coords Float)
decodeLatLong loc = do
ll <- loc .: "latLng"
Coords <$> ll .: "lat" <*> ll .: "lng"
data Coords a = Coords {
lat :: a
, long :: a
} deriving (Eq, Show, Generic)
data GeoQuery = GQ {
gqStreet :: T.Text
, gqCity :: T.Text
, gqCountry :: T.Text
}
| GQFree T.Text
deriving (Eq, Show)
renderGeoQuery :: GeoQuery -> T.Text
renderGeoQuery q = case q of
(GQ addr city country) -> T.concat $ intersperse ", " [addr, city, country]
(GQFree t) -> t