{-# LANGUAGE FlexibleInstances, OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

-- |
-- Module      : Network.Wreq.Types
-- Copyright   : (c) 2014 Bryan O'Sullivan
--
-- License     : BSD-style
-- Maintainer  : bos@serpentine.com
-- Stability   : experimental
-- Portability : GHC
--
-- HTTP client types.

module Network.Wreq.Types
    (
    -- * Client configuration
      Options(..)
    , Auth(..)
    , AWSAuthVersion(..)
    , ResponseChecker
    -- * Request payloads
    , Payload(..)
    , Postable(..)
    , Patchable(..)
    , Putable(..)
    -- ** URL-encoded forms
    , FormParam(..)
    , FormValue(..)
    -- * Headers
    , ContentType
    , Link(..)
    -- * Errors
    , JSONError(..)
    -- * Request handling
    , Req
    , reqURL
    , Run
    ) where

import Control.Lens ((&), (.~))
import Data.Aeson (Encoding, Value, encode)
import Data.Aeson.Encoding (encodingToLazyByteString)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Word (Word, Word8, Word16, Word32, Word64)
import Network.HTTP.Client (Request(method))
import Network.HTTP.Client.MultipartFormData (Part, formDataBody)
import Network.Wreq.Internal.Types
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TL
import qualified Network.HTTP.Client as HTTP
import qualified Network.Wreq.Internal.Lens as Lens

-- By default if the type is Putable, we use that as postPayload
instance Postable Part
instance Postable [Part]
instance Postable [(S.ByteString, S.ByteString)]
instance Postable (S.ByteString, S.ByteString)
instance Postable [FormParam]
instance Postable FormParam
instance Postable Payload
instance Postable S.ByteString
instance Postable L.ByteString
instance Postable Value
instance Postable Encoding

-- By default if the type is Putable, we use that as patchPayload
instance Patchable Part
instance Patchable [Part]
instance Patchable [(S.ByteString, S.ByteString)]
instance Patchable (S.ByteString, S.ByteString)
instance Patchable [FormParam]
instance Patchable FormParam
instance Patchable Payload
instance Patchable S.ByteString
instance Patchable L.ByteString
instance Patchable Value
instance Patchable Encoding

instance Putable Part where
    putPayload :: Part -> Request -> IO Request
putPayload Part
a = forall a. Putable a => a -> Request -> IO Request
putPayload [Part
a]

instance Putable [Part] where
    putPayload :: [Part] -> Request -> IO Request
putPayload [Part]
p Request
req =
        -- According to doc, formDataBody changes the request type to POST which is wrong; change it back
        (\Request
r -> Request
r{method :: ByteString
method=Request -> ByteString
method Request
req}) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall (m :: * -> *). MonadIO m => [Part] -> Request -> m Request
formDataBody [Part]
p Request
req

instance Putable [(S.ByteString, S.ByteString)] where
    putPayload :: [(ByteString, ByteString)] -> Request -> IO Request
putPayload [(ByteString, ByteString)]
ps Request
req =
        -- According to doc, urlEncodedBody changes the request type to POST which is wrong; change it back
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [(ByteString, ByteString)] -> Request -> Request
HTTP.urlEncodedBody [(ByteString, ByteString)]
ps Request
req {method :: ByteString
method=Request -> ByteString
method Request
req}

instance Putable (S.ByteString, S.ByteString) where
    putPayload :: (ByteString, ByteString) -> Request -> IO Request
putPayload (ByteString, ByteString)
p = forall a. Putable a => a -> Request -> IO Request
putPayload [(ByteString, ByteString)
p]

instance Putable [FormParam] where
    putPayload :: [FormParam] -> Request -> IO Request
putPayload [FormParam]
ps = forall a. Putable a => a -> Request -> IO Request
putPayload (forall a b. (a -> b) -> [a] -> [b]
map FormParam -> (ByteString, ByteString)
f [FormParam]
ps)
      where f :: FormParam -> (ByteString, ByteString)
f (ByteString
a := v
b) = (ByteString
a, forall a. FormValue a => a -> ByteString
renderFormValue v
b)

instance Putable FormParam where
    putPayload :: FormParam -> Request -> IO Request
putPayload FormParam
p = forall a. Putable a => a -> Request -> IO Request
putPayload [FormParam
p]

instance Putable Payload where
    putPayload :: Payload -> Request -> IO Request
putPayload Payload
pl =
      case Payload
pl of
        Raw ByteString
ct RequestBody
rb -> ByteString -> RequestBody -> Request -> IO Request
payload ByteString
ct RequestBody
rb

instance Putable S.ByteString where
    putPayload :: ByteString -> Request -> IO Request
putPayload = ByteString -> RequestBody -> Request -> IO Request
payload ByteString
"application/octet-stream" forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> RequestBody
HTTP.RequestBodyBS

instance Putable L.ByteString where
    putPayload :: ByteString -> Request -> IO Request
putPayload = ByteString -> RequestBody -> Request -> IO Request
payload ByteString
"application/octet-stream" forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> RequestBody
HTTP.RequestBodyLBS

instance Putable Value where
    putPayload :: Value -> Request -> IO Request
putPayload = ByteString -> RequestBody -> Request -> IO Request
payload ByteString
"application/json" forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> RequestBody
HTTP.RequestBodyLBS forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => a -> ByteString
encode

instance Putable Encoding where
    putPayload :: Encoding -> Request -> IO Request
putPayload = ByteString -> RequestBody -> Request -> IO Request
payload ByteString
"application/json" forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> RequestBody
HTTP.RequestBodyLBS forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      forall a. Encoding' a -> ByteString
encodingToLazyByteString

instance FormValue T.Text where
    renderFormValue :: Text -> ByteString
renderFormValue = Text -> ByteString
T.encodeUtf8

instance FormValue TL.Text where
    renderFormValue :: Text -> ByteString
renderFormValue = Text -> ByteString
T.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
TL.toStrict

instance FormValue TL.Builder where
    renderFormValue :: Builder -> ByteString
renderFormValue = Text -> ByteString
T.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
TL.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
TL.toLazyText

instance FormValue String where
    renderFormValue :: String -> ByteString
renderFormValue = Text -> ByteString
T.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

instance FormValue S.ByteString where
    renderFormValue :: ByteString -> ByteString
renderFormValue = forall a. a -> a
id

instance FormValue L.ByteString where
    renderFormValue :: ByteString -> ByteString
renderFormValue = [ByteString] -> ByteString
S.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
L.toChunks

instance FormValue Int where renderFormValue :: Int -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
instance FormValue Int8 where renderFormValue :: Int8 -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
instance FormValue Int16 where renderFormValue :: Int16 -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
instance FormValue Int32 where renderFormValue :: Int32 -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
instance FormValue Int64 where renderFormValue :: Int64 -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
instance FormValue Integer where renderFormValue :: Integer -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show

instance FormValue Word where renderFormValue :: Word -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
instance FormValue Word8 where renderFormValue :: Word8 -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
instance FormValue Word16 where renderFormValue :: Word16 -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
instance FormValue Word32 where renderFormValue :: Word32 -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
instance FormValue Word64 where renderFormValue :: Word64 -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show

instance FormValue Float where renderFormValue :: Float -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
instance FormValue Double where renderFormValue :: Double -> ByteString
renderFormValue = forall a. FormValue a => a -> ByteString
renderFormValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show

instance FormValue () where renderFormValue :: () -> ByteString
renderFormValue ()
_ = ByteString
""

instance (FormValue a) => FormValue (Maybe a) where
    renderFormValue :: Maybe a -> ByteString
renderFormValue (Just a
a) = forall a. FormValue a => a -> ByteString
renderFormValue a
a
    renderFormValue Maybe a
Nothing  = ByteString
""

payload :: S.ByteString -> HTTP.RequestBody -> Request -> IO Request
payload :: ByteString -> RequestBody -> Request -> IO Request
payload ByteString
ct RequestBody
body Request
req =
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Request
req forall a b. a -> (a -> b) -> b
& HeaderName -> ByteString -> Request -> Request
Lens.maybeSetHeader HeaderName
"Content-Type" ByteString
ct
               forall a b. a -> (a -> b) -> b
& Lens' Request RequestBody
Lens.requestBody forall s t a b. ASetter s t a b -> b -> s -> t
.~ RequestBody
body