From 3d66412eae85525694ed185a3b46e761c65857e4 Mon Sep 17 00:00:00 2001 From: Boyd Stephen Smith Jr Date: Sun, 16 Mar 2014 15:13:32 -0500 Subject: [PATCH] Use same manager for multiple requests. This is accomplished by adding a ReaderT layer to our monad, pushing the manager allocation to the user. For convenience we now supply a default evaluation strategy that is roughly equivalent to how we were hanlding manager allocation before, but only doing it once, no matter how many requests are in the monadic action. --- src/Cryptsy/API/Public.hs | 17 +++++++++++++++++ src/Cryptsy/API/Public/Internal.hs | 9 +++------ src/Cryptsy/API/Public/Types/Monad.hs | 8 +++++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Cryptsy/API/Public.hs b/src/Cryptsy/API/Public.hs index 6ee6e53..de78898 100644 --- a/src/Cryptsy/API/Public.hs +++ b/src/Cryptsy/API/Public.hs @@ -6,12 +6,29 @@ module Cryptsy.API.Public , module Cryptsy.API.Public.Market , module Cryptsy.API.Public.OrderData , module Cryptsy.API.Public.OrderBook + , defaultEvalPubCryptsy ) where +-- either +import Control.Monad.Trans.Either (runEitherT) + +-- http-client +import Network.HTTP.Client (withManager) + +-- http-client-tls +import Network.HTTP.Client.TLS (tlsManagerSettings) + +-- transformers +import Control.Monad.Trans.Reader (runReaderT) + +-- this package import Cryptsy.API.Public.Market import Cryptsy.API.Public.MarketData.New import Cryptsy.API.Public.MarketData.Old import Cryptsy.API.Public.OrderBook import Cryptsy.API.Public.OrderData import Cryptsy.API.Public.Types + +defaultEvalPubCryptsy :: PubCryptsy a -> IO (Either CryptsyError a) +defaultEvalPubCryptsy = withManager tlsManagerSettings . (runEitherT .) . runReaderT diff --git a/src/Cryptsy/API/Public/Internal.hs b/src/Cryptsy/API/Public/Internal.hs index b73c1c2..1643585 100644 --- a/src/Cryptsy/API/Public/Internal.hs +++ b/src/Cryptsy/API/Public/Internal.hs @@ -15,12 +15,9 @@ import Data.Either.Combinators (mapLeft) -- http-client import Network.HTTP.Client - ( parseUrl, responseBody, responseCookieJar, withManager + ( parseUrl, responseBody, responseCookieJar ) --- http-client-tls -import Network.HTTP.Client.TLS (tlsManagerSettings) - -- pipes-attoparsec import Pipes.Attoparsec (parse) @@ -31,6 +28,7 @@ import Pipes.HTTP (withHTTP) import Data.Text (Text, pack) -- transformers +import Control.Monad.Trans.Reader (ReaderT(..)) import Control.Monad.Trans.State.Strict (evalStateT) -- unordered-containers @@ -62,8 +60,7 @@ errMsgKey = pack "error" pubCryptsy :: String -- ^ URL -> (Value -> Parser a) -> PubCryptsy a -pubCryptsy apiurl parser = EitherT . withManager tlsManagerSettings - $ \manager -> runEitherT $ do +pubCryptsy apiurl parser = ReaderT $ \manager -> do req <- hoistEither . mapLeft (BadURL apiurl) $ parseUrl apiurl (parseResult, _cookieJar) <- EitherT . fmap (mapLeft (FailReadResponse req)) diff --git a/src/Cryptsy/API/Public/Types/Monad.hs b/src/Cryptsy/API/Public/Types/Monad.hs index e0305ce..3817ee2 100644 --- a/src/Cryptsy/API/Public/Types/Monad.hs +++ b/src/Cryptsy/API/Public/Types/Monad.hs @@ -8,8 +8,14 @@ where -- either import Control.Monad.Trans.Either (EitherT) +-- http-client +import Network.HTTP.Client (Manager) + +-- transformers +import Control.Monad.Trans.Reader (ReaderT) + -- this package import Cryptsy.API.Public.Types.Error -- |request monad -type PubCryptsy = EitherT CryptsyError IO +type PubCryptsy = ReaderT Manager (EitherT CryptsyError IO) -- 2.11.4.GIT