on master help sign in
Add ‘customAuth’ and ‘attachHeader’ (#42)
afabf11Mark Karpov
CHANGELOG.md+5
@@ -1,3 +1,8 @@
1+## Req 1.1.0
2+
3+* Added `customAuth` and `attachHeader` to facilitate creation of custom
4+ authentication options.
5+
1## Req 1.0.0
6## Req 1.0.0
2
7
3* Added the `reqBr` function allowing to consume `Response BodyReader`
8* Added the `reqBr` function allowing to consume `Response BodyReader`
Network/HTTP/Req.hs+17−10
@@ -170,6 +170,7 @@ module Network.HTTP.Req
170 , QueryParam (..)
170 , QueryParam (..)
171 -- *** Headers
171 -- *** Headers
172 , header
172 , header
173+ , attachHeader
173 -- *** Cookies
174 -- *** Cookies
174 -- $cookies
175 -- $cookies
175 , cookieJar
176 , cookieJar
@@ -180,6 +181,7 @@ module Network.HTTP.Req
180 , oAuth1
181 , oAuth1
181 , oAuth2Bearer
182 , oAuth2Bearer
182 , oAuth2Token
183 , oAuth2Token
184+ , customAuth
183 -- *** Other
185 -- *** Other
184 , port
186 , port
185 , decompress
187 , decompress
@@ -1143,12 +1145,6 @@ withQueryParams f = Option (Endo (first f)) Nothing
1143withRequest :: (L.Request -> L.Request) -> Option scheme
1145withRequest :: (L.Request -> L.Request) -> Option scheme
1144withRequest f = Option (Endo (second f)) Nothing
1146withRequest f = Option (Endo (second f)) Nothing
1145
1147
1146−-- | A helper to create an 'Option' that adds a finalizer (an IO-enabled
1147−-- request transformation that is applied after all other modifications).
1148−
1149−asFinalizer :: (L.Request -> IO L.Request) -> Option scheme
1150−asFinalizer = Option mempty . pure
1151−
1152instance RequestComponent (Option scheme) where
1148instance RequestComponent (Option scheme) where
1153 getRequestMod (Option f _) = Endo $ \x ->
1149 getRequestMod (Option f _) = Endo $ \x ->
1154 let (qparams, x') = appEndo f ([], x)
1150 let (qparams, x') = appEndo f ([], x)
@@ -1229,6 +1225,8 @@ header name value = withRequest (attachHeader name value)
1229
1225
1230-- | A non-public helper that attaches a header with given name and content
1226-- | A non-public helper that attaches a header with given name and content
1231-- to a 'L.Request'.
1227-- to a 'L.Request'.
1228+--
1229+-- @since 1.1.0
1232
1230
1233attachHeader :: ByteString -> ByteString -> L.Request -> L.Request
1231attachHeader :: ByteString -> ByteString -> L.Request -> L.Request
1234attachHeader name value x =
1232attachHeader name value x =
@@ -1283,7 +1281,7 @@ basicAuthUnsafe
1283 :: ByteString -- ^ Username
1281 :: ByteString -- ^ Username
1284 -> ByteString -- ^ Password
1282 -> ByteString -- ^ Password
1285 -> Option scheme -- ^ Auth 'Option'
1283 -> Option scheme -- ^ Auth 'Option'
1286−basicAuthUnsafe username password = asFinalizer
1284+basicAuthUnsafe username password = customAuth
1287 (pure . L.applyBasicAuth username password)
1285 (pure . L.applyBasicAuth username password)
1288
1286
1289-- | The 'Option' adds OAuth1 authentication.
1287-- | The 'Option' adds OAuth1 authentication.
@@ -1297,7 +1295,7 @@ oAuth1
1297 -> ByteString -- ^ OAuth token secret
1295 -> ByteString -- ^ OAuth token secret
1298 -> Option scheme -- ^ Auth 'Option'
1296 -> Option scheme -- ^ Auth 'Option'
1299oAuth1 consumerToken consumerSecret token tokenSecret =
1297oAuth1 consumerToken consumerSecret token tokenSecret =
1300− asFinalizer (OAuth.signOAuth app creds)
1298+ customAuth (OAuth.signOAuth app creds)
1301 where
1299 where
1302 app = OAuth.newOAuth
1300 app = OAuth.newOAuth
1303 { OAuth.oauthConsumerKey = consumerToken
1301 { OAuth.oauthConsumerKey = consumerToken
@@ -1316,7 +1314,7 @@ oAuth1 consumerToken consumerSecret token tokenSecret =
1316oAuth2Bearer
1314oAuth2Bearer
1317 :: ByteString -- ^ Token
1315 :: ByteString -- ^ Token
1318 -> Option 'Https -- ^ Auth 'Option'
1316 -> Option 'Https -- ^ Auth 'Option'
1319−oAuth2Bearer token = asFinalizer
1317+oAuth2Bearer token = customAuth
1320 (pure . attachHeader "Authorization" ("Bearer " <> token))
1318 (pure . attachHeader "Authorization" ("Bearer " <> token))
1321
1319
1322-- | The 'Option' adds a not-quite-standard OAuth2 bearer token (that seems
1320-- | The 'Option' adds a not-quite-standard OAuth2 bearer token (that seems
@@ -1332,9 +1330,18 @@ oAuth2Bearer token = asFinalizer
1332oAuth2Token
1330oAuth2Token
1333 :: ByteString -- ^ Token
1331 :: ByteString -- ^ Token
1334 -> Option 'Https -- ^ Auth 'Option'
1332 -> Option 'Https -- ^ Auth 'Option'
1335−oAuth2Token token = asFinalizer
1333+oAuth2Token token = customAuth
1336 (pure . attachHeader "Authorization" ("token " <> token))
1334 (pure . attachHeader "Authorization" ("token " <> token))
1337
1335
1336+-- | A helper to create custom authentication 'Option's. The given
1337+-- 'IO'-enabled request transformation is applied after all other
1338+-- modifications when constructing a request. Use wisely.
1339+--
1340+-- @since 1.1.0
1341+
1342+customAuth :: (L.Request -> IO L.Request) -> Option scheme
1343+customAuth = Option mempty . pure
1344+
1338----------------------------------------------------------------------------
1345----------------------------------------------------------------------------
1339-- Request—Optional parameters—Other
1346-- Request—Optional parameters—Other
1340
1347