{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
module Database.Bloodhound.Internal.Client.BHRequest
(
BHRequest (..),
mkFullRequest,
mkSimpleRequest,
Server (..),
Endpoint (..),
mkEndpoint,
withQueries,
getEndpoint,
BHResponse (..),
ParsedEsResponse,
decodeResponse,
eitherDecodeResponse,
parseEsResponse,
parseEsResponseWith,
isVersionConflict,
isSuccess,
isCreated,
statusCodeIs,
EsProtocolException (..),
EsResult (..),
EsResultFound (..),
EsError (..),
Acknowledged (..),
Accepted (..),
)
where
import qualified Blaze.ByteString.Builder as BB
import Control.Applicative as A
import Control.Monad
import Control.Monad.Catch
import Data.Aeson
import qualified Data.ByteString.Lazy as BL
import Data.Ix
import Data.Monoid
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Database.Bloodhound.Internal.Client.Doc
import GHC.Exts
import Network.HTTP.Client
import qualified Network.HTTP.Types.Method as NHTM
import qualified Network.HTTP.Types.Status as NHTS
import qualified Network.HTTP.Types.URI as NHTU
import Prelude hiding (filter, head)
newtype Server = Server Text
deriving stock (Server -> Server -> Bool
(Server -> Server -> Bool)
-> (Server -> Server -> Bool) -> Eq Server
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Server -> Server -> Bool
$c/= :: Server -> Server -> Bool
== :: Server -> Server -> Bool
$c== :: Server -> Server -> Bool
Eq, Int -> Server -> ShowS
[Server] -> ShowS
Server -> String
(Int -> Server -> ShowS)
-> (Server -> String) -> ([Server] -> ShowS) -> Show Server
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Server] -> ShowS
$cshowList :: [Server] -> ShowS
show :: Server -> String
$cshow :: Server -> String
showsPrec :: Int -> Server -> ShowS
$cshowsPrec :: Int -> Server -> ShowS
Show)
deriving newtype (Value -> Parser [Server]
Value -> Parser Server
(Value -> Parser Server)
-> (Value -> Parser [Server]) -> FromJSON Server
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Server]
$cparseJSONList :: Value -> Parser [Server]
parseJSON :: Value -> Parser Server
$cparseJSON :: Value -> Parser Server
FromJSON)
data Endpoint = Endpoint
{ Endpoint -> [Text]
getRawEndpoint :: [Text],
Endpoint -> [(Text, Maybe Text)]
getRawEndpointQueries :: [(Text, Maybe Text)]
}
deriving stock (Endpoint -> Endpoint -> Bool
(Endpoint -> Endpoint -> Bool)
-> (Endpoint -> Endpoint -> Bool) -> Eq Endpoint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Endpoint -> Endpoint -> Bool
$c/= :: Endpoint -> Endpoint -> Bool
== :: Endpoint -> Endpoint -> Bool
$c== :: Endpoint -> Endpoint -> Bool
Eq, Int -> Endpoint -> ShowS
[Endpoint] -> ShowS
Endpoint -> String
(Int -> Endpoint -> ShowS)
-> (Endpoint -> String) -> ([Endpoint] -> ShowS) -> Show Endpoint
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Endpoint] -> ShowS
$cshowList :: [Endpoint] -> ShowS
show :: Endpoint -> String
$cshow :: Endpoint -> String
showsPrec :: Int -> Endpoint -> ShowS
$cshowsPrec :: Int -> Endpoint -> ShowS
Show)
instance IsList Endpoint where
type Item Endpoint = Text
toList :: Endpoint -> [Item Endpoint]
toList = Endpoint -> [Text]
Endpoint -> [Item Endpoint]
getRawEndpoint
fromList :: [Item Endpoint] -> Endpoint
fromList = [Text] -> Endpoint
[Item Endpoint] -> Endpoint
mkEndpoint
mkEndpoint :: [Text] -> Endpoint
mkEndpoint :: [Text] -> Endpoint
mkEndpoint [Text]
urlParts = [Text] -> [(Text, Maybe Text)] -> Endpoint
Endpoint [Text]
urlParts [(Text, Maybe Text)]
forall a. Monoid a => a
mempty
getEndpoint :: Server -> Endpoint -> Text
getEndpoint :: Server -> Endpoint -> Text
getEndpoint (Server Text
serverRoot) Endpoint
endpoint =
Text -> [Text] -> Text
T.intercalate Text
"/" (Text
serverRoot Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: Endpoint -> [Text]
getRawEndpoint Endpoint
endpoint) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
queries
where
queries :: Text
queries = ByteString -> Text
T.decodeUtf8 (ByteString -> Text) -> ByteString -> Text
forall a b. (a -> b) -> a -> b
$ Builder -> ByteString
BB.toByteString (Builder -> ByteString) -> Builder -> ByteString
forall a b. (a -> b) -> a -> b
$ Bool -> [(Text, Maybe Text)] -> Builder
NHTU.renderQueryText Bool
prependQuestionMark ([(Text, Maybe Text)] -> Builder)
-> [(Text, Maybe Text)] -> Builder
forall a b. (a -> b) -> a -> b
$ Endpoint -> [(Text, Maybe Text)]
getRawEndpointQueries Endpoint
endpoint
prependQuestionMark :: Bool
prependQuestionMark = Bool
True
withQueries :: Endpoint -> [(Text, Maybe Text)] -> Endpoint
withQueries :: Endpoint -> [(Text, Maybe Text)] -> Endpoint
withQueries Endpoint
endpoint [(Text, Maybe Text)]
queries = Endpoint
endpoint {getRawEndpointQueries :: [(Text, Maybe Text)]
getRawEndpointQueries = Endpoint -> [(Text, Maybe Text)]
getRawEndpointQueries Endpoint
endpoint [(Text, Maybe Text)]
-> [(Text, Maybe Text)] -> [(Text, Maybe Text)]
forall a. Semigroup a => a -> a -> a
<> [(Text, Maybe Text)]
queries}
data BHRequest responseBody = BHRequest
{ BHRequest responseBody -> ByteString
bhRequestMethod :: NHTM.Method,
BHRequest responseBody -> Endpoint
bhRequestEndpoint :: Endpoint,
BHRequest responseBody -> Maybe ByteString
bhRequestBody :: Maybe BL.ByteString
}
deriving stock (BHRequest responseBody -> BHRequest responseBody -> Bool
(BHRequest responseBody -> BHRequest responseBody -> Bool)
-> (BHRequest responseBody -> BHRequest responseBody -> Bool)
-> Eq (BHRequest responseBody)
forall responseBody.
BHRequest responseBody -> BHRequest responseBody -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BHRequest responseBody -> BHRequest responseBody -> Bool
$c/= :: forall responseBody.
BHRequest responseBody -> BHRequest responseBody -> Bool
== :: BHRequest responseBody -> BHRequest responseBody -> Bool
$c== :: forall responseBody.
BHRequest responseBody -> BHRequest responseBody -> Bool
Eq, Int -> BHRequest responseBody -> ShowS
[BHRequest responseBody] -> ShowS
BHRequest responseBody -> String
(Int -> BHRequest responseBody -> ShowS)
-> (BHRequest responseBody -> String)
-> ([BHRequest responseBody] -> ShowS)
-> Show (BHRequest responseBody)
forall responseBody. Int -> BHRequest responseBody -> ShowS
forall responseBody. [BHRequest responseBody] -> ShowS
forall responseBody. BHRequest responseBody -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BHRequest responseBody] -> ShowS
$cshowList :: forall responseBody. [BHRequest responseBody] -> ShowS
show :: BHRequest responseBody -> String
$cshow :: forall responseBody. BHRequest responseBody -> String
showsPrec :: Int -> BHRequest responseBody -> ShowS
$cshowsPrec :: forall responseBody. Int -> BHRequest responseBody -> ShowS
Show)
mkFullRequest :: NHTM.Method -> Endpoint -> BL.ByteString -> BHRequest body
mkFullRequest :: ByteString -> Endpoint -> ByteString -> BHRequest body
mkFullRequest ByteString
method' Endpoint
endpoint ByteString
body =
BHRequest :: forall responseBody.
ByteString
-> Endpoint -> Maybe ByteString -> BHRequest responseBody
BHRequest
{ bhRequestMethod :: ByteString
bhRequestMethod = ByteString
method',
bhRequestEndpoint :: Endpoint
bhRequestEndpoint = Endpoint
endpoint,
bhRequestBody :: Maybe ByteString
bhRequestBody = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
body
}
mkSimpleRequest :: NHTM.Method -> Endpoint -> BHRequest body
mkSimpleRequest :: ByteString -> Endpoint -> BHRequest body
mkSimpleRequest ByteString
method' Endpoint
endpoint =
BHRequest :: forall responseBody.
ByteString
-> Endpoint -> Maybe ByteString -> BHRequest responseBody
BHRequest
{ bhRequestMethod :: ByteString
bhRequestMethod = ByteString
method',
bhRequestEndpoint :: Endpoint
bhRequestEndpoint = Endpoint
endpoint,
bhRequestBody :: Maybe ByteString
bhRequestBody = Maybe ByteString
forall a. Maybe a
Nothing
}
newtype BHResponse body = BHResponse {BHResponse body -> Response ByteString
getResponse :: Network.HTTP.Client.Response BL.ByteString}
deriving stock (Int -> BHResponse body -> ShowS
[BHResponse body] -> ShowS
BHResponse body -> String
(Int -> BHResponse body -> ShowS)
-> (BHResponse body -> String)
-> ([BHResponse body] -> ShowS)
-> Show (BHResponse body)
forall body. Int -> BHResponse body -> ShowS
forall body. [BHResponse body] -> ShowS
forall body. BHResponse body -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BHResponse body] -> ShowS
$cshowList :: forall body. [BHResponse body] -> ShowS
show :: BHResponse body -> String
$cshow :: forall body. BHResponse body -> String
showsPrec :: Int -> BHResponse body -> ShowS
$cshowsPrec :: forall body. Int -> BHResponse body -> ShowS
Show)
type ParsedEsResponse a = Either EsError a
parseEsResponse ::
( MonadThrow m,
FromJSON body
) =>
BHResponse body ->
m (ParsedEsResponse body)
parseEsResponse :: BHResponse body -> m (ParsedEsResponse body)
parseEsResponse BHResponse body
response
| BHResponse body -> Bool
forall a. BHResponse a -> Bool
isSuccess BHResponse body
response = case ByteString -> Either String body
forall a. FromJSON a => ByteString -> Either String a
eitherDecode ByteString
body of
Right body
a -> ParsedEsResponse body -> m (ParsedEsResponse body)
forall (m :: * -> *) a. Monad m => a -> m a
return (body -> ParsedEsResponse body
forall a b. b -> Either a b
Right body
a)
Left String
err ->
String -> m (ParsedEsResponse body)
tryParseError String
err
| Bool
otherwise = String -> m (ParsedEsResponse body)
tryParseError String
"Non-200 status code"
where
body :: ByteString
body = Response ByteString -> ByteString
forall body. Response body -> body
responseBody (Response ByteString -> ByteString)
-> Response ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ BHResponse body -> Response ByteString
forall body. BHResponse body -> Response ByteString
getResponse BHResponse body
response
tryParseError :: String -> m (ParsedEsResponse body)
tryParseError String
originalError =
case ByteString -> Either String EsError
forall a. FromJSON a => ByteString -> Either String a
eitherDecode ByteString
body of
Right EsError
e -> ParsedEsResponse body -> m (ParsedEsResponse body)
forall (m :: * -> *) a. Monad m => a -> m a
return (EsError -> ParsedEsResponse body
forall a b. a -> Either a b
Left EsError
e)
Left String
err -> String -> m (ParsedEsResponse body)
explode (String
"Original error was: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
originalError String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" Error parse failure was: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
err)
explode :: String -> m (ParsedEsResponse body)
explode String
errorMsg = EsProtocolException -> m (ParsedEsResponse body)
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (EsProtocolException -> m (ParsedEsResponse body))
-> EsProtocolException -> m (ParsedEsResponse body)
forall a b. (a -> b) -> a -> b
$ Text -> ByteString -> EsProtocolException
EsProtocolException (String -> Text
T.pack String
errorMsg) ByteString
body
parseEsResponseWith ::
( MonadThrow m,
FromJSON body
) =>
(body -> Either String parsed) ->
BHResponse body ->
m parsed
parseEsResponseWith :: (body -> Either String parsed) -> BHResponse body -> m parsed
parseEsResponseWith body -> Either String parsed
parser BHResponse body
response =
case ByteString -> Either String body
forall a. FromJSON a => ByteString -> Either String a
eitherDecode ByteString
body of
Left String
e -> String -> m parsed
explode String
e
Right body
parsed ->
case body -> Either String parsed
parser body
parsed of
Right parsed
a -> parsed -> m parsed
forall (m :: * -> *) a. Monad m => a -> m a
return parsed
a
Left String
e -> String -> m parsed
explode String
e
where
body :: ByteString
body = Response ByteString -> ByteString
forall body. Response body -> body
responseBody (Response ByteString -> ByteString)
-> Response ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ BHResponse body -> Response ByteString
forall body. BHResponse body -> Response ByteString
getResponse BHResponse body
response
explode :: String -> m parsed
explode String
errorMsg = EsProtocolException -> m parsed
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (EsProtocolException -> m parsed)
-> EsProtocolException -> m parsed
forall a b. (a -> b) -> a -> b
$ Text -> ByteString -> EsProtocolException
EsProtocolException (String -> Text
T.pack String
errorMsg) ByteString
body
decodeResponse ::
FromJSON a =>
BHResponse a ->
Maybe a
decodeResponse :: BHResponse a -> Maybe a
decodeResponse = ByteString -> Maybe a
forall a. FromJSON a => ByteString -> Maybe a
decode (ByteString -> Maybe a)
-> (BHResponse a -> ByteString) -> BHResponse a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response ByteString -> ByteString
forall body. Response body -> body
responseBody (Response ByteString -> ByteString)
-> (BHResponse a -> Response ByteString)
-> BHResponse a
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BHResponse a -> Response ByteString
forall body. BHResponse body -> Response ByteString
getResponse
eitherDecodeResponse ::
FromJSON a =>
BHResponse a ->
Either String a
eitherDecodeResponse :: BHResponse a -> Either String a
eitherDecodeResponse = ByteString -> Either String a
forall a. FromJSON a => ByteString -> Either String a
eitherDecode (ByteString -> Either String a)
-> (BHResponse a -> ByteString) -> BHResponse a -> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response ByteString -> ByteString
forall body. Response body -> body
responseBody (Response ByteString -> ByteString)
-> (BHResponse a -> Response ByteString)
-> BHResponse a
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BHResponse a -> Response ByteString
forall body. BHResponse body -> Response ByteString
getResponse
isVersionConflict :: BHResponse a -> Bool
isVersionConflict :: BHResponse a -> Bool
isVersionConflict = (Int -> Bool) -> BHResponse a -> Bool
forall a. (Int -> Bool) -> BHResponse a -> Bool
statusCheck (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
409)
isSuccess :: BHResponse a -> Bool
isSuccess :: BHResponse a -> Bool
isSuccess = (Int, Int) -> BHResponse a -> Bool
forall body. (Int, Int) -> BHResponse body -> Bool
statusCodeIs (Int
200, Int
299)
isCreated :: BHResponse a -> Bool
isCreated :: BHResponse a -> Bool
isCreated = (Int -> Bool) -> BHResponse a -> Bool
forall a. (Int -> Bool) -> BHResponse a -> Bool
statusCheck (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
201)
statusCheck :: (Int -> Bool) -> BHResponse a -> Bool
statusCheck :: (Int -> Bool) -> BHResponse a -> Bool
statusCheck Int -> Bool
prd = Int -> Bool
prd (Int -> Bool) -> (BHResponse a -> Int) -> BHResponse a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Status -> Int
NHTS.statusCode (Status -> Int) -> (BHResponse a -> Status) -> BHResponse a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response ByteString -> Status
forall body. Response body -> Status
responseStatus (Response ByteString -> Status)
-> (BHResponse a -> Response ByteString) -> BHResponse a -> Status
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BHResponse a -> Response ByteString
forall body. BHResponse body -> Response ByteString
getResponse
statusCodeIs :: (Int, Int) -> BHResponse body -> Bool
statusCodeIs :: (Int, Int) -> BHResponse body -> Bool
statusCodeIs (Int, Int)
r BHResponse body
resp = (Int, Int) -> Int -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (Int, Int)
r (Int -> Bool) -> Int -> Bool
forall a b. (a -> b) -> a -> b
$ Status -> Int
NHTS.statusCode (Response ByteString -> Status
forall body. Response body -> Status
responseStatus (Response ByteString -> Status) -> Response ByteString -> Status
forall a b. (a -> b) -> a -> b
$ BHResponse body -> Response ByteString
forall body. BHResponse body -> Response ByteString
getResponse BHResponse body
resp)
data EsResult a = EsResult
{ EsResult a -> Text
_index :: Text,
EsResult a -> Text
_type :: Text,
EsResult a -> Text
_id :: Text,
EsResult a -> Maybe (EsResultFound a)
foundResult :: Maybe (EsResultFound a)
}
deriving (EsResult a -> EsResult a -> Bool
(EsResult a -> EsResult a -> Bool)
-> (EsResult a -> EsResult a -> Bool) -> Eq (EsResult a)
forall a. Eq a => EsResult a -> EsResult a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EsResult a -> EsResult a -> Bool
$c/= :: forall a. Eq a => EsResult a -> EsResult a -> Bool
== :: EsResult a -> EsResult a -> Bool
$c== :: forall a. Eq a => EsResult a -> EsResult a -> Bool
Eq, Int -> EsResult a -> ShowS
[EsResult a] -> ShowS
EsResult a -> String
(Int -> EsResult a -> ShowS)
-> (EsResult a -> String)
-> ([EsResult a] -> ShowS)
-> Show (EsResult a)
forall a. Show a => Int -> EsResult a -> ShowS
forall a. Show a => [EsResult a] -> ShowS
forall a. Show a => EsResult a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EsResult a] -> ShowS
$cshowList :: forall a. Show a => [EsResult a] -> ShowS
show :: EsResult a -> String
$cshow :: forall a. Show a => EsResult a -> String
showsPrec :: Int -> EsResult a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> EsResult a -> ShowS
Show)
data EsResultFound a = EsResultFound
{ EsResultFound a -> DocVersion
_version :: DocVersion,
EsResultFound a -> a
_source :: a
}
deriving (EsResultFound a -> EsResultFound a -> Bool
(EsResultFound a -> EsResultFound a -> Bool)
-> (EsResultFound a -> EsResultFound a -> Bool)
-> Eq (EsResultFound a)
forall a. Eq a => EsResultFound a -> EsResultFound a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EsResultFound a -> EsResultFound a -> Bool
$c/= :: forall a. Eq a => EsResultFound a -> EsResultFound a -> Bool
== :: EsResultFound a -> EsResultFound a -> Bool
$c== :: forall a. Eq a => EsResultFound a -> EsResultFound a -> Bool
Eq, Int -> EsResultFound a -> ShowS
[EsResultFound a] -> ShowS
EsResultFound a -> String
(Int -> EsResultFound a -> ShowS)
-> (EsResultFound a -> String)
-> ([EsResultFound a] -> ShowS)
-> Show (EsResultFound a)
forall a. Show a => Int -> EsResultFound a -> ShowS
forall a. Show a => [EsResultFound a] -> ShowS
forall a. Show a => EsResultFound a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EsResultFound a] -> ShowS
$cshowList :: forall a. Show a => [EsResultFound a] -> ShowS
show :: EsResultFound a -> String
$cshow :: forall a. Show a => EsResultFound a -> String
showsPrec :: Int -> EsResultFound a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> EsResultFound a -> ShowS
Show)
instance (FromJSON a) => FromJSON (EsResult a) where
parseJSON :: Value -> Parser (EsResult a)
parseJSON jsonVal :: Value
jsonVal@(Object Object
v) = do
Bool
found <- Object
v Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"found" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
False
Maybe (EsResultFound a)
fr <-
if Bool
found
then Value -> Parser (Maybe (EsResultFound a))
forall a. FromJSON a => Value -> Parser a
parseJSON Value
jsonVal
else Maybe (EsResultFound a) -> Parser (Maybe (EsResultFound a))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (EsResultFound a)
forall a. Maybe a
Nothing
Text -> Text -> Text -> Maybe (EsResultFound a) -> EsResult a
forall a.
Text -> Text -> Text -> Maybe (EsResultFound a) -> EsResult a
EsResult (Text -> Text -> Text -> Maybe (EsResultFound a) -> EsResult a)
-> Parser Text
-> Parser (Text -> Text -> Maybe (EsResultFound a) -> EsResult a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"_index"
Parser (Text -> Text -> Maybe (EsResultFound a) -> EsResult a)
-> Parser Text
-> Parser (Text -> Maybe (EsResultFound a) -> EsResult a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"_type"
Parser (Text -> Maybe (EsResultFound a) -> EsResult a)
-> Parser Text -> Parser (Maybe (EsResultFound a) -> EsResult a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"_id"
Parser (Maybe (EsResultFound a) -> EsResult a)
-> Parser (Maybe (EsResultFound a)) -> Parser (EsResult a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe (EsResultFound a) -> Parser (Maybe (EsResultFound a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (EsResultFound a)
fr
parseJSON Value
_ = Parser (EsResult a)
forall (f :: * -> *) a. Alternative f => f a
empty
instance (FromJSON a) => FromJSON (EsResultFound a) where
parseJSON :: Value -> Parser (EsResultFound a)
parseJSON (Object Object
v) =
DocVersion -> a -> EsResultFound a
forall a. DocVersion -> a -> EsResultFound a
EsResultFound
(DocVersion -> a -> EsResultFound a)
-> Parser DocVersion -> Parser (a -> EsResultFound a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser DocVersion
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"_version"
Parser (a -> EsResultFound a)
-> Parser a -> Parser (EsResultFound a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser a
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"_source"
parseJSON Value
_ = Parser (EsResultFound a)
forall (f :: * -> *) a. Alternative f => f a
empty
data EsError = EsError
{ EsError -> Int
errorStatus :: Int,
EsError -> Text
errorMessage :: Text
}
deriving (EsError -> EsError -> Bool
(EsError -> EsError -> Bool)
-> (EsError -> EsError -> Bool) -> Eq EsError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EsError -> EsError -> Bool
$c/= :: EsError -> EsError -> Bool
== :: EsError -> EsError -> Bool
$c== :: EsError -> EsError -> Bool
Eq, Int -> EsError -> ShowS
[EsError] -> ShowS
EsError -> String
(Int -> EsError -> ShowS)
-> (EsError -> String) -> ([EsError] -> ShowS) -> Show EsError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EsError] -> ShowS
$cshowList :: [EsError] -> ShowS
show :: EsError -> String
$cshow :: EsError -> String
showsPrec :: Int -> EsError -> ShowS
$cshowsPrec :: Int -> EsError -> ShowS
Show)
instance FromJSON EsError where
parseJSON :: Value -> Parser EsError
parseJSON (Object Object
v) =
Int -> Text -> EsError
EsError
(Int -> Text -> EsError) -> Parser Int -> Parser (Text -> EsError)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"status"
Parser (Text -> EsError) -> Parser Text -> Parser EsError
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"error" Parser Text -> Parser Text -> Parser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Object
v Object -> Key -> Parser Object
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"error" Parser Object -> (Object -> Parser Text) -> Parser Text
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"reason")))
parseJSON Value
_ = Parser EsError
forall (f :: * -> *) a. Alternative f => f a
empty
data EsProtocolException = EsProtocolException
{ EsProtocolException -> Text
esProtoExMessage :: !Text,
EsProtocolException -> ByteString
esProtoExResponse :: !BL.ByteString
}
deriving (EsProtocolException -> EsProtocolException -> Bool
(EsProtocolException -> EsProtocolException -> Bool)
-> (EsProtocolException -> EsProtocolException -> Bool)
-> Eq EsProtocolException
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EsProtocolException -> EsProtocolException -> Bool
$c/= :: EsProtocolException -> EsProtocolException -> Bool
== :: EsProtocolException -> EsProtocolException -> Bool
$c== :: EsProtocolException -> EsProtocolException -> Bool
Eq, Int -> EsProtocolException -> ShowS
[EsProtocolException] -> ShowS
EsProtocolException -> String
(Int -> EsProtocolException -> ShowS)
-> (EsProtocolException -> String)
-> ([EsProtocolException] -> ShowS)
-> Show EsProtocolException
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EsProtocolException] -> ShowS
$cshowList :: [EsProtocolException] -> ShowS
show :: EsProtocolException -> String
$cshow :: EsProtocolException -> String
showsPrec :: Int -> EsProtocolException -> ShowS
$cshowsPrec :: Int -> EsProtocolException -> ShowS
Show)
instance Exception EsProtocolException
newtype Acknowledged = Acknowledged {Acknowledged -> Bool
isAcknowledged :: Bool}
deriving stock (Acknowledged -> Acknowledged -> Bool
(Acknowledged -> Acknowledged -> Bool)
-> (Acknowledged -> Acknowledged -> Bool) -> Eq Acknowledged
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Acknowledged -> Acknowledged -> Bool
$c/= :: Acknowledged -> Acknowledged -> Bool
== :: Acknowledged -> Acknowledged -> Bool
$c== :: Acknowledged -> Acknowledged -> Bool
Eq, Int -> Acknowledged -> ShowS
[Acknowledged] -> ShowS
Acknowledged -> String
(Int -> Acknowledged -> ShowS)
-> (Acknowledged -> String)
-> ([Acknowledged] -> ShowS)
-> Show Acknowledged
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Acknowledged] -> ShowS
$cshowList :: [Acknowledged] -> ShowS
show :: Acknowledged -> String
$cshow :: Acknowledged -> String
showsPrec :: Int -> Acknowledged -> ShowS
$cshowsPrec :: Int -> Acknowledged -> ShowS
Show)
instance FromJSON Acknowledged where
parseJSON :: Value -> Parser Acknowledged
parseJSON =
String
-> (Object -> Parser Acknowledged) -> Value -> Parser Acknowledged
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject
String
"Acknowledged"
(Object -> Key -> Parser Acknowledged
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"acknowledged")
newtype Accepted = Accepted {Accepted -> Bool
isAccepted :: Bool}
deriving stock (Accepted -> Accepted -> Bool
(Accepted -> Accepted -> Bool)
-> (Accepted -> Accepted -> Bool) -> Eq Accepted
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Accepted -> Accepted -> Bool
$c/= :: Accepted -> Accepted -> Bool
== :: Accepted -> Accepted -> Bool
$c== :: Accepted -> Accepted -> Bool
Eq, Int -> Accepted -> ShowS
[Accepted] -> ShowS
Accepted -> String
(Int -> Accepted -> ShowS)
-> (Accepted -> String) -> ([Accepted] -> ShowS) -> Show Accepted
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Accepted] -> ShowS
$cshowList :: [Accepted] -> ShowS
show :: Accepted -> String
$cshow :: Accepted -> String
showsPrec :: Int -> Accepted -> ShowS
$cshowsPrec :: Int -> Accepted -> ShowS
Show)
instance FromJSON Accepted where
parseJSON :: Value -> Parser Accepted
parseJSON =
String -> (Object -> Parser Accepted) -> Value -> Parser Accepted
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject
String
"Accepted"
(Object -> Key -> Parser Accepted
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"accepted")