{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE OverloadedStrings #-}
module Network.HTTP.Simple
(
httpBS
, httpLBS
, httpNoBody
#ifdef VERSION_aeson
, httpJSON
, httpJSONEither
#endif
, httpSink
, httpSource
, withResponse
, H.Header
, H.Query
, H.QueryItem
, H.Request
, H.RequestHeaders
, H.Response
, H.ResponseHeaders
#ifdef VERSION_aeson
, JSONException (..)
#endif
, H.HttpException (..)
, H.Proxy (..)
, H.defaultRequest
, H.parseRequest
, H.parseRequest_
, parseRequestThrow
, parseRequestThrow_
, setRequestMethod
, setRequestSecure
, setRequestHost
, setRequestPort
, setRequestPath
, addRequestHeader
, getRequestHeader
, setRequestHeader
, setRequestHeaders
, setRequestQueryString
, getRequestQueryString
, addToRequestQueryString
, setRequestBody
#ifdef VERSION_aeson
, setRequestBodyJSON
#endif
, setRequestBodyLBS
, setRequestBodySource
, setRequestBodyFile
, setRequestBodyURLEncoded
, H.setRequestIgnoreStatus
, H.setRequestCheckStatus
, setRequestBasicAuth
#if MIN_VERSION_http_client(0,7,6)
, setRequestBearerAuth
#endif
, setRequestManager
, setRequestProxy
, setRequestResponseTimeout
, getResponseStatus
, getResponseStatusCode
, getResponseHeader
, getResponseHeaders
, getResponseBody
, httpLbs
) where
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Network.HTTP.Client as H
import qualified Network.HTTP.Client.Internal as HI
import qualified Network.HTTP.Client.TLS as H
import Network.HTTP.Client.Conduit (bodyReaderSource)
import qualified Network.HTTP.Client.Conduit as HC
import Control.Monad.IO.Unlift (MonadIO, liftIO, MonadUnliftIO, withRunInIO)
#ifdef VERSION_aeson
import Data.Aeson (FromJSON (..), Value)
import Data.Aeson.Parser (json')
import qualified Data.Aeson.Types as A
import qualified Data.Aeson as A
#endif
import qualified Data.Traversable as T
import Control.Exception (throw, throwIO, Exception)
import Data.Monoid
import Data.Typeable (Typeable)
import qualified Data.Conduit as C
import Data.Conduit (runConduit, (.|), ConduitM)
import qualified Data.Conduit.Attoparsec as C
import qualified Network.HTTP.Types as H
import Data.Int (Int64)
import Control.Monad.Trans.Resource (MonadResource, MonadThrow)
import qualified Control.Exception as E (bracket)
import Data.Void (Void)
import qualified Data.Attoparsec.ByteString as Atto
import qualified Data.Attoparsec.ByteString.Char8 as Atto8
httpBS :: MonadIO m => H.Request -> m (H.Response S.ByteString)
httpBS :: forall (m :: * -> *).
MonadIO m =>
Request -> m (Response ByteString)
httpBS Request
req = IO (Response ByteString) -> m (Response ByteString)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Response ByteString) -> m (Response ByteString))
-> IO (Response ByteString) -> m (Response ByteString)
forall a b. (a -> b) -> a -> b
$ do
Manager
man <- IO Manager
H.getGlobalManager
(ByteString -> ByteString)
-> Response ByteString -> Response ByteString
forall a b. (a -> b) -> Response a -> Response b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ByteString
L.toStrict (Response ByteString -> Response ByteString)
-> IO (Response ByteString) -> IO (Response ByteString)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Request -> Manager -> IO (Response ByteString)
H.httpLbs Request
req Manager
man
httpLBS :: MonadIO m => H.Request -> m (H.Response L.ByteString)
httpLBS :: forall (m :: * -> *).
MonadIO m =>
Request -> m (Response ByteString)
httpLBS Request
req = IO (Response ByteString) -> m (Response ByteString)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Response ByteString) -> m (Response ByteString))
-> IO (Response ByteString) -> m (Response ByteString)
forall a b. (a -> b) -> a -> b
$ do
Manager
man <- IO Manager
H.getGlobalManager
Request -> Manager -> IO (Response ByteString)
H.httpLbs Request
req Manager
man
httpNoBody :: MonadIO m => H.Request -> m (H.Response ())
httpNoBody :: forall (m :: * -> *). MonadIO m => Request -> m (Response ())
httpNoBody Request
req = IO (Response ()) -> m (Response ())
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Response ()) -> m (Response ()))
-> IO (Response ()) -> m (Response ())
forall a b. (a -> b) -> a -> b
$ do
Manager
man <- IO Manager
H.getGlobalManager
Request -> Manager -> IO (Response ())
H.httpNoBody Request
req Manager
man
#ifdef VERSION_aeson
httpJSON :: (MonadIO m, FromJSON a) => H.Request -> m (H.Response a)
httpJSON :: forall (m :: * -> *) a.
(MonadIO m, FromJSON a) =>
Request -> m (Response a)
httpJSON Request
req = IO (Response a) -> m (Response a)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Response a) -> m (Response a))
-> IO (Response a) -> m (Response a)
forall a b. (a -> b) -> a -> b
$ Request -> IO (Response (Either JSONException a))
forall (m :: * -> *) a.
(MonadIO m, FromJSON a) =>
Request -> m (Response (Either JSONException a))
httpJSONEither Request
req IO (Response (Either JSONException a))
-> (Response (Either JSONException a) -> IO (Response a))
-> IO (Response a)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Either JSONException a -> IO a)
-> Response (Either JSONException a) -> IO (Response a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Response a -> m (Response b)
T.mapM ((JSONException -> IO a)
-> (a -> IO a) -> Either JSONException a -> IO a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either JSONException -> IO a
forall e a. Exception e => e -> IO a
throwIO a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return)
httpJSONEither :: (MonadIO m, FromJSON a)
=> H.Request
-> m (H.Response (Either JSONException a))
httpJSONEither :: forall (m :: * -> *) a.
(MonadIO m, FromJSON a) =>
Request -> m (Response (Either JSONException a))
httpJSONEither Request
req = IO (Response (Either JSONException a))
-> m (Response (Either JSONException a))
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Response (Either JSONException a))
-> m (Response (Either JSONException a)))
-> IO (Response (Either JSONException a))
-> m (Response (Either JSONException a))
forall a b. (a -> b) -> a -> b
$ Request
-> (Response ()
-> ConduitM ByteString Void IO (Response (Either JSONException a)))
-> IO (Response (Either JSONException a))
forall (m :: * -> *) a.
MonadUnliftIO m =>
Request -> (Response () -> ConduitM ByteString Void m a) -> m a
httpSink Request
req' Response ()
-> ConduitM ByteString Void IO (Response (Either JSONException a))
forall {m :: * -> *} {b} {o}.
(Monad m, FromJSON b) =>
Response ()
-> ConduitT ByteString o m (Response (Either JSONException b))
sink
where
req' :: Request
req' = HeaderName -> ByteString -> Request -> Request
addRequestHeader HeaderName
H.hAccept ByteString
"application/json" Request
req
sink :: Response ()
-> ConduitT ByteString o m (Response (Either JSONException b))
sink Response ()
orig = (Either JSONException b -> Response (Either JSONException b))
-> ConduitT ByteString o m (Either JSONException b)
-> ConduitT ByteString o m (Response (Either JSONException b))
forall a b.
(a -> b) -> ConduitT ByteString o m a -> ConduitT ByteString o m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Either JSONException b
x -> (() -> Either JSONException b)
-> Response () -> Response (Either JSONException b)
forall a b. (a -> b) -> Response a -> Response b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either JSONException b -> () -> Either JSONException b
forall a b. a -> b -> a
const Either JSONException b
x) Response ()
orig) (ConduitT ByteString o m (Either JSONException b)
-> ConduitT ByteString o m (Response (Either JSONException b)))
-> ConduitT ByteString o m (Either JSONException b)
-> ConduitT ByteString o m (Response (Either JSONException b))
forall a b. (a -> b) -> a -> b
$ do
Either ParseError Value
eres1 <- Parser ByteString Value
-> ConduitT ByteString o m (Either ParseError Value)
forall a (m :: * -> *) b o.
(AttoparsecInput a, Monad m) =>
Parser a b -> ConduitT a o m (Either ParseError b)
C.sinkParserEither (Parser ByteString Value
json' Parser ByteString Value
-> Parser ByteString () -> Parser ByteString Value
forall a b.
Parser ByteString a -> Parser ByteString b -> Parser ByteString a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (Parser ByteString ()
Atto8.skipSpace Parser ByteString ()
-> Parser ByteString () -> Parser ByteString ()
forall a b.
Parser ByteString a -> Parser ByteString b -> Parser ByteString b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ByteString ()
forall t. Chunk t => Parser t ()
Atto.endOfInput))
case Either ParseError Value
eres1 of
Left ParseError
e -> Either JSONException b
-> ConduitT ByteString o m (Either JSONException b)
forall a. a -> ConduitT ByteString o m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either JSONException b
-> ConduitT ByteString o m (Either JSONException b))
-> Either JSONException b
-> ConduitT ByteString o m (Either JSONException b)
forall a b. (a -> b) -> a -> b
$ JSONException -> Either JSONException b
forall a b. a -> Either a b
Left (JSONException -> Either JSONException b)
-> JSONException -> Either JSONException b
forall a b. (a -> b) -> a -> b
$ Request -> Response () -> ParseError -> JSONException
JSONParseException Request
req' Response ()
orig ParseError
e
Right Value
value ->
case Value -> Result b
forall a. FromJSON a => Value -> Result a
A.fromJSON Value
value of
A.Error String
e -> Either JSONException b
-> ConduitT ByteString o m (Either JSONException b)
forall a. a -> ConduitT ByteString o m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either JSONException b
-> ConduitT ByteString o m (Either JSONException b))
-> Either JSONException b
-> ConduitT ByteString o m (Either JSONException b)
forall a b. (a -> b) -> a -> b
$ JSONException -> Either JSONException b
forall a b. a -> Either a b
Left (JSONException -> Either JSONException b)
-> JSONException -> Either JSONException b
forall a b. (a -> b) -> a -> b
$ Request -> Response Value -> String -> JSONException
JSONConversionException
Request
req' ((() -> Value) -> Response () -> Response Value
forall a b. (a -> b) -> Response a -> Response b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Value -> () -> Value
forall a b. a -> b -> a
const Value
value) Response ()
orig) String
e
A.Success b
x -> Either JSONException b
-> ConduitT ByteString o m (Either JSONException b)
forall a. a -> ConduitT ByteString o m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either JSONException b
-> ConduitT ByteString o m (Either JSONException b))
-> Either JSONException b
-> ConduitT ByteString o m (Either JSONException b)
forall a b. (a -> b) -> a -> b
$ b -> Either JSONException b
forall a b. b -> Either a b
Right b
x
data JSONException
= JSONParseException H.Request (H.Response ()) C.ParseError
| JSONConversionException H.Request (H.Response Value) String
deriving (Int -> JSONException -> ShowS
[JSONException] -> ShowS
JSONException -> String
(Int -> JSONException -> ShowS)
-> (JSONException -> String)
-> ([JSONException] -> ShowS)
-> Show JSONException
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONException -> ShowS
showsPrec :: Int -> JSONException -> ShowS
$cshow :: JSONException -> String
show :: JSONException -> String
$cshowList :: [JSONException] -> ShowS
showList :: [JSONException] -> ShowS
Show, Typeable)
instance Exception JSONException
#endif
httpSink :: MonadUnliftIO m
=> H.Request
-> (H.Response () -> ConduitM S.ByteString Void m a)
-> m a
httpSink :: forall (m :: * -> *) a.
MonadUnliftIO m =>
Request -> (Response () -> ConduitM ByteString Void m a) -> m a
httpSink Request
req Response () -> ConduitM ByteString Void m a
sink = ((forall a. m a -> IO a) -> IO a) -> m a
forall b. ((forall a. m a -> IO a) -> IO b) -> m b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO a) -> m a)
-> ((forall a. m a -> IO a) -> IO a) -> m a
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> do
Manager
man <- IO Manager
H.getGlobalManager
IO (Response BodyReader)
-> (Response BodyReader -> IO ())
-> (Response BodyReader -> IO a)
-> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
E.bracket
(Request -> Manager -> IO (Response BodyReader)
H.responseOpen Request
req Manager
man)
Response BodyReader -> IO ()
forall a. Response a -> IO ()
H.responseClose
((Response BodyReader -> IO a) -> IO a)
-> (Response BodyReader -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \Response BodyReader
res -> m a -> IO a
forall a. m a -> IO a
run
(m a -> IO a) -> m a -> IO a
forall a b. (a -> b) -> a -> b
$ ConduitT () Void m a -> m a
forall (m :: * -> *) r. Monad m => ConduitT () Void m r -> m r
runConduit
(ConduitT () Void m a -> m a) -> ConduitT () Void m a -> m a
forall a b. (a -> b) -> a -> b
$ BodyReader -> ConduitM () ByteString m ()
forall (m :: * -> *) i.
MonadIO m =>
BodyReader -> ConduitM i ByteString m ()
bodyReaderSource (Response BodyReader -> BodyReader
forall a. Response a -> a
getResponseBody Response BodyReader
res)
ConduitM () ByteString m ()
-> ConduitM ByteString Void m a -> ConduitT () Void m a
forall (m :: * -> *) a b c r.
Monad m =>
ConduitT a b m () -> ConduitT b c m r -> ConduitT a c m r
.| Response () -> ConduitM ByteString Void m a
sink ((BodyReader -> ()) -> Response BodyReader -> Response ()
forall a b. (a -> b) -> Response a -> Response b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (() -> BodyReader -> ()
forall a b. a -> b -> a
const ()) Response BodyReader
res)
httpSource :: (MonadResource m, MonadIO n)
=> H.Request
-> (H.Response (C.ConduitM i S.ByteString n ())
-> C.ConduitM i o m r)
-> C.ConduitM i o m r
httpSource :: forall (m :: * -> *) (n :: * -> *) i o r.
(MonadResource m, MonadIO n) =>
Request
-> (Response (ConduitM i ByteString n ()) -> ConduitM i o m r)
-> ConduitM i o m r
httpSource Request
req Response (ConduitM i ByteString n ()) -> ConduitM i o m r
withRes = do
Manager
man <- IO Manager -> ConduitT i o m Manager
forall a. IO a -> ConduitT i o m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Manager
H.getGlobalManager
IO (Response BodyReader)
-> (Response BodyReader -> IO ())
-> (Response BodyReader -> ConduitM i o m r)
-> ConduitM i o m r
forall (m :: * -> *) a i o r.
MonadResource m =>
IO a -> (a -> IO ()) -> (a -> ConduitT i o m r) -> ConduitT i o m r
C.bracketP (Request -> Manager -> IO (Response BodyReader)
H.responseOpen Request
req Manager
man) Response BodyReader -> IO ()
forall a. Response a -> IO ()
H.responseClose
(Response (ConduitM i ByteString n ()) -> ConduitM i o m r
withRes (Response (ConduitM i ByteString n ()) -> ConduitM i o m r)
-> (Response BodyReader -> Response (ConduitM i ByteString n ()))
-> Response BodyReader
-> ConduitM i o m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BodyReader -> ConduitM i ByteString n ())
-> Response BodyReader -> Response (ConduitM i ByteString n ())
forall a b. (a -> b) -> Response a -> Response b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BodyReader -> ConduitM i ByteString n ()
forall (m :: * -> *) i.
MonadIO m =>
BodyReader -> ConduitM i ByteString m ()
bodyReaderSource)
withResponse :: (MonadUnliftIO m, MonadIO n)
=> H.Request
-> (H.Response (C.ConduitM i S.ByteString n ()) -> m a)
-> m a
withResponse :: forall (m :: * -> *) (n :: * -> *) i a.
(MonadUnliftIO m, MonadIO n) =>
Request -> (Response (ConduitM i ByteString n ()) -> m a) -> m a
withResponse Request
req Response (ConduitM i ByteString n ()) -> m a
withRes = ((forall a. m a -> IO a) -> IO a) -> m a
forall b. ((forall a. m a -> IO a) -> IO b) -> m b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO a) -> m a)
-> ((forall a. m a -> IO a) -> IO a) -> m a
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> do
Manager
man <- IO Manager
H.getGlobalManager
IO (Response BodyReader)
-> (Response BodyReader -> IO ())
-> (Response BodyReader -> IO a)
-> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
E.bracket
(Request -> Manager -> IO (Response BodyReader)
H.responseOpen Request
req Manager
man)
Response BodyReader -> IO ()
forall a. Response a -> IO ()
H.responseClose
(m a -> IO a
forall a. m a -> IO a
run (m a -> IO a)
-> (Response BodyReader -> m a) -> Response BodyReader -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response (ConduitM i ByteString n ()) -> m a
withRes (Response (ConduitM i ByteString n ()) -> m a)
-> (Response BodyReader -> Response (ConduitM i ByteString n ()))
-> Response BodyReader
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BodyReader -> ConduitM i ByteString n ())
-> Response BodyReader -> Response (ConduitM i ByteString n ())
forall a b. (a -> b) -> Response a -> Response b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BodyReader -> ConduitM i ByteString n ()
forall (m :: * -> *) i.
MonadIO m =>
BodyReader -> ConduitM i ByteString m ()
bodyReaderSource)
parseRequestThrow :: MonadThrow m => String -> m HC.Request
parseRequestThrow :: forall (m :: * -> *). MonadThrow m => String -> m Request
parseRequestThrow = String -> m Request
forall (m :: * -> *). MonadThrow m => String -> m Request
HC.parseUrlThrow
parseRequestThrow_ :: String -> HC.Request
parseRequestThrow_ :: String -> Request
parseRequestThrow_ = (SomeException -> Request)
-> (Request -> Request) -> Either SomeException Request -> Request
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either SomeException -> Request
forall a e. Exception e => e -> a
throw Request -> Request
forall a. a -> a
id (Either SomeException Request -> Request)
-> (String -> Either SomeException Request) -> String -> Request
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either SomeException Request
forall (m :: * -> *). MonadThrow m => String -> m Request
HC.parseUrlThrow
httpLbs :: MonadIO m => H.Request -> m (H.Response L.ByteString)
httpLbs :: forall (m :: * -> *).
MonadIO m =>
Request -> m (Response ByteString)
httpLbs = Request -> m (Response ByteString)
forall (m :: * -> *).
MonadIO m =>
Request -> m (Response ByteString)
httpLBS
setRequestMethod :: S.ByteString -> H.Request -> H.Request
setRequestMethod :: ByteString -> Request -> Request
setRequestMethod ByteString
x Request
req = Request
req { H.method = x }
setRequestSecure :: Bool -> H.Request -> H.Request
setRequestSecure :: Bool -> Request -> Request
setRequestSecure Bool
x Request
req = Request
req { H.secure = x }
setRequestHost :: S.ByteString -> H.Request -> H.Request
setRequestHost :: ByteString -> Request -> Request
setRequestHost ByteString
x Request
r = Request
r { H.host = x }
setRequestPort :: Int -> H.Request -> H.Request
setRequestPort :: Int -> Request -> Request
setRequestPort Int
x Request
r = Request
r { H.port = x }
setRequestPath :: S.ByteString -> H.Request -> H.Request
setRequestPath :: ByteString -> Request -> Request
setRequestPath ByteString
x Request
r = Request
r { H.path = x }
addRequestHeader :: H.HeaderName -> S.ByteString -> H.Request -> H.Request
HeaderName
name ByteString
val Request
req =
Request
req { H.requestHeaders = (name, val) : H.requestHeaders req }
getRequestHeader :: H.HeaderName -> H.Request -> [S.ByteString]
HeaderName
name =
(Header -> ByteString) -> [Header] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map Header -> ByteString
forall a b. (a, b) -> b
snd ([Header] -> [ByteString])
-> (Request -> [Header]) -> Request -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Header -> Bool) -> [Header] -> [Header]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(HeaderName
x, ByteString
_) -> HeaderName
x HeaderName -> HeaderName -> Bool
forall a. Eq a => a -> a -> Bool
== HeaderName
name) ([Header] -> [Header])
-> (Request -> [Header]) -> Request -> [Header]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Request -> [Header]
H.requestHeaders
setRequestHeader :: H.HeaderName -> [S.ByteString] -> H.Request -> H.Request
HeaderName
name [ByteString]
vals Request
req =
Request
req { H.requestHeaders =
filter (\(HeaderName
x, ByteString
_) -> HeaderName
x HeaderName -> HeaderName -> Bool
forall a. Eq a => a -> a -> Bool
/= HeaderName
name) (H.requestHeaders req)
++ (map (name, ) vals)
}
setRequestHeaders :: H.RequestHeaders -> H.Request -> H.Request
[Header]
x Request
req = Request
req { H.requestHeaders = x }
getRequestQueryString :: H.Request -> H.Query
getRequestQueryString :: Request -> Query
getRequestQueryString = ByteString -> Query
H.parseQuery (ByteString -> Query)
-> (Request -> ByteString) -> Request -> Query
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Request -> ByteString
H.queryString
setRequestQueryString :: H.Query -> H.Request -> H.Request
setRequestQueryString :: Query -> Request -> Request
setRequestQueryString = Query -> Request -> Request
H.setQueryString
addToRequestQueryString :: H.Query -> H.Request -> H.Request
addToRequestQueryString :: Query -> Request -> Request
addToRequestQueryString Query
additions Request
req = Query -> Request -> Request
setRequestQueryString Query
q Request
req
where q :: Query
q = Query
additions Query -> Query -> Query
forall a. Semigroup a => a -> a -> a
<> Request -> Query
getRequestQueryString Request
req
setRequestBody :: H.RequestBody -> H.Request -> H.Request
setRequestBody :: RequestBody -> Request -> Request
setRequestBody RequestBody
x Request
req = Request
req { H.requestBody = x }
#ifdef VERSION_aeson
setRequestBodyJSON :: A.ToJSON a => a -> H.Request -> H.Request
setRequestBodyJSON :: forall a. ToJSON a => a -> Request -> Request
setRequestBodyJSON a
x Request
req =
Request
req { H.requestHeaders
= (H.hContentType, "application/json; charset=utf-8")
: filter (\(HeaderName
y, ByteString
_) -> HeaderName
y HeaderName -> HeaderName -> Bool
forall a. Eq a => a -> a -> Bool
/= HeaderName
H.hContentType) (H.requestHeaders req)
, H.requestBody = H.RequestBodyLBS $ A.encode x
}
#endif
setRequestBodyLBS :: L.ByteString -> H.Request -> H.Request
setRequestBodyLBS :: ByteString -> Request -> Request
setRequestBodyLBS = RequestBody -> Request -> Request
setRequestBody (RequestBody -> Request -> Request)
-> (ByteString -> RequestBody) -> ByteString -> Request -> Request
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> RequestBody
H.RequestBodyLBS
setRequestBodySource :: Int64
-> ConduitM () S.ByteString IO ()
-> H.Request
-> H.Request
setRequestBodySource :: Int64 -> ConduitM () ByteString IO () -> Request -> Request
setRequestBodySource Int64
len ConduitM () ByteString IO ()
src Request
req = Request
req { H.requestBody = HC.requestBodySource len src }
setRequestBodyFile :: FilePath -> H.Request -> H.Request
setRequestBodyFile :: String -> Request -> Request
setRequestBodyFile = RequestBody -> Request -> Request
setRequestBody (RequestBody -> Request -> Request)
-> (String -> RequestBody) -> String -> Request -> Request
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO RequestBody -> RequestBody
HI.RequestBodyIO (IO RequestBody -> RequestBody)
-> (String -> IO RequestBody) -> String -> RequestBody
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO RequestBody
H.streamFile
setRequestBodyURLEncoded :: [(S.ByteString, S.ByteString)] -> H.Request -> H.Request
setRequestBodyURLEncoded :: [(ByteString, ByteString)] -> Request -> Request
setRequestBodyURLEncoded = [(ByteString, ByteString)] -> Request -> Request
H.urlEncodedBody
setRequestBasicAuth :: S.ByteString
-> S.ByteString
-> H.Request
-> H.Request
setRequestBasicAuth :: ByteString -> ByteString -> Request -> Request
setRequestBasicAuth = ByteString -> ByteString -> Request -> Request
H.applyBasicAuth
#if MIN_VERSION_http_client(0,7,6)
setRequestBearerAuth :: S.ByteString
-> H.Request
-> H.Request
setRequestBearerAuth :: ByteString -> Request -> Request
setRequestBearerAuth = ByteString -> Request -> Request
H.applyBearerAuth
#endif
setRequestManager :: H.Manager -> H.Request -> H.Request
setRequestManager :: Manager -> Request -> Request
setRequestManager Manager
x Request
req = Request
req { HI.requestManagerOverride = Just x }
setRequestProxy :: Maybe H.Proxy -> H.Request -> H.Request
setRequestProxy :: Maybe Proxy -> Request -> Request
setRequestProxy Maybe Proxy
x Request
req = Request
req { H.proxy = x }
setRequestResponseTimeout :: H.ResponseTimeout -> H.Request -> H.Request
setRequestResponseTimeout :: ResponseTimeout -> Request -> Request
setRequestResponseTimeout ResponseTimeout
x Request
req = Request
req { H.responseTimeout = x }
getResponseStatus :: H.Response a -> H.Status
getResponseStatus :: forall a. Response a -> Status
getResponseStatus = Response a -> Status
forall a. Response a -> Status
H.responseStatus
getResponseStatusCode :: H.Response a -> Int
getResponseStatusCode :: forall a. Response a -> Int
getResponseStatusCode = Status -> Int
H.statusCode (Status -> Int) -> (Response a -> Status) -> Response a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response a -> Status
forall a. Response a -> Status
getResponseStatus
getResponseHeader :: H.HeaderName -> H.Response a -> [S.ByteString]
HeaderName
name = (Header -> ByteString) -> [Header] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map Header -> ByteString
forall a b. (a, b) -> b
snd ([Header] -> [ByteString])
-> (Response a -> [Header]) -> Response a -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Header -> Bool) -> [Header] -> [Header]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(HeaderName
x, ByteString
_) -> HeaderName
x HeaderName -> HeaderName -> Bool
forall a. Eq a => a -> a -> Bool
== HeaderName
name) ([Header] -> [Header])
-> (Response a -> [Header]) -> Response a -> [Header]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response a -> [Header]
forall body. Response body -> [Header]
H.responseHeaders
getResponseHeaders :: H.Response a -> [(H.HeaderName, S.ByteString)]
= Response a -> [Header]
forall body. Response body -> [Header]
H.responseHeaders
getResponseBody :: H.Response a -> a
getResponseBody :: forall a. Response a -> a
getResponseBody = Response a -> a
forall a. Response a -> a
H.responseBody