Handle Cookies correctly for RunStreamingClient

Makes `performWithStreamingRequest` take into consideration the
CookieJar, which it previously didn't.

It's now possible to e.g. use the servant-client generated functions to
access a streaming endpoint that depends on a cookie being set to
authenticate the user

Fixes #1605
This commit is contained in:
romes 2022-09-01 23:54:36 +02:00
parent f0e2316895
commit e455fa9173
2 changed files with 25 additions and 4 deletions

10
changelog.d/1606 Normal file
View File

@ -0,0 +1,10 @@
synopsis: Handle Cookies correctly for RunStreamingClient
prs: #1606
issues: #1605
description: {
Makes performWithStreamingRequest take into consideration the
CookieJar, which it previously didn't.
}

View File

@ -174,10 +174,21 @@ performRequest acceptStatus req = do
-- | TODO: support UVerb ('acceptStatus' argument, like in 'performRequest' above).
performWithStreamingRequest :: Request -> (StreamingResponse -> IO a) -> ClientM a
performWithStreamingRequest req k = do
m <- asks manager
burl <- asks baseUrl
createClientRequest <- asks makeClientRequest
request <- liftIO $ createClientRequest burl req
ClientEnv m burl cookieJar' createClientRequest <- ask
clientRequest <- liftIO $ createClientRequest burl req
request <- case cookieJar' of
Nothing -> pure clientRequest
Just cj -> liftIO $ do
now <- getCurrentTime
atomically $ do
oldCookieJar <- readTVar cj
let (newRequest, newCookieJar) =
Client.insertCookiesIntoRequest
clientRequest
oldCookieJar
now
writeTVar cj newCookieJar
pure newRequest
ClientM $ lift $ lift $ Codensity $ \k1 ->
Client.withResponse request m $ \res -> do
let status = Client.responseStatus res