2019-05-17 04:25:58 +03:00
|
|
|
{-
|
|
|
|
- TODO When making a request, handle the case where the request id is
|
|
|
|
already in use.
|
|
|
|
-}
|
2019-05-16 03:00:10 +03:00
|
|
|
|
|
|
|
module Vere.Http.Client where
|
|
|
|
|
|
|
|
import ClassyPrelude
|
2019-05-17 04:45:03 +03:00
|
|
|
import Vere.Http
|
2019-05-17 03:05:34 +03:00
|
|
|
|
2019-05-17 04:25:58 +03:00
|
|
|
import qualified Network.HTTP.Client as H
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type ReqId = Word
|
|
|
|
|
2019-05-17 04:45:03 +03:00
|
|
|
data Ev = Receive ReqId Event -- [%receive @ todo]
|
2019-05-16 03:00:10 +03:00
|
|
|
|
|
|
|
data Eff
|
2019-05-17 04:45:03 +03:00
|
|
|
= NewReq ReqId Request -- [%request @ todo]
|
|
|
|
| CancelReq ReqId -- [%cancel-request @]
|
2019-05-17 04:25:58 +03:00
|
|
|
|
|
|
|
data State = State
|
|
|
|
{ sManager :: H.Manager
|
2019-05-17 04:45:03 +03:00
|
|
|
, sLive :: TVar (Map ReqId (Async ()))
|
2019-05-17 04:25:58 +03:00
|
|
|
, sChan :: MVar Ev
|
|
|
|
}
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
cvtReq :: Request -> H.Request
|
|
|
|
cvtReq = undefined
|
|
|
|
|
|
|
|
cvtRespHeaders :: H.Response a -> ResponseHeader
|
|
|
|
cvtRespHeaders resp = undefined
|
2019-05-17 03:05:34 +03:00
|
|
|
|
2019-05-17 04:25:58 +03:00
|
|
|
--------------------------------------------------------------------------------
|
2019-05-17 03:05:34 +03:00
|
|
|
|
|
|
|
initState :: IO State
|
2019-05-17 04:25:58 +03:00
|
|
|
initState = State <$> H.newManager H.defaultManagerSettings
|
|
|
|
<*> newTVarIO mempty
|
|
|
|
<*> newEmptyMVar
|
2019-05-17 03:05:34 +03:00
|
|
|
|
2019-05-17 04:25:58 +03:00
|
|
|
emit :: State -> Ev -> IO ()
|
2019-05-17 04:45:03 +03:00
|
|
|
emit st event = putMVar (sChan st) event
|
2019-05-17 03:05:34 +03:00
|
|
|
|
2019-05-17 04:25:58 +03:00
|
|
|
runEff :: State -> Eff -> IO ()
|
2019-05-17 04:45:03 +03:00
|
|
|
runEff st = \case CancelReq id -> cancelReq st id
|
|
|
|
NewReq id req -> newReq st id req
|
2019-05-17 03:05:34 +03:00
|
|
|
|
2019-05-17 04:25:58 +03:00
|
|
|
newReq :: State -> ReqId -> Request -> IO ()
|
2019-05-17 04:45:03 +03:00
|
|
|
newReq st id req = do async <- runReq st id req
|
|
|
|
atomically $ modifyTVar (sLive st) (insertMap id async)
|
|
|
|
|
|
|
|
waitCancel :: Async a -> IO (Either SomeException a)
|
|
|
|
waitCancel async = cancel async >> waitCatch async
|
|
|
|
|
|
|
|
cancelThread :: State -> ReqId -> Async a -> IO ()
|
|
|
|
cancelThread st id =
|
|
|
|
waitCancel >=> \case Left _ -> emit st (Receive id Canceled)
|
|
|
|
Right _ -> pure ()
|
2019-05-17 03:05:34 +03:00
|
|
|
|
2019-05-17 04:25:58 +03:00
|
|
|
cancelReq :: State -> ReqId -> IO ()
|
|
|
|
cancelReq st id =
|
2019-05-17 03:05:34 +03:00
|
|
|
join $ atomically $ do
|
2019-05-17 04:25:58 +03:00
|
|
|
tbl <- readTVar (sLive st)
|
|
|
|
case lookup id tbl of
|
2019-05-17 04:45:03 +03:00
|
|
|
Nothing -> pure (pure ())
|
|
|
|
Just async -> do writeTVar (sLive st) (deleteMap id tbl)
|
|
|
|
pure (cancelThread st id async)
|
|
|
|
|
|
|
|
runReq :: State -> ReqId -> Request -> IO (Async ())
|
|
|
|
runReq st id req = async (H.withResponse (cvtReq req) (sManager st) exec)
|
2019-05-17 03:05:34 +03:00
|
|
|
where
|
2019-05-17 04:25:58 +03:00
|
|
|
recv :: H.BodyReader -> IO (Maybe ByteString)
|
|
|
|
recv read = read <&> \case chunk | null chunk -> Nothing
|
|
|
|
| otherwise -> Just chunk
|
2019-05-17 04:45:03 +03:00
|
|
|
|
|
|
|
exec :: H.Response H.BodyReader -> IO ()
|
|
|
|
exec resp = do
|
|
|
|
let headers = cvtRespHeaders resp
|
|
|
|
getChunk = recv (H.responseBody resp)
|
|
|
|
loop = getChunk >>= \case
|
|
|
|
Just bs -> emit st (Receive id $ Received bs) >> loop
|
|
|
|
Nothing -> emit st (Receive id Done)
|
|
|
|
emit st (Receive id $ Started headers)
|
|
|
|
loop
|