@@ -1,5 +1,6 @@
11{-# LANGUAGE RankNTypes #-}
22{-# LANGUAGE TypeOperators #-}
3+{-# LANGUAGE NamedFieldPuns #-}
34{-# LANGUAGE DataKinds #-}
45{-# LANGUAGE AllowAmbiguousTypes #-}
56{-# LANGUAGE DataKinds #-}
@@ -11,7 +12,7 @@
1112{-# LANGUAGE UndecidableInstances #-}
1213{-# LANGUAGE TypeFamilies #-}
1314{-# LANGUAGE FlexibleContexts #-}
14−-- should all the NormalizeFunction instances be in one place?
15+-- should all the NormalizeFunction instances be in one place?
1516{-# OPTIONS_GHC -fno-warn-orphans #-}
1617module Roboservant.Client where
1718
@@ -27,13 +28,14 @@ import qualified Data.Vinyl.Curry as V
2728import qualified Data.Text as T
2829import Control.Monad.Reader
2930import Data.Hashable
31+import Network.HTTP.Types.Status
3032
3133-- fuzz :: forall api.
3234-- (FlattenServer api, ToReifiedApi (Endpoints api)) =>
3335-- Server api ->
3436-- Config ->
3537-- IO (Maybe Report)
36−-- fuzz s = fuzz' (reifyServer s)
38+-- fuzz s = fuzz' (reifyServer s)
3739-- -- todo: how do we pull reifyServer out?
3840-- where reifyServer :: (FlattenServer api, ToReifiedApi (Endpoints api))
3941-- => Server api -> ReifiedApi
@@ -80,7 +82,7 @@ instance
8082 foo :: V.Curried (EndpointArgs endpoint) (ReaderT ClientEnv IO ResultType)
8183 -> V.Curried (EndpointArgs endpoint) (IO ResultType)
8284 foo = mapCurried @(EndpointArgs endpoint) @(ReaderT ClientEnv IO ResultType) (`runReaderT` clientEnv)
83−
85+
8486mapCurried :: forall ts a b. V.RecordCurry' ts => (a -> b) -> V.Curried ts a -> V.Curried ts b
8587mapCurried f g = V.rcurry' @ts $ f . V.runcurry' g
8688
@@ -90,10 +92,15 @@ type ResultType = Either InteractionError (NonEmpty (Dynamic,Int))
9092
9193instance (Typeable x, Hashable x, Breakdown x) => NormalizeFunction (ClientM x) where
9294 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−--
95+ normalize c = ReaderT $
96+ fmap (bimap renderClientError breakdown) . runClientM c
97+ where
98+ renderClientError :: ClientError -> InteractionError
99+ renderClientError err = case err of
100+ FailureResponse _ (Response{responseStatusCode}) -> InteractionError textual (responseStatusCode == status500)
101+ _ -> InteractionError textual True
96102
103+ where textual = T.pack $ show err
97104instance ToReifiedClientApi '[] where
98105 toReifiedClientApi NoClientEndpoints _ _ = []
99106
@@ -105,7 +112,7 @@ instance
105112 FlattenClient (endpoint :<|> api)
106113 where
107114 flattenClient (endpoint :<|> c) = endpoint `AClientEndpoint` flattenClient @api c
108−
115+
109116instance
110117 (
111118 Endpoints api ~ '[api]
@@ -114,6 +121,7 @@ instance
114121 where
115122 flattenClient c = c `AClientEndpoint` NoClientEndpoints
116123
124+
117125instance FlattenClient (Verb method statusCode contentTypes responseType)
118126 where
119127 flattenClient c = c `AClientEndpoint` NoClientEndpoints