packages feed

ziptastic-client (empty) → 0.1.0.0

raw patch · 5 files changed

+195/−0 lines, 5 filesdep +aesondep +basedep +bytestringsetup-changed

Dependencies added: aeson, base, bytestring, http-api-data, http-client, http-client-tls, iso3166-country-codes, servant, servant-client, text, tz, ziptastic-core

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2017 Elliot Cameron and Ziptastic and Grafted-In LLC++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Elliot Cameron nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,8 @@+# ziptastic-client++[![Haskell Programming Language](https://img.shields.io/badge/language-Haskell-blue.svg)](http://www.haskell.org)+[![BSD3 License](http://img.shields.io/badge/license-BSD3-brightgreen.svg)](https://tldrlegal.com/license/bsd-3-clause-license-%28revised%29)++A convenient and type-safe client library for the [Ziptastic](https://www.getziptastic.com/) API providing forward and reverse geocoding via country, zip code (postal code), latitude, and longitude.++This package is maintained by [Grafted-In](https://www.graftedin.io/).
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Ziptastic/Client.hs view
@@ -0,0 +1,107 @@+{-# LANGUAGE+    DataKinds+  , TypeOperators+  , OverloadedStrings+#-}++-- | This module provides a simple interface to Ziptastic's forward and reverse geocoding API+-- (<https://www.getziptastic.com/>).+--+-- > {-# LANGUAGE OverloadedStrings #-}+-- > import Data.ISO3166_CountryCodes (CountryCode(US))+-- > import Network.HTTP.Client (newManager)+-- > import Network.HTTP.Client.TLS (tlsManagerSettings)+-- > import Ziptastic.Client+-- >+-- > apiKey :: ApiKey+-- > apiKey = "abcdefghijklmnopqrstuvwxyz"+-- >+-- > main :: IO ()+-- > main = do+-- >   manager <- newManager tlsManagerSettings+-- >   print =<< forwardGeocode apiKey manager US "48867"+-- >   print =<< reverseGeocode apiKey manager 42.9934 (-84.1595)+module Ziptastic.Client+  ( ApiKey(..)+  , LocaleInfo(..)+  , Core.LocaleCoords(..)+  , forwardGeocode+  , reverseGeocode+  , reverseGeocodeWithRadius+  ) where++import           Data.ISO3166_CountryCodes (CountryCode)+import           Data.Proxy (Proxy(..))+import           Data.Text (Text)+import           Network.HTTP.Client (Manager)+import           Servant.API ((:<|>)(..))+import           Servant.Client+  ( BaseUrl(..), ClientEnv(..), ClientM, Scheme(..)+  , client, runClientM+  )++import           Ziptastic.Core (ApiKey, ForApi(..), LocaleInfo)+import qualified Ziptastic.Core as Core++forwardGeocode :: ApiKey+               -> Manager      -- ^ HTTP connection manager+               -> CountryCode  -- ^ country+               -> Text         -- ^ postal code+               -> IO (Either String [LocaleInfo])+forwardGeocode apiKey manager countryCode postalCode = strLeft <$> runClientM func (ClientEnv manager baseUrl)+  where func = forwardGeocode' (apiClient apiKey) (ForApi countryCode) postalCode++-- | Performs a reverse geocode lookup at the given coordinates using a default radius of 5000 meters.+reverseGeocode :: ApiKey+               -> Manager -- ^ HTTP connection manager+               -> Double  -- ^ latitude+               -> Double  -- ^ longitude+               -> IO (Either String [LocaleInfo])+reverseGeocode apiKey manager lat long = reverseGeocodeWithRadius apiKey manager lat long 5000++-- | Performs a reverse geocode lookup at the given coordinates using a specified radius in meters.+reverseGeocodeWithRadius :: ApiKey+                         -> Manager -- ^ HTTP connection manager+                         -> Double  -- ^ latitude+                         -> Double  -- ^ longitude+                         -> Int     -- ^ radius (in meters)+                         -> IO (Either String [LocaleInfo])+reverseGeocodeWithRadius apiKey manager lat long radius = strLeft <$> runClientM func (ClientEnv manager baseUrl)+  where func = reverseGeocodeWithRadius' (mkReverseGeocode (apiClient apiKey) lat long) radius++strLeft :: Show e => Either e a -> Either String a+strLeft (Left e)  = Left (show e)+strLeft (Right a) = Right a++data ApiClient = ApiClient+  { forwardGeocode'  :: ForApi CountryCode -> Text -> ClientM [LocaleInfo]+  , mkReverseGeocode :: Double -> Double -> ReverseGeocodeApiClient+  }+data ReverseGeocodeApiClient = ReverseGeocodeApiClient+  { reverseGeocodeWithRadius' :: Int -> ClientM [LocaleInfo]+  , reverseGeocode'           :: ClientM [LocaleInfo]+  }++-- TODO: This boilerplate can be removed with servant-client 0.10 with Servant.Client.Generic.+apiClient :: ApiKey -> ApiClient+apiClient apiKey = ApiClient+  { forwardGeocode'  = forwardGeocodeApi+  , mkReverseGeocode = mkReversGeocodeEndpoints+  }+  where+    forwardGeocodeApi :<|> reverseGeocodeApi = client (Proxy :: Proxy Core.Api) (Just apiKey)+    mkReversGeocodeEndpoints lat long = ReverseGeocodeApiClient+      { reverseGeocodeWithRadius' = withRadius+      , reverseGeocode'           = withDefaultRadius+      }+      where+        withRadius :<|> withDefaultRadius = reverseGeocodeApi lat long++baseUrl :: BaseUrl+baseUrl = BaseUrl+  { baseUrlScheme = if Core.baseUrlIsHttps then Https else Http+  , baseUrlHost   = Core.baseUrlHost+  , baseUrlPort   = Core.baseUrlPort+  , baseUrlPath   = Core.baseUrlPath+  }+
+ ziptastic-client.cabal view
@@ -0,0 +1,48 @@+name:                ziptastic-client+version:             0.1.0.0+synopsis:+  Core Servant specification for the Ziptastic API (https://www.getziptastic.com) for doing forward and reverse geocoding.+description:+  This package provides a type-safe Servant specification for the Ziptastic+  (https://www.getziptastic.com) API for doing forward and reverse geocoding+  via zip/postal code, latitude, and longitude.+  .+  This package is maintained by Grafted-In (https://www.graftedin.io/).+homepage:            https://github.com/Ziptastic/ziptastic-haskell#readme+license:             BSD3+license-file:        LICENSE+author:              Elliot Cameron+maintainer:          elliot@graftedin.io+copyright:           2017 Elliot Cameron and Ziptastic and Grafted-In LLC+category:            Web+build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10+tested-with:         GHC==8.0.2++library+  hs-source-dirs:  src+  exposed-modules: Ziptastic.Client+  build-depends:+      base >= 4.7 && < 5+    , bytestring+    , aeson+    , http-api-data+    , http-client+    , http-client-tls+    , iso3166-country-codes+    , servant+    , servant-client+    , text+    , tz+    , ziptastic-core+  default-language: Haskell2010+  other-extensions:+    DataKinds+    TypeOperators+    OverloadedStrings+  ghc-options: -Wall++source-repository head+  type:     git+  location: https://github.com/Ziptastic/ziptastic-haskell