2018-09-05 14:26:46 +03:00
|
|
|
module Hasura.Events.HTTP
|
2019-02-14 10:37:59 +03:00
|
|
|
( HTTPErr(..)
|
2018-10-26 19:28:03 +03:00
|
|
|
, HTTPResp(..)
|
2018-09-05 14:26:46 +03:00
|
|
|
, runHTTP
|
|
|
|
, isNetworkError
|
|
|
|
, isNetworkErrorHC
|
|
|
|
, ExtraContext(..)
|
|
|
|
) where
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
import Data.Either
|
|
|
|
|
2019-07-11 08:37:06 +03:00
|
|
|
import qualified Data.Aeson as J
|
|
|
|
import qualified Data.Aeson.Casing as J
|
|
|
|
import qualified Data.Aeson.TH as J
|
|
|
|
import qualified Data.ByteString.Lazy as B
|
|
|
|
import qualified Data.CaseInsensitive as CI
|
|
|
|
import qualified Data.TByteString as TBS
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.Encoding as TE
|
|
|
|
import qualified Data.Text.Encoding.Error as TE
|
|
|
|
import qualified Data.Time.Clock as Time
|
|
|
|
import qualified Network.HTTP.Client as HTTP
|
|
|
|
import qualified Network.HTTP.Types as HTTP
|
|
|
|
|
|
|
|
import Control.Exception (try)
|
|
|
|
import Control.Monad.IO.Class (MonadIO, liftIO)
|
|
|
|
import Control.Monad.Reader (MonadReader)
|
2018-09-05 14:26:46 +03:00
|
|
|
import Data.Has
|
|
|
|
import Hasura.Logging
|
|
|
|
import Hasura.Prelude
|
2018-11-23 16:02:46 +03:00
|
|
|
import Hasura.RQL.DDL.Headers
|
2019-03-25 20:10:52 +03:00
|
|
|
import Hasura.RQL.Types.EventTrigger
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
data ExtraContext
|
|
|
|
= ExtraContext
|
|
|
|
{ elEventCreatedAt :: Time.UTCTime
|
2019-03-25 20:10:52 +03:00
|
|
|
, elEventId :: EventId
|
2018-09-05 14:26:46 +03:00
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
$(J.deriveJSON (J.aesonDrop 2 J.snakeCase){J.omitNothingFields=True} ''ExtraContext)
|
|
|
|
|
2018-10-26 19:28:03 +03:00
|
|
|
data HTTPResp
|
|
|
|
= HTTPResp
|
|
|
|
{ hrsStatus :: !Int
|
|
|
|
, hrsHeaders :: ![HeaderConf]
|
|
|
|
, hrsBody :: !TBS.TByteString
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
$(J.deriveToJSON (J.aesonDrop 3 J.snakeCase){J.omitNothingFields=True} ''HTTPResp)
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
instance ToEngineLog HTTPResp Hasura where
|
|
|
|
toEngineLog resp = (LevelInfo, eventTriggerLogType, J.toJSON resp)
|
2018-10-26 19:28:03 +03:00
|
|
|
|
2019-02-14 10:37:59 +03:00
|
|
|
mkHTTPResp :: HTTP.Response B.ByteString -> HTTPResp
|
2018-10-26 19:28:03 +03:00
|
|
|
mkHTTPResp resp =
|
|
|
|
HTTPResp
|
2019-02-14 10:37:59 +03:00
|
|
|
{ hrsStatus = HTTP.statusCode $ HTTP.responseStatus resp
|
|
|
|
, hrsHeaders = map decodeHeader $ HTTP.responseHeaders resp
|
|
|
|
, hrsBody = TBS.fromLBS $ HTTP.responseBody resp
|
|
|
|
}
|
2018-10-26 19:28:03 +03:00
|
|
|
where
|
|
|
|
decodeBS = TE.decodeUtf8With TE.lenientDecode
|
|
|
|
decodeHeader (hdrName, hdrVal)
|
|
|
|
= HeaderConf (decodeBS $ CI.original hdrName) (HVValue (decodeBS hdrVal))
|
|
|
|
|
|
|
|
data HTTPRespExtra
|
|
|
|
= HTTPRespExtra
|
|
|
|
{ _hreResponse :: HTTPResp
|
|
|
|
, _hreContext :: Maybe ExtraContext
|
|
|
|
}
|
|
|
|
|
|
|
|
$(J.deriveToJSON (J.aesonDrop 4 J.snakeCase){J.omitNothingFields=True} ''HTTPRespExtra)
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
instance ToEngineLog HTTPRespExtra Hasura where
|
|
|
|
toEngineLog resp = (LevelInfo, eventTriggerLogType, J.toJSON resp)
|
2018-10-26 19:28:03 +03:00
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
data HTTPErr
|
2019-02-14 10:37:59 +03:00
|
|
|
= HClient !HTTP.HttpException
|
|
|
|
| HParse !HTTP.Status !String
|
2018-10-26 19:28:03 +03:00
|
|
|
| HStatus !HTTPResp
|
2018-09-05 14:26:46 +03:00
|
|
|
| HOther !String
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
instance J.ToJSON HTTPErr where
|
|
|
|
toJSON err = toObj $ case err of
|
|
|
|
(HClient e) -> ("client", J.toJSON $ show e)
|
|
|
|
(HParse st e) ->
|
|
|
|
( "parse"
|
2019-02-14 10:37:59 +03:00
|
|
|
, J.toJSON (HTTP.statusCode st, show e)
|
2018-09-05 14:26:46 +03:00
|
|
|
)
|
2018-10-26 19:28:03 +03:00
|
|
|
(HStatus resp) ->
|
|
|
|
("status", J.toJSON resp)
|
2018-09-05 14:26:46 +03:00
|
|
|
(HOther e) -> ("internal", J.toJSON $ show e)
|
|
|
|
where
|
|
|
|
toObj :: (T.Text, J.Value) -> J.Value
|
|
|
|
toObj (k, v) = J.object [ "type" J..= k
|
|
|
|
, "detail" J..= v]
|
|
|
|
-- encapsulates a http operation
|
2019-11-26 15:14:21 +03:00
|
|
|
instance ToEngineLog HTTPErr Hasura where
|
|
|
|
toEngineLog err = (LevelError, eventTriggerLogType, J.toJSON err)
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
isNetworkError :: HTTPErr -> Bool
|
|
|
|
isNetworkError = \case
|
|
|
|
HClient he -> isNetworkErrorHC he
|
|
|
|
_ -> False
|
|
|
|
|
2019-02-14 10:37:59 +03:00
|
|
|
isNetworkErrorHC :: HTTP.HttpException -> Bool
|
2018-09-05 14:26:46 +03:00
|
|
|
isNetworkErrorHC = \case
|
2019-02-14 10:37:59 +03:00
|
|
|
HTTP.HttpExceptionRequest _ (HTTP.ConnectionFailure _) -> True
|
|
|
|
HTTP.HttpExceptionRequest _ HTTP.ConnectionTimeout -> True
|
|
|
|
HTTP.HttpExceptionRequest _ HTTP.ResponseTimeout -> True
|
2018-09-05 14:26:46 +03:00
|
|
|
_ -> False
|
|
|
|
|
2019-02-14 10:37:59 +03:00
|
|
|
anyBodyParser :: HTTP.Response B.ByteString -> Either HTTPErr HTTPResp
|
2018-10-26 19:28:03 +03:00
|
|
|
anyBodyParser resp = do
|
|
|
|
let httpResp = mkHTTPResp resp
|
2019-02-14 10:37:59 +03:00
|
|
|
if respCode >= HTTP.status200 && respCode < HTTP.status300
|
2018-10-26 19:28:03 +03:00
|
|
|
then return httpResp
|
|
|
|
else throwError $ HStatus httpResp
|
2018-09-05 14:26:46 +03:00
|
|
|
where
|
2019-02-14 10:37:59 +03:00
|
|
|
respCode = HTTP.responseStatus resp
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
data HTTPReq
|
|
|
|
= HTTPReq
|
|
|
|
{ _hrqMethod :: !String
|
|
|
|
, _hrqUrl :: !String
|
|
|
|
, _hrqPayload :: !(Maybe J.Value)
|
|
|
|
, _hrqTry :: !Int
|
|
|
|
, _hrqDelay :: !(Maybe Int)
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
$(J.deriveJSON (J.aesonDrop 4 J.snakeCase){J.omitNothingFields=True} ''HTTPReq)
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
instance ToEngineLog HTTPReq Hasura where
|
|
|
|
toEngineLog req = (LevelInfo, eventTriggerLogType, J.toJSON req)
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2020-03-05 20:59:26 +03:00
|
|
|
-- | Like 'HTTP.httpLbs' but we catch 'HTTP.HttpException' and return all known
|
|
|
|
-- error-like conditions as 'HTTPErr'.
|
2018-09-05 14:26:46 +03:00
|
|
|
runHTTP
|
|
|
|
:: ( MonadReader r m
|
2019-11-26 15:14:21 +03:00
|
|
|
, Has (Logger Hasura) r
|
2019-02-14 10:37:59 +03:00
|
|
|
, Has HTTP.Manager r
|
2019-11-26 15:14:21 +03:00
|
|
|
, MonadIO m
|
2018-09-05 14:26:46 +03:00
|
|
|
)
|
2019-02-14 10:37:59 +03:00
|
|
|
=> HTTP.Request -> Maybe ExtraContext -> m (Either HTTPErr HTTPResp)
|
|
|
|
runHTTP req exLog = do
|
2019-11-26 15:14:21 +03:00
|
|
|
logger :: Logger Hasura <- asks getter
|
2019-02-14 10:37:59 +03:00
|
|
|
manager <- asks getter
|
|
|
|
res <- liftIO $ try $ HTTP.httpLbs req manager
|
2018-09-05 14:26:46 +03:00
|
|
|
case res of
|
2019-11-26 15:14:21 +03:00
|
|
|
Left e -> unLogger logger $ HClient e
|
|
|
|
Right resp -> unLogger logger $ HTTPRespExtra (mkHTTPResp resp) exLog
|
2019-02-14 10:37:59 +03:00
|
|
|
return $ either (Left . HClient) anyBodyParser res
|