aboutsummaryrefslogtreecommitdiff
path: root/client/src/Thermoprint/Client.hs
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/Thermoprint/Client.hs')
-rw-r--r--client/src/Thermoprint/Client.hs28
1 files changed, 23 insertions, 5 deletions
diff --git a/client/src/Thermoprint/Client.hs b/client/src/Thermoprint/Client.hs
index 46f6073..8c4d99d 100644
--- a/client/src/Thermoprint/Client.hs
+++ b/client/src/Thermoprint/Client.hs
@@ -5,7 +5,10 @@
5{-# LANGUAGE RecordWildCards #-} 5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE FlexibleContexts #-} 6{-# LANGUAGE FlexibleContexts #-}
7{-# LANGUAGE FlexibleInstances #-} 7{-# LANGUAGE FlexibleInstances #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE RankNTypes #-} 9{-# LANGUAGE RankNTypes #-}
10{-# LANGUAGE FunctionalDependencies #-}
11{-# LANGUAGE UndecidableInstances #-}
9 12
10-- | A client library for 'Thermoprint.API' 13-- | A client library for 'Thermoprint.API'
11module Thermoprint.Client 14module Thermoprint.Client
@@ -16,7 +19,7 @@ module Thermoprint.Client
16 , module Thermoprint.API 19 , module Thermoprint.API
17 , module Servant.Common.BaseUrl 20 , module Servant.Common.BaseUrl
18 , module Control.Monad.Except 21 , module Control.Monad.Except
19 , module Servant.Utils.Enter 22 , module Control.Natural
20 ) where 23 ) where
21 24
22import Thermoprint.API 25import Thermoprint.API
@@ -30,7 +33,8 @@ import Servant.Common.BaseUrl
30import Servant.Common.Req 33import Servant.Common.Req
31import Network.HTTP.Client (Manager, ManagerSettings, newManager, defaultManagerSettings) 34import Network.HTTP.Client (Manager, ManagerSettings, newManager, defaultManagerSettings)
32import Servant.API 35import Servant.API
33import Servant.Utils.Enter 36-- import Servant.Utils.Enter
37import Control.Natural ((:~>)(..))
34import Control.Monad.Except (ExceptT(..), runExceptT) 38import Control.Monad.Except (ExceptT(..), runExceptT)
35 39
36import Control.Monad.Reader (ReaderT, runReaderT, ask) 40import Control.Monad.Reader (ReaderT, runReaderT, ask)
@@ -42,6 +46,20 @@ import Control.Monad
42import Control.Category 46import Control.Category
43import Prelude hiding (id, (.)) 47import Prelude hiding (id, (.))
44 48
49
50class Enter typ mod ret | typ mod -> ret, typ ret -> mod, mod ret -> typ where
51 enter :: mod -> typ -> ret
52
53instance Enter (m a) (m :~> n) (n a) where
54 enter (NT f) = f
55
56instance (Enter typ1 mod1 ret1, Enter typ2 mod2 ret2, mod1 ~ mod2) => Enter (typ1 :<|> typ2) mod1 (ret1 :<|> ret2) where
57 enter mod (a :<|> b) = enter mod a :<|> enter mod b
58
59instance Enter typ mod ret => Enter (r -> typ) mod (r -> ret) where
60 enter mod = (enter mod .)
61
62
45-- | All 'ThermoprintAPI'-functions as a record 63-- | All 'ThermoprintAPI'-functions as a record
46-- 64--
47-- Use like this: 65-- Use like this:
@@ -105,10 +123,10 @@ mkClientS n = Client{..}
105 = enter n $ client thermoprintAPI 123 = enter n $ client thermoprintAPI
106 124
107mkClient :: (MonadThrow m, MonadIO m) => ManagerSettings -> BaseUrl -> Client m 125mkClient :: (MonadThrow m, MonadIO m) => ManagerSettings -> BaseUrl -> Client m
108mkClient mSettings url = mkClientS $ Nat clientNat 126mkClient mSettings url = mkClientS clientNat
109 where 127 where
110 clientNat :: forall m a. (MonadThrow m, MonadIO m) => ClientM a -> m a 128 clientNat :: forall m. (MonadThrow m, MonadIO m) => ClientM :~> m
111 clientNat cAct = do 129 clientNat = NT $ \cAct -> do
112 mgr <- liftIO $ newManager mSettings 130 mgr <- liftIO $ newManager mSettings
113 either throwM return =<< liftIO (runClientM cAct $ ClientEnv mgr url) 131 either throwM return =<< liftIO (runClientM cAct $ ClientEnv mgr url)
114 132