aboutsummaryrefslogtreecommitdiff
path: root/client/src
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2017-03-07 14:16:21 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2017-03-07 14:16:21 +0100
commit7065a8cc1b8b01cd32d4b1d5317b323fec5238bd (patch)
treed6608f463b066aa6caf33357fd9ae0e508e49084 /client/src
parent75d9fe614dca572aa1d7cfa53553e9c103eb2dd0 (diff)
downloadthermoprint-7065a8cc1b8b01cd32d4b1d5317b323fec5238bd.tar
thermoprint-7065a8cc1b8b01cd32d4b1d5317b323fec5238bd.tar.gz
thermoprint-7065a8cc1b8b01cd32d4b1d5317b323fec5238bd.tar.bz2
thermoprint-7065a8cc1b8b01cd32d4b1d5317b323fec5238bd.tar.xz
thermoprint-7065a8cc1b8b01cd32d4b1d5317b323fec5238bd.zip
Bump versions
Diffstat (limited to 'client/src')
-rw-r--r--client/src/Thermoprint/Client.hs55
1 files changed, 14 insertions, 41 deletions
diff --git a/client/src/Thermoprint/Client.hs b/client/src/Thermoprint/Client.hs
index 448a912..46f6073 100644
--- a/client/src/Thermoprint/Client.hs
+++ b/client/src/Thermoprint/Client.hs
@@ -5,12 +5,12 @@
5{-# LANGUAGE RecordWildCards #-} 5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE FlexibleContexts #-} 6{-# LANGUAGE FlexibleContexts #-}
7{-# LANGUAGE FlexibleInstances #-} 7{-# LANGUAGE FlexibleInstances #-}
8{-# LANGUAGE RankNTypes #-}
8 9
9-- | A client library for 'Thermoprint.API' 10-- | A client library for 'Thermoprint.API'
10module Thermoprint.Client 11module Thermoprint.Client
11 ( Client(..) 12 ( Client(..)
12 , mkClient, mkClient' 13 , mkClient, mkClient'
13 , throwNat, ioNat
14 -- = Reexports 14 -- = Reexports
15 , ServantError(..) 15 , ServantError(..)
16 , module Thermoprint.API 16 , module Thermoprint.API
@@ -27,10 +27,11 @@ import Data.Time (UTCTime)
27import Servant.Client hiding (HasClient(..)) 27import Servant.Client hiding (HasClient(..))
28import qualified Servant.Client as S 28import qualified Servant.Client as S
29import Servant.Common.BaseUrl 29import Servant.Common.BaseUrl
30import Servant.Common.Req
30import Network.HTTP.Client (Manager, ManagerSettings, newManager, defaultManagerSettings) 31import Network.HTTP.Client (Manager, ManagerSettings, newManager, defaultManagerSettings)
31import Servant.API 32import Servant.API
32import Servant.Utils.Enter 33import Servant.Utils.Enter
33import Control.Monad.Except (ExceptT, runExceptT) 34import Control.Monad.Except (ExceptT(..), runExceptT)
34 35
35import Control.Monad.Reader (ReaderT, runReaderT, ask) 36import Control.Monad.Reader (ReaderT, runReaderT, ask)
36import Control.Monad.Catch (Exception, MonadThrow(..)) 37import Control.Monad.Catch (Exception, MonadThrow(..))
@@ -92,29 +93,10 @@ withArgs subAPI = (\(a :<|> _) -> a) . subAPI :<|> (\(_ :<|> b) -> b) . subAPI
92 93
93mkClientS :: Monad m 94mkClientS :: Monad m
94 => (ClientM :~> m) -- ^ A monad functor ('Nat') used to make the api functions work in any monad which can do 'IO' and handle errors 95 => (ClientM :~> m) -- ^ A monad functor ('Nat') used to make the api functions work in any monad which can do 'IO' and handle errors
95 -> ManagerSettings
96 -> BaseUrl
97 -> Client m 96 -> Client m
98-- ^ Generate a 'Client' 97-- ^ Generate a 'Client'
99mkClientS n mgrS url = Client 98mkClientS n = Client{..}
100 { printers = clientF n printers
101 , jobs = \a b c -> clientF n $ jobs a b c
102 , jobCreate = \a b -> clientF n $ jobCreate a b
103 , job = \a -> clientF n $ job a
104 , jobStatus = \a -> clientF n $ jobStatus a
105 , jobDelete = \a -> clientF n $ jobDelete a
106 , drafts = clientF n drafts
107 , draftCreate = \a b -> clientF n $ draftCreate a b
108 , draftReplace = \a b c -> clientF n $ draftReplace a b c
109 , draft = \a -> clientF n $ draft a
110 , draftDelete = \a -> clientF n $ draftDelete a
111 , draftPrint = \a b -> clientF n $ draftPrint a b
112 }
113 where 99 where
114 clientF :: Monad m => (ClientM :~> m) -> (Manager -> BaseUrl -> m a) -> m a
115 clientF n f = do
116 mgr <- unNat n $ (liftIO $ newManager mgrS :: ClientM Manager)
117 f mgr url
118 printers 100 printers
119 :<|> (jobs :<|> jobCreate) 101 :<|> (jobs :<|> jobCreate)
120 :<|> (withArgs -> job :<|> (withArgs -> jobStatus :<|> jobDelete)) 102 :<|> (withArgs -> job :<|> (withArgs -> jobStatus :<|> jobDelete))
@@ -122,23 +104,14 @@ mkClientS n mgrS url = Client
122 :<|> (withArgs -> draftReplace :<|> (withArgs -> draft :<|> (withArgs -> draftDelete :<|> draftPrint))) 104 :<|> (withArgs -> draftReplace :<|> (withArgs -> draft :<|> (withArgs -> draftDelete :<|> draftPrint)))
123 = enter n $ client thermoprintAPI 105 = enter n $ client thermoprintAPI
124 106
125mkClient :: Monad m 107mkClient :: (MonadThrow m, MonadIO m) => ManagerSettings -> BaseUrl -> Client m
126 => (ClientM :~> m) -- ^ A monad functor ('Nat') used to make the api functions work in any monad which can do 'IO' and handle errors 108mkClient mSettings url = mkClientS $ Nat clientNat
127 -> BaseUrl 109 where
128 -> Client m 110 clientNat :: forall m a. (MonadThrow m, MonadIO m) => ClientM a -> m a
129mkClient n url = mkClientS n defaultManagerSettings url 111 clientNat cAct = do
130 112 mgr <- liftIO $ newManager mSettings
131mkClient' :: (MonadThrow m, MonadIO m) => BaseUrl -> Client m 113 either throwM return =<< liftIO (runClientM cAct $ ClientEnv mgr url)
132-- ^ @mkClient' = mkClient $ ioNat . throwNat@
133mkClient' = mkClient $ ioNat . throwNat
134
135throwNat :: (Exception e, MonadThrow m) => ExceptT e m :~> m
136-- ^ Squash a layer of 'EitherT' into the underlying monad supporting 'throwM'
137throwNat = Nat $ either throwM return <=< runExceptT
138
139ioNat :: MonadIO m => IO :~> m
140-- ^ @ioNat = Nat liftIO@
141ioNat = Nat liftIO
142 114
143readerNat :: a -> ReaderT a m :~> m 115mkClient' :: (MonadThrow m, MonadIO m) => BaseUrl -> Client m
144readerNat a = Nat $ flip runReaderT a 116-- ^ @mkClient' = mkClient defaultManagerSettings
117mkClient' = mkClient defaultManagerSettings