@@ -11,7 +11,7 @@
1111{-# LANGUAGE UndecidableInstances #-}
1212{-# LANGUAGE TypeFamilies #-}
1313{-# LANGUAGE FlexibleContexts #-}
14−-- should all the NormalizeFunction instances be in one place?
14+-- should all the NormalizeFunction instances be in one place?
1515{-# OPTIONS_GHC -fno-warn-orphans #-}
1616module Roboservant.Client where
1717
@@ -27,13 +27,14 @@ import qualified Data.Vinyl.Curry as V
2727import qualified Data.Text as T
2828import Control.Monad.Reader
2929import Data.Hashable
30+import Network.HTTP.Types.Status
3031
3132-- fuzz :: forall api.
3233-- (FlattenServer api, ToReifiedApi (Endpoints api)) =>
3334-- Server api ->
3435-- Config ->
3536-- IO (Maybe Report)
36−-- fuzz s = fuzz' (reifyServer s)
37+-- fuzz s = fuzz' (reifyServer s)
3738-- -- todo: how do we pull reifyServer out?
3839-- where reifyServer :: (FlattenServer api, ToReifiedApi (Endpoints api))
3940-- => Server api -> ReifiedApi
@@ -80,7 +81,7 @@ instance
8081 foo :: V.Curried (EndpointArgs endpoint) (ReaderT ClientEnv IO ResultType)
8182 -> V.Curried (EndpointArgs endpoint) (IO ResultType)
8283 foo = mapCurried @(EndpointArgs endpoint) @(ReaderT ClientEnv IO ResultType) (`runReaderT` clientEnv)
83−
84+
8485mapCurried :: forall ts a b. V.RecordCurry' ts => (a -> b) -> V.Curried ts a -> V.Curried ts b
8586mapCurried f g = V.rcurry' @ts $ f . V.runcurry' g
8687
@@ -90,10 +91,15 @@ type ResultType = Either InteractionError (NonEmpty (Dynamic,Int))
9091
9192instance (Typeable x, Hashable x, Breakdown x) => NormalizeFunction (ClientM x) where
9293 type Normal (ClientM x) = ReaderT ClientEnv IO (Either InteractionError (NonEmpty (Dynamic,Int)))
93− normalize c = ReaderT $
94− fmap (bimap (InteractionError . T.pack . show) breakdown) . runClientM c
95−--
94+ normalize c = ReaderT $
95+ fmap (bimap renderClientError breakdown) . runClientM c
96+ where
97+ renderClientError :: ClientError -> InteractionError
98+ renderClientError err = case err of
99+ FailureResponse _ (Response{responseStatusCode}) -> InteractionError textual (responseStatusCode == status500)
100+ _ -> InteractionError textual True
96101
102+ where textual = T.pack $ show err
97103instance ToReifiedClientApi '[] where
98104 toReifiedClientApi NoClientEndpoints _ _ = []
99105
@@ -105,7 +111,7 @@ instance
105111 FlattenClient (endpoint :<|> api)
106112 where
107113 flattenClient (endpoint :<|> c) = endpoint `AClientEndpoint` flattenClient @api c
108−
114+
109115instance
110116 (
111117 Endpoints api ~ '[api]
@@ -114,6 +120,7 @@ instance
114120 where
115121 flattenClient c = c `AClientEndpoint` NoClientEndpoints
116122
123+
117124instance FlattenClient (Verb method statusCode contentTypes responseType)
118125 where
119126 flattenClient c = c `AClientEndpoint` NoClientEndpoints