2018-12-13 10:26:15 +03:00
|
|
|
module Hasura.HTTP
|
|
|
|
( wreqOptions
|
|
|
|
, HttpException(..)
|
|
|
|
) where
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2019-02-14 10:37:59 +03:00
|
|
|
import Control.Lens hiding ((.=))
|
2018-11-23 16:02:46 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
import qualified Data.Aeson as J
|
2018-11-23 16:02:46 +03:00
|
|
|
import qualified Data.Text.Encoding as T
|
|
|
|
import qualified Network.HTTP.Client as HTTP
|
|
|
|
import qualified Network.HTTP.Types as HTTP
|
|
|
|
import qualified Network.Wreq as Wreq
|
|
|
|
|
|
|
|
import Hasura.Server.Version (currentVersion)
|
|
|
|
|
|
|
|
wreqOptions :: HTTP.Manager -> [HTTP.Header] -> Wreq.Options
|
|
|
|
wreqOptions manager hdrs =
|
|
|
|
Wreq.defaults
|
|
|
|
& Wreq.headers .~ contentType : userAgent : hdrs
|
|
|
|
& Wreq.checkResponse ?~ (\_ _ -> return ())
|
|
|
|
& Wreq.manager .~ Right manager
|
|
|
|
where
|
|
|
|
contentType = ("Content-Type", "application/json")
|
|
|
|
userAgent = ( "User-Agent"
|
|
|
|
, "hasura-graphql-engine/" <> T.encodeUtf8 currentVersion
|
|
|
|
)
|
2018-12-13 10:26:15 +03:00
|
|
|
|
|
|
|
newtype HttpException
|
|
|
|
= HttpException
|
|
|
|
{ unHttpException :: HTTP.HttpException }
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
instance J.ToJSON HttpException where
|
|
|
|
toJSON = \case
|
|
|
|
(HttpException (HTTP.InvalidUrlException _ e)) ->
|
|
|
|
J.object [ "type" J..= ("invalid_url" :: Text)
|
|
|
|
, "message" J..= e
|
|
|
|
]
|
|
|
|
(HttpException (HTTP.HttpExceptionRequest _ cont)) ->
|
|
|
|
J.object [ "type" J..= ("http_exception" :: Text)
|
|
|
|
, "message" J..= show cont
|
|
|
|
]
|