@@ -124,7 +124,7 @@ module Network.HTTP.Req
124 -- *** Headers
124 -- *** Headers
126 -- *** Cookies
126 -- *** Cookies
127− -- *** Authorization
127+ -- *** Authentication
128 -- *** Other
128 -- *** Other
130 -- * Response
130 -- * Response
@@ -133,7 +133,7 @@ module Network.HTTP.Req
133 , CanHaveBody (..) )
133 , CanHaveBody (..) )
136−import Control.Arrow (first)
136+import Control.Arrow (first, second)
137import Control.Exception (try)
137import Control.Exception (try)
138import Control.Monad.IO.Class
138import Control.Monad.IO.Class
139import Data.Aeson
139import Data.Aeson
@@ -152,6 +152,7 @@ import System.IO.Unsafe (unsafePerformIO)
152import qualified Data.Binary.Builder as R
152import qualified Data.Binary.Builder as R
153import qualified Data.ByteString as B
153import qualified Data.ByteString as B
154import qualified Data.ByteString.Lazy as BL
154import qualified Data.ByteString.Lazy as BL
155+import qualified Data.CaseInsensitive as CI
155import qualified Data.List.NonEmpty as NE
156import qualified Data.List.NonEmpty as NE
156import qualified Data.Text.Encoding as T
157import qualified Data.Text.Encoding as T
157import qualified Network.Connection as NC
158import qualified Network.Connection as NC
@@ -492,6 +493,18 @@ newtype Option = Option (Endo (Y.QueryText, L.Request))
492 -- time new parameter is added.
493 -- time new parameter is added.
493 deriving (Semigroup, Monoid)
494 deriving (Semigroup, Monoid)
496+-- | A helper to create an 'Option' that modifies only collection of query
497+-- parameters. This helper is not a part of public API.
499+withQueryParams :: (Y.QueryText -> Y.QueryText) -> Option
500+withQueryParams = Option . Endo . first
502+-- | A helper to create an 'Option' that modifies only 'L.Request'. This
503+-- helper is not a part of public API.
505+withRequest :: (L.Request -> L.Request) -> Option
506+withRequest = Option . Endo . second
495instance RequestComponent Option where
508instance RequestComponent Option where
496 getRequestMod (Option f) = Endo $ \x ->
509 getRequestMod (Option f) = Endo $ \x ->
497 let (qparams, x') = appEndo f ([], x)
510 let (qparams, x') = appEndo f ([], x)
@@ -540,13 +553,22 @@ class QueryParam a where
540 queryParam :: Text -> Maybe Text -> a
553 queryParam :: Text -> Maybe Text -> a
542instance QueryParam Option where
555instance QueryParam Option where
543− queryParam name mvalue = (Option . Endo . first) ((:) (name, mvalue))
556+ queryParam name mvalue = withQueryParams ((:) (name, mvalue))
545----------------------------------------------------------------------------
558----------------------------------------------------------------------------
546-- Request — Optional parameters — Headers
559-- Request — Optional parameters — Headers
561+-- | Create an 'Option' that adds a header. The 'Text' values will be
562+-- inserted in UTF-8 encoding.
565+ :: Text -- ^ Header name
566+ -> Text -- ^ Header value
548−header :: Text -> Text -> Option
567+ -> Option
549−header = undefined
568+header name value = withRequest $ \x ->
569+ let name' = T.encodeUtf8 name
570+ value' = T.encodeUtf8 value
571+ in x { L.requestHeaders = (CI.mk name', value') : L.requestHeaders x }
551----------------------------------------------------------------------------
573----------------------------------------------------------------------------
552-- Request — Optional parameters — Cookies
574-- Request — Optional parameters — Cookies
@@ -554,20 +576,28 @@ header = undefined
554-- TODO No idea right now.
576-- TODO No idea right now.
556----------------------------------------------------------------------------
578----------------------------------------------------------------------------
557−-- Request — Optional parameters — Authorization
579+-- Request — Optional parameters — Authentication
559−-- Hmm, need to take a look at wreq first…
581+-- TODO basicAuth
561----------------------------------------------------------------------------
587----------------------------------------------------------------------------
562-- Request — Optional parameters — Other
588-- Request — Optional parameters — Other
564−port :: Word -> Option
590+-- | Specify the port to connect to explicitly. Normally, 'Url' you use
565−port = undefined -- TODO
591+-- determines default port, @80@ for HTTP and @443@ for HTTPS, this 'Option'
592+-- allows to choose arbitrary port overwriting the defaults.
567−-- redirectCount :: Word -> Option
594+port :: Word -> Option
568−-- redirectCount = undefined -- TODO
595+port n = withRequest $ \x ->
596+ x { L.port = fromIntegral n }
570−-- etc.
598+-- TODO decompress
599+-- TODO responseTimeout
600+-- TODO requestVersion
572----------------------------------------------------------------------------
602----------------------------------------------------------------------------
573-- Response
603-- Response