packages feed

mapquest-api 0.2.0.0 → 0.3

raw patch · 3 files changed

+55/−22 lines, 3 filesdep +gogglesdep +mtldep ~reqPVP ok

version bump matches the API change (PVP)

Dependencies added: goggles, mtl

Dependency ranges changed: req

API changes (from Hackage documentation)

- Web.API.MapQuest: request :: Text -> GeoQuery -> IO (Maybe (Coords Float))
+ Web.API.MapQuest: Creds :: Text -> Creds
+ Web.API.MapQuest: [apiKey] :: Creds -> Text
+ Web.API.MapQuest: newtype Creds
+ Web.API.MapQuest: runRequest :: Creds -> GeoQuery -> IO (Maybe (Coords Float))

Files

mapquest-api.cabal view
@@ -1,5 +1,5 @@ name:                mapquest-api-version:             0.2.0.0+version:             0.3 synopsis:            Bindings to the MapQuest API description:         This library provides a high-level interface to the MapQuest API. Currently only the "geocoding" API (street address to coordinates) is provided, but the functionality is straightforward to extend. homepage:            https://github.com/ocramz/mapquest-api@@ -21,7 +21,9 @@   exposed-modules:     Web.API.MapQuest   other-modules:       Web.API.MapQuest.Geocoding   build-depends:       base >= 4.7 && < 5-                     , req+                     , goggles >= 0.3+                     , mtl+                     , req >= 1.0                      , bytestring                      , text                      , aeson
src/Web/API/MapQuest.hs view
@@ -9,8 +9,10 @@ -} module Web.API.MapQuest (   -- * Geocoding-  G.request,+  -- ** Running requests+  G.runRequest,     -- ** Parameters+  G.Creds(..),      G.GeoQuery(..),   -- ** Output   G.Coords(..)
src/Web/API/MapQuest/Geocoding.hs view
@@ -1,4 +1,9 @@ {-# language OverloadedStrings, DataKinds, DeriveGeneric #-}+{-# language DeriveFunctor, GeneralizedNewtypeDeriving, FlexibleInstances, MultiParamTypeClasses #-}+{-# language PackageImports #-}+{-# language TypeFamilies #-}+{-# language FlexibleContexts #-}+ {-| Module      : Web.API.MapQuest.Geocoding Description : Geocoding interface@@ -8,11 +13,14 @@ Stability   : experimental Portability : POSIX -}-module Web.API.MapQuest.Geocoding (request, GeoQuery(..), Coords(..))where+module Web.API.MapQuest.Geocoding+  (runRequest, Creds(..), GeoQuery(..), Coords(..))+   where  import Data.List (intersperse) import Data.Monoid ((<>)) +import "mtl" Control.Monad.Reader.Class import Network.HTTP.Req import qualified Data.Text as T import qualified Data.ByteString.Lazy as LBS@@ -24,41 +32,62 @@ import Data.Aeson import Data.Aeson.Types (Parser, parseMaybe) +import Network.Goggles+ -- https://developer.mapquest.com/documentation/geocoding-api/address/get/  apiRootPath :: Url 'Http apiRootPath = http "www.mapquestapi.com" /: "geocoding" /: "v1" /: "address" +-- | The user's API credential (i.e. the MapQuest "consumer key", visible at <https://developer.mapquest.com/user/me/apps> )+newtype Creds = Creds { apiKey :: T.Text } deriving (Eq, Show) +data MapQuest++instance HasCredentials MapQuest where+  type Credentials MapQuest = Creds+  -- type Options MapQuest = ()      -- NB: Options and TokenContent are moved to HasToken in goggles-0.3+  -- type TokenContent MapQuest = ()+  -- tokenFetch = undefined++instance MonadHttp (WebApiM MapQuest) where+  handleHttpException = throwM+ -- example request : -- GET http://www.mapquestapi.com/geocoding/v1/address?key=KEY&location=Washington,DC -instance MonadHttp IO where-  handleHttpException = throwM ++++request ::+     -- T.Text  -- ^ API key (available for free on <https://developer.mapquest.com>)+     GeoQuery -- ^ Query address+  -> WebApiM MapQuest (Maybe (Coords Float))+request q = do+  key <- asks (apiKey . credentials) +  r <- req GET apiRootPath NoReqBody lbsResponse (opts' key)+  return $ decoder1 $ responseBody r where+    opts' k = +      ("key" =: k) <>+      ("outFormat" =: ("json" :: T.Text)) <>+      ("location" =: renderGeoQuery q)+ -- | Call the MapQuest Geocoding API with a given address and extract the coordinates from the parsed result. -- -- Example usage : -- assuming the user has bound /key/ to hold the API key string, the following can be run in a GHCi shell : ----- >>> request key (GQ "Via Irnerio" "Bologna" "Italy")+-- >>> runRequest key (GQ "Via Irnerio" "Bologna" "Italy") -- Just (Coords {lat = 44.49897, long = 11.34503}) ----- >>> request key (GQFree "Hong Kong")+-- >>> runRequest key (GQFree "Ngong Ping 360, Hong Kong") -- Just (Coords {lat = 22.264412, long = 114.16706})--request ::-     T.Text  -- ^ API key (available for free on <https://developer.mapquest.com>)-  -> GeoQuery -- ^ Query address-  -> 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)-+runRequest ::+  Creds -> GeoQuery -> IO (Maybe (Coords Float))+runRequest k q = do+  h <- createHandle k undefined+  evalWebApiIO h (request q)   decoder1 :: LBS.ByteString -> Maybe (Coords Float)@@ -97,7 +126,7 @@   , gqCity :: T.Text   -- ^ City (e.g. \"Bologna\")   , gqCountry :: T.Text -- ^ Country (e.g. \"Italy\")   }-  | GQFree T.Text -- ^ Free-text query (must be a valid address or location)+  | GQFree T.Text -- ^ Free-text query (must be a valid address or location e.g. \"Ngong Ping 360, Hong Kong\")   deriving (Eq, Show)  renderGeoQuery :: GeoQuery -> T.Text