diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-17 23:46:48 +0000 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-17 23:46:48 +0000 |
| commit | 072d7ad172d114121ae0d50f8c7841ace9699965 (patch) | |
| tree | ce42a9c3fc4210b84e77f5f50a109339d4ba3772 /client/src | |
| parent | 39ae58ddbe3f59f99e4ce2e3d68cfdbccaaec202 (diff) | |
| download | thermoprint-072d7ad172d114121ae0d50f8c7841ace9699965.tar thermoprint-072d7ad172d114121ae0d50f8c7841ace9699965.tar.gz thermoprint-072d7ad172d114121ae0d50f8c7841ace9699965.tar.bz2 thermoprint-072d7ad172d114121ae0d50f8c7841ace9699965.tar.xz thermoprint-072d7ad172d114121ae0d50f8c7841ace9699965.zip | |
Started work on client library
Diffstat (limited to 'client/src')
| -rw-r--r-- | client/src/Thermoprint/Client.hs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/client/src/Thermoprint/Client.hs b/client/src/Thermoprint/Client.hs new file mode 100644 index 0000000..8299821 --- /dev/null +++ b/client/src/Thermoprint/Client.hs | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | {-# LANGUAGE DataKinds #-} | ||
| 2 | {-# LANGUAGE TypeOperators #-} | ||
| 3 | {-# LANGUAGE ViewPatterns #-} | ||
| 4 | {-# LANGUAGE RecordWildCards #-} | ||
| 5 | |||
| 6 | -- | A client library for 'Thermoprint.API' | ||
| 7 | module Thermoprint.Client | ||
| 8 | ( Client(..) | ||
| 9 | , mkClient | ||
| 10 | -- = Reexports | ||
| 11 | , ServantError(..) | ||
| 12 | , module Servant.Common.BaseUrl | ||
| 13 | , module Control.Monad.Trans.Either | ||
| 14 | , module Servant.Server.Internal.Enter | ||
| 15 | ) where | ||
| 16 | |||
| 17 | import Thermoprint.API | ||
| 18 | import Data.Map (Map) | ||
| 19 | import Data.Sequence (Seq) | ||
| 20 | import Data.Time (UTCTime) | ||
| 21 | |||
| 22 | import Servant.Client hiding (HasClient(..)) | ||
| 23 | import qualified Servant.Client as S | ||
| 24 | import Servant.Common.BaseUrl | ||
| 25 | import Servant.API | ||
| 26 | import Servant.Server.Internal.Enter | ||
| 27 | import Control.Monad.Trans.Either | ||
| 28 | |||
| 29 | import Control.Category | ||
| 30 | import Prelude hiding (id, (.)) | ||
| 31 | |||
| 32 | -- | All 'ThermoprintAPI'-functions as a record | ||
| 33 | -- | ||
| 34 | -- Use like this: | ||
| 35 | -- | ||
| 36 | -- > import Control.Category | ||
| 37 | -- > import Prelude hiding (id, (.)) | ||
| 38 | -- > | ||
| 39 | -- > main :: IO () | ||
| 40 | -- > main = do | ||
| 41 | -- > Client{..} <- mkClient id $ Http "localhost" 3000 | ||
| 42 | -- > | ||
| 43 | -- > print =<< runEitherT printers -- Display a list of printers with their status | ||
| 44 | data Client m = Client | ||
| 45 | { printers :: EitherT ServantError m (Map PrinterId PrinterStatus) | ||
| 46 | -- ^ List all printers | ||
| 47 | , jobs :: Maybe PrinterId | ||
| 48 | -> Maybe JobId | ||
| 49 | -> Maybe JobId | ||
| 50 | -> Maybe UTCTime | ||
| 51 | -> Maybe UTCTime | ||
| 52 | -> EitherT ServantError m (Seq (JobId, UTCTime, JobStatus)) | ||
| 53 | -- ^ @jobs p minId maxId minTime maxTime@ lists a selection of jobs | ||
| 54 | , print :: Maybe PrinterId -> Printout -> EitherT ServantError m JobId | ||
| 55 | -- ^ Send a 'Printout' to be queued | ||
| 56 | , jobContents :: JobId -> EitherT ServantError m Printout | ||
| 57 | , jobStatus :: JobId -> EitherT ServantError m JobStatus | ||
| 58 | , jobDelete :: JobId -> EitherT ServantError m () | ||
| 59 | } | ||
| 60 | |||
| 61 | withArg :: (a -> layoutA :<|> layoutB) -> (a -> layoutA) :<|> (a -> layoutB) | ||
| 62 | withArg outer = (\(a :<|> _) -> a) . outer :<|> (\(_ :<|> b) -> b) . outer | ||
| 63 | |||
| 64 | mkClient :: (IO :~> m) -- ^ A monad functor ('Nat') used to make the api functions work in any monad which can be constructed from 'IO' | ||
| 65 | -> BaseUrl | ||
| 66 | -> Client m | ||
| 67 | -- ^ Generate a 'Client' | ||
| 68 | mkClient n url = Client{..} | ||
| 69 | where | ||
| 70 | printers | ||
| 71 | :<|> (jobs :<|> print) | ||
| 72 | :<|> (withArg -> jobContents :<|> (withArg -> jobStatus :<|> jobDelete)) | ||
| 73 | :<|> _ | ||
| 74 | = enter (hoistNat n) $ client thermoprintAPI url | ||
