git.iko.soy/typeable/bloodhound
on master help sign in
Merge pull request #84 from Soostone/request-hook

Add Request hook

commitc3df59771b1a59b8b25d9e04b328cfbaa79b7bdd authorChris Allen <cma@bitemyapp.com> dateSun 27 Dec 2015, 07:33 (11 years ago) parent3fe391f218b407
5 files changed+120−54
bloodhound.cabal+1 src/Database/Bloodhound/Client.hs+20−9 src/Database/Bloodhound/Types.hs+39−41 src/Database/Bloodhound/Types/Internal.hs+56 tests/tests.hs+4−4
bloodhound.cabal+1
@@ -27,6 +27,7 @@ library
2727 Database.Bloodhound.Client
2828 Database.Bloodhound.Types
2929 Database.Bloodhound.Types.Class
30+ Database.Bloodhound.Types.Internal
3031 hs-source-dirs: src
3132 build-depends: base >= 4.3 && <5,
3233 bytestring >= 0.10.0 && <0.11,
src/Database/Bloodhound/Client.hs+20−9
@@ -58,6 +58,8 @@ module Database.Bloodhound.Client
5858 , getStatus
5959 , encodeBulkOperations
6060 , encodeBulkOperation
61+ -- * Authentication
62+ , basicAuthHook
6163 -- * Reply-handling tools
6264 , isVersionConflict
6365 , isSuccess
@@ -175,10 +177,11 @@ dispatch :: MonadBH m => Method -> Text -> Maybe L.ByteString
175177 -> m Reply
176178dispatch dMethod url body = do
177179 initReq <- liftIO $ parseUrl' url
180+ reqHook <- bhRequestHook <$> getBHEnv
178181 let reqBody = RequestBodyLBS $ fromMaybe emptyBody body
179− let req = initReq { method = dMethod
180− , requestBody = reqBody
181− , checkStatus = \_ _ _ -> Nothing}
182+ req <- liftIO $ reqHook $ initReq { method = dMethod
183+ , requestBody = reqBody
184+ , checkStatus = \_ _ _ -> Nothing}
182185 mgr <- bhManager <$> getBHEnv
183186 liftIO $ httpLbs req mgr
184187
@@ -222,8 +225,7 @@ bindM2 f ma mb = join (f <$> ma <*> mb)
222225withBH :: ManagerSettings -> Server -> BH IO a -> IO a
223226withBH ms s f = do
224227 mgr <- newManager ms
225− let env = BHEnv { bhServer = s
226− , bhManager = mgr }
228+ let env = mkBHEnv s mgr
227229 runBH env f
228230
229231-- Shortcut functions for HTTP methods
@@ -249,11 +251,9 @@ post = dispatch NHTM.methodPost
249251-- Just 200
250252getStatus :: MonadBH m => m (Maybe Status)
251253getStatus = do
252− url <- joinPath []
253− request <- liftIO $ parseUrl' url
254− mgr <- bhManager <$> getBHEnv
255− response <- liftIO $ httpLbs request mgr
254+ response <- get =<< url
256255 return $ decode (responseBody response)
256+ where url = joinPath []
257257
258258-- | 'createIndex' will create an index given a 'Server', 'IndexSettings', and an 'IndexName'.
259259--
@@ -785,3 +785,14 @@ isCreated = statusCheck (== 201)
785785
786786statusCheck :: (Int -> Bool) -> Reply -> Bool
787787statusCheck prd = prd . NHTS.statusCode . responseStatus
788+
789+-- | This is a hook that can be set via the 'bhRequestHook' function
790+-- that will authenticate all requests using an HTTP Basic
791+-- Authentication header. Note that it is *strongly* recommended that
792+-- this option only be used over an SSL connection.
793+--
794+-- >> (mkBHEnv myServer myManager) { bhRequestHook = basicAuthHook (EsUsername "myuser") (EsPassword "mypass") }
795+basicAuthHook :: Monad m => EsUsername -> EsPassword -> Request -> m Request
796+basicAuthHook (EsUsername u) (EsPassword p) = return . applyBasicAuth u' p'
797+ where u' = T.encodeUtf8 u
798+ p' = T.encodeUtf8 p
src/Database/Bloodhound/Types.hs+39−41
@@ -51,7 +51,11 @@ module Database.Bloodhound.Types
5151 , omitNulls
5252 , BH(..)
5353 , runBH
54− , BHEnv(..)
54+ , BHEnv
55+ , bhServer
56+ , bhManager
57+ , bhRequestHook
58+ , mkBHEnv
5559 , MonadBH(..)
5660 , Version(..)
5761 , Status(..)
@@ -258,6 +262,9 @@ module Database.Bloodhound.Types
258262 , TermsResult(..)
259263 , DateHistogramResult(..)
260264 , DateRangeResult(..)
265+
266+ , EsUsername(..)
267+ , EsPassword(..)
261268 ) where
262269
263270import Control.Applicative
@@ -267,30 +274,31 @@ import Control.Monad.Reader
267274import Control.Monad.State
268275import Control.Monad.Writer
269276import Data.Aeson
270−import Data.Aeson.Types (Pair, Parser, emptyObject,
271− parseMaybe)
272−import qualified Data.ByteString.Lazy.Char8 as L
277+import Data.Aeson.Types (Pair, Parser, emptyObject,
278+ parseMaybe)
279+import qualified Data.ByteString.Lazy.Char8 as L
273280import Data.Char
274−import Data.Hashable (Hashable)
275−import qualified Data.HashMap.Strict as HM
276−import Data.List (foldl', nub)
277−import Data.List.NonEmpty (NonEmpty (..), toList)
278−import qualified Data.Map.Strict as M
281+import Data.Hashable (Hashable)
282+import qualified Data.HashMap.Strict as HM
283+import Data.List (foldl', nub)
284+import Data.List.NonEmpty (NonEmpty (..), toList)
285+import qualified Data.Map.Strict as M
279286import Data.Maybe
280−import Data.Text (Text)
281−import qualified Data.Text as T
287+import Data.Text (Text)
288+import qualified Data.Text as T
282289import Data.Time.Calendar
283−import Data.Time.Clock (NominalDiffTime, UTCTime)
290+import Data.Time.Clock (NominalDiffTime, UTCTime)
284291import Data.Time.Clock.POSIX
285−import qualified Data.Traversable as DT
286−import Data.Typeable (Typeable)
287−import qualified Data.Vector as V
292+import qualified Data.Traversable as DT
293+import Data.Typeable (Typeable)
294+import qualified Data.Vector as V
288295import GHC.Enum
289−import GHC.Generics (Generic)
296+import GHC.Generics (Generic)
290297import Network.HTTP.Client
291−import qualified Network.HTTP.Types.Method as NHTM
298+import qualified Network.HTTP.Types.Method as NHTM
292299
293300import Database.Bloodhound.Types.Class
301+import Database.Bloodhound.Types.Internal
294302
295303-- $setup
296304-- >>> :set -XOverloadedStrings
@@ -303,20 +311,12 @@ import Database.Bloodhound.Types.Class
303311-- defaultIndexSettings is exported by Database.Bloodhound as well
304312-- no trailing slashes in servers, library handles building the path.
305313
306−{-| Common environment for Elasticsearch calls. Connections will be
307− pipelined according to the provided HTTP connection manager.
308−-}
309−data BHEnv = BHEnv { bhServer :: Server
310− , bhManager :: Manager
311− }
312−
313−{-| All API calls to Elasticsearch operate within
314− MonadBH. The idea is that it can be easily embedded in your
315− own monad transformer stack. A default instance for a ReaderT and
316− alias 'BH' is provided for the simple case.
317−-}
318−class (Functor m, Applicative m, MonadIO m) => MonadBH m where
319− getBHEnv :: m BHEnv
314+-- | Create a 'BHEnv' with all optional fields defaulted. HTTP hook
315+-- will be a noop. You can use the exported fields to customize it further, e.g.:
316+--
317+-- >> (mkBHEnv myServer myManager) { bhRequestHook = customHook }
318+mkBHEnv :: Server -> Manager -> BHEnv
319+mkBHEnv s m = BHEnv s m return
320320
321321newtype BH m a = BH {
322322 unBH :: ReaderT BHEnv m a
@@ -345,10 +345,6 @@ instance (MonadReader r m) => MonadReader r (BH m) where
345345instance (Functor m, Applicative m, MonadIO m) => MonadBH (BH m) where
346346 getBHEnv = BH getBHEnv
347347
348−
349−instance (Functor m, Applicative m, MonadIO m) => MonadBH (ReaderT BHEnv m) where
350− getBHEnv = ask
351−
352348runBH :: BHEnv -> BH m a -> m a
353349runBH e f = runReaderT (unBH f) e
354350
@@ -479,9 +475,9 @@ data CompoundFormat = CompoundFileFormat Bool
479475
480476newtype NominalDiffTimeJSON = NominalDiffTimeJSON { ndtJSON :: NominalDiffTime }
481477
482−data IndexSettingsSummary = IndexSettingsSummary { sSummaryIndexName :: IndexName
478+data IndexSettingsSummary = IndexSettingsSummary { sSummaryIndexName :: IndexName
483479 , sSummaryFixedSettings :: IndexSettings
484− , sSummaryUpdateable :: [UpdatableIndexSetting]}
480+ , sSummaryUpdateable :: [UpdatableIndexSetting]}
485481 deriving (Eq, Show, Generic, Typeable)
486482
487483{-| 'Reply' and 'Method' are type synonyms from 'Network.HTTP.Types.Method.Method' -}
@@ -764,10 +760,6 @@ newtype ShardCount = ShardCount Int deriving (Eq, Show, Generic, ToJSON, Typeabl
764760-}
765761newtype ReplicaCount = ReplicaCount Int deriving (Eq, Show, Generic, ToJSON, Typeable)
766762
767−{-| 'Server' is used with the client functions to point at the ES instance
768−-}
769−newtype Server = Server Text deriving (Eq, Show, Generic, Typeable)
770−
771763{-| 'IndexName' is used to describe which index to query/create/delete
772764-}
773765newtype IndexName = IndexName Text deriving (Eq, Generic, Show, ToJSON, FromJSON, Typeable)
@@ -3544,3 +3536,9 @@ instance Enum DocVersion where
35443536 fromEnum = docVersionNumber
35453537 enumFrom = boundedEnumFrom
35463538 enumFromThen = boundedEnumFromThen
3539+
3540+-- | Username type used for HTTP Basic authentication. See 'basicAuthHook'.
3541+newtype EsUsername = EsUsername { esUsername :: Text } deriving (Show, Eq)
3542+
3543+-- | Password type used for HTTP Basic authentication. See 'basicAuthHook'.
3544+newtype EsPassword = EsPassword { esPassword :: Text } deriving (Show, Eq)
src/Database/Bloodhound/Types/Internal.hsnew file+56
@@ -0,0 +1,56 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+
6+-------------------------------------------------------------------------------
7+-- |
8+-- Module : Database.Bloodhound.Types.Internal
9+-- Copyright : (C) 2014 Chris Allen
10+-- License : BSD-style (see the file LICENSE)
11+-- Maintainer : Chris Allen <cma@bitemyapp.com>
12+-- Stability : provisional
13+-- Portability : DeriveGeneric, RecordWildCards
14+--
15+-- Internal data types for Bloodhound. These types may change without
16+-- notice so import at your own risk.
17+-------------------------------------------------------------------------------
18+module Database.Bloodhound.Types.Internal
19+ ( BHEnv(..)
20+ , Server(..)
21+ , MonadBH(..)
22+ ) where
23+
24+
25+import Control.Applicative
26+import Control.Monad.Reader
27+import Data.Text (Text)
28+import Data.Typeable (Typeable)
29+import GHC.Generics (Generic)
30+import Network.HTTP.Client
31+
32+{-| Common environment for Elasticsearch calls. Connections will be
33+ pipelined according to the provided HTTP connection manager.
34+-}
35+data BHEnv = BHEnv { bhServer :: Server
36+ , bhManager :: Manager
37+ , bhRequestHook :: Request -> IO Request
38+ -- ^ Low-level hook that is run before every request is sent. Used to implement custom authentication strategies. Defaults to 'return' with 'mkBHEnv'.
39+ }
40+
41+instance (Functor m, Applicative m, MonadIO m) => MonadBH (ReaderT BHEnv m) where
42+ getBHEnv = ask
43+
44+{-| 'Server' is used with the client functions to point at the ES instance
45+-}
46+newtype Server = Server Text deriving (Eq, Show, Generic, Typeable)
47+
48+{-| All API calls to Elasticsearch operate within
49+ MonadBH
50+ . The idea is that it can be easily embedded in your
51+ own monad transformer stack. A default instance for a ReaderT and
52+ alias 'BH' is provided for the simple case.
53+-}
54+class (Functor m, Applicative m, MonadIO m) => MonadBH m where
55+ getBHEnv :: m BHEnv
56+
tests/tests.hs+4−4
@@ -259,7 +259,7 @@ searchExpectNoResults search = do
259259
260260searchExpectAggs :: Search -> BH IO ()
261261searchExpectAggs search = do
262− reply <- searchAll search
262+ reply <- searchByIndex testIndex search
263263 let isEmpty x = return (M.null x)
264264 let result = decode (responseBody reply) :: Maybe (SearchResult Tweet)
265265 liftIO $
@@ -268,7 +268,7 @@ searchExpectAggs search = do
268268searchValidBucketAgg :: (BucketAggregation a, FromJSON a, Show a) =>
269269 Search -> Text -> (Text -> AggregationResults -> Maybe (Bucket a)) -> BH IO ()
270270searchValidBucketAgg search aggKey extractor = do
271− reply <- searchAll search
271+ reply <- searchByIndex testIndex search
272272 let bucketDocs = docCount . head . buckets
273273 let result = decode (responseBody reply) :: Maybe (SearchResult Tweet)
274274 let count = result >>= aggregations >>= extractor aggKey >>= \x -> return (bucketDocs x)
@@ -293,8 +293,8 @@ searchExpectSource src expected = do
293293 _ <- insertData
294294 let query = QueryMatchQuery $ mkMatchQuery (FieldName "_all") (QueryString "haskell")
295295 let search = (mkSearch (Just query) Nothing) { source = Just src }
296− reply <- searchAll search
297− result <- parseEsResponse reply-- :: Either EsError (SearchResult Value)
296+ reply <- searchByIndex testIndex search
297+ result <- parseEsResponse reply
298298 let value = grabFirst result
299299 liftIO $
300300 value `shouldBe` expected