on master help sign in
Add headers, port, and more TODO items
commit2d27d78af7ea134ca110570a71adcb47b50d7082 authormrkkrp <markkarpov@opmbx.org> dateTue 25 Oct 2016, 12:33 (10 years ago) parent0f88c4b
2 files changed+43−12
Network/HTTP/Req.hs+42−12 req.cabal+1
Network/HTTP/Req.hs+42−12
@@ -124,7 +124,7 @@ module Network.HTTP.Req
124124 -- *** Headers
125125 , header
126126 -- *** Cookies
127− -- *** Authorization
127+ -- *** Authentication
128128 -- *** Other
129129 , port
130130 -- * Response
@@ -133,7 +133,7 @@ module Network.HTTP.Req
133133 , CanHaveBody (..) )
134134where
135135
136−import Control.Arrow (first)
136+import Control.Arrow (first, second)
137137import Control.Exception (try)
138138import Control.Monad.IO.Class
139139import Data.Aeson
@@ -152,6 +152,7 @@ import System.IO.Unsafe (unsafePerformIO)
152152import qualified Data.Binary.Builder as R
153153import qualified Data.ByteString as B
154154import qualified Data.ByteString.Lazy as BL
155+import qualified Data.CaseInsensitive as CI
155156import qualified Data.List.NonEmpty as NE
156157import qualified Data.Text.Encoding as T
157158import qualified Network.Connection as NC
@@ -492,6 +493,18 @@ newtype Option = Option (Endo (Y.QueryText, L.Request))
492493 -- time new parameter is added.
493494 deriving (Semigroup, Monoid)
494495
496+-- | A helper to create an 'Option' that modifies only collection of query
497+-- parameters. This helper is not a part of public API.
498+
499+withQueryParams :: (Y.QueryText -> Y.QueryText) -> Option
500+withQueryParams = Option . Endo . first
501+
502+-- | A helper to create an 'Option' that modifies only 'L.Request'. This
503+-- helper is not a part of public API.
504+
505+withRequest :: (L.Request -> L.Request) -> Option
506+withRequest = Option . Endo . second
507+
495508instance RequestComponent Option where
496509 getRequestMod (Option f) = Endo $ \x ->
497510 let (qparams, x') = appEndo f ([], x)
@@ -540,13 +553,22 @@ class QueryParam a where
540553 queryParam :: Text -> Maybe Text -> a
541554
542555instance QueryParam Option where
543− queryParam name mvalue = (Option . Endo . first) ((:) (name, mvalue))
556+ queryParam name mvalue = withQueryParams ((:) (name, mvalue))
544557
545558----------------------------------------------------------------------------
546559-- Request — Optional parameters — Headers
547560
548−header :: Text -> Text -> Option
549−header = undefined
561+-- | Create an 'Option' that adds a header. The 'Text' values will be
562+-- inserted in UTF-8 encoding.
563+
564+header
565+ :: Text -- ^ Header name
566+ -> Text -- ^ Header value
567+ -> Option
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 }
550572
551573----------------------------------------------------------------------------
552574-- Request — Optional parameters — Cookies
@@ -554,20 +576,28 @@ header = undefined
554576-- TODO No idea right now.
555577
556578----------------------------------------------------------------------------
557−-- Request — Optional parameters — Authorization
579+-- Request — Optional parameters — Authentication
558580
559−-- Hmm, need to take a look at wreq first…
581+-- TODO basicAuth
582+-- TODO oAuth1
583+-- TODO oAuth2Bearer
584+-- TODO oAuth2Token
585+-- TODO awsAuth
560586
561587----------------------------------------------------------------------------
562588-- Request — Optional parameters — Other
563589
564−port :: Word -> Option
565−port = undefined -- TODO
590+-- | Specify the port to connect to explicitly. Normally, 'Url' you use
591+-- determines default port, @80@ for HTTP and @443@ for HTTPS, this 'Option'
592+-- allows to choose arbitrary port overwriting the defaults.
566593
567−-- redirectCount :: Word -> Option
568−-- redirectCount = undefined -- TODO
594+port :: Word -> Option
595+port n = withRequest $ \x ->
596+ x { L.port = fromIntegral n }
569597
570−-- etc.
598+-- TODO decompress
599+-- TODO responseTimeout
600+-- TODO requestVersion
571601
572602----------------------------------------------------------------------------
573603-- Response
req.cabal+1
@@ -60,6 +60,7 @@ library
6060 , base >= 4.7 && < 5.0
6161 , binary >= 0.8 && < 0.9
6262 , bytestring >= 0.2 && < 0.11
63+ , case-insensitive >= 0.2 && < 1.3
6364 , connection >= 0.2.2 && < 0.3
6465 , data-default-class
6566 , http-client >= 0.5 && < 0.6