[feat] no unnecessary conversion, add more docs in README
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
mangoiv 2024-08-24 10:35:54 +02:00
parent d5d506ffc6
commit 749669dd23
Signed by: mangoiv
SSH key fingerprint: SHA256:JlsRe4zkmS13EG6gMFNjv13Lw5rtoMPu3Lq69ZQTKF8
3 changed files with 17 additions and 3 deletions

View file

@ -8,3 +8,15 @@ combinator library.
You may use it if you just have to vomit out a quick script and don't want to deal with all the complexities of involving a library You may use it if you just have to vomit out a quick script and don't want to deal with all the complexities of involving a library
that provides more features or more safety guarantees. that provides more features or more safety guarantees.
## Example
```haskell
λ GHCi ~ res <- get_ "https://jsonplaceholder.typicode.com/posts" [q_ "userId" "1"]
res :: Data.ByteString.Lazy.Internal.LazyByteString
(0.40 secs, 6,838,760 bytes)
λ GHCi ~ decode @Value res
Just (Array [Object (fromList [("body",String "quia et suscipit\nsuscipit ...
it :: Maybe Value
(0.01 secs, 3,324,080 bytes)
```

View file

@ -47,6 +47,7 @@ library
DataKinds DataKinds
DerivingVia DerivingVia
OverloadedRecordDot OverloadedRecordDot
OverloadedStrings
TypeFamilies TypeFamilies
test-suite http-barf-test test-suite http-barf-test

View file

@ -27,6 +27,7 @@ import Control.Monad (when)
import Control.Monad.IO.Class (MonadIO (liftIO)) import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Aeson (Value) import Data.Aeson (Value)
import Data.Aeson qualified as Aeson import Data.Aeson qualified as Aeson
import Data.ByteString (StrictByteString)
import Data.ByteString.Char8 qualified as BS8 import Data.ByteString.Char8 qualified as BS8
import Data.ByteString.Lazy (LazyByteString) import Data.ByteString.Lazy (LazyByteString)
import Data.List qualified as List import Data.List qualified as List
@ -134,12 +135,12 @@ post_
-> m LazyByteString -> m LazyByteString
post_ = httpWithManager "POST" post_ = httpWithManager "POST"
buildRequestFromReq :: String -> String -> Req' -> IO Request buildRequestFromReq :: StrictByteString -> String -> Req' -> IO Request
buildRequestFromReq method url req = do buildRequestFromReq method url req = do
r <- r <-
setQueryString (foldMap (\(s, s') -> [(BS8.pack s, Just (BS8.pack s'))]) req.queryParams) setQueryString (foldMap (\(s, s') -> [(BS8.pack s, Just (BS8.pack s'))]) req.queryParams)
. (\r -> r {requestBody = RequestBodyLBS $ Aeson.encode req.jsonBody}) . (\r -> r {requestBody = RequestBodyLBS $ Aeson.encode req.jsonBody})
. (\r -> r {method = BS8.pack method}) . (\r -> r {method = method})
<$> parseRequest url <$> parseRequest url
let err = hPutStrLn stderr let err = hPutStrLn stderr
when req.inspectRequest do when req.inspectRequest do
@ -152,7 +153,7 @@ buildRequestFromReq method url req = do
exitFailure exitFailure
pure r pure r
httpWithManager :: MonadIO m => String -> String -> Req -> m LazyByteString httpWithManager :: MonadIO m => StrictByteString -> String -> Req -> m LazyByteString
httpWithManager method url req = liftIO do httpWithManager method url req = liftIO do
r <- buildRequestFromReq method url $ req.appReq defaultReq' r <- buildRequestFromReq method url $ req.appReq defaultReq'
manager <- newManager if r.secure then tlsManagerSettings else defaultManagerSettings manager <- newManager if r.secure then tlsManagerSettings else defaultManagerSettings