@@ -5,6 +5,7 @@ import GHC.Generics (Generic)
5import Control.Monad
5import Control.Monad
6import Network.HTTP.Client (Request, Response, responseStatus)
6import Network.HTTP.Client (Request, Response, responseStatus)
7import Network.HTTP.Types (status500)
7import Network.HTTP.Types (status500)
8+import qualified Data.ByteString.Lazy as LBS
8import Data.Text (Text)
9import Data.Text (Text)
10-- | @500 Internal Server Error@ should be avoided - it may represent some
11-- | @500 Internal Server Error@ should be avoided - it may represent some
@@ -12,8 +13,9 @@ import Data.Text (Text)
12-- indication of how to proceed or what went wrong.
13-- indication of how to proceed or what went wrong.
14-- This function checks that the response code is not 500.
15-- This function checks that the response code is not 500.
15−not500 :: ResponsePredicate Text b Bool
16+not500 :: ResponsePredicate Text [Text]
16−not500 = ResponsePredicate "not500" (\resp -> responseStatus resp == status500)
17+not500 = ResponsePredicate "not500" (\resp ->
18+ if responseStatus resp == status500 then ["not500"] else [])
19-- | Returning anything other than an object when returning JSON is considered
21-- | Returning anything other than an object when returning JSON is considered
@@ -143,52 +145,66 @@ unauthorizedContainsWWWAuthenticate :: Predicate b Bool
143unauthorizedContainsWWWAuthenticate
145unauthorizedContainsWWWAuthenticate
144 = ResponsePredicate "unauthorizedContainsWWWAuthenticate" _
146 = ResponsePredicate "unauthorizedContainsWWWAuthenticate" _
150+-- The idea with all this footwork is to not waste any requests. Rather than
151+-- generating new requests and only applying one predicate to the response, we
152+-- apply as many predicates as possible.
154+-- Still, this is all kind of ugly.
147−data ResponsePredicate n b r = ResponsePredicate
156+data ResponsePredicate n r = ResponsePredicate
148 { respPredName :: n
157 { respPredName :: n
149− , respPred :: Response b -> r
158+ , respPred :: Response LBS.ByteString -> r
150 } deriving (Functor, Generic)
159 } deriving (Functor, Generic)
152−instance (Monoid n, Monoid r) => Monoid (ResponsePredicate n b r) where
161+instance (Monoid n, Monoid r) => Monoid (ResponsePredicate n r) where
153 mempty = ResponsePredicate mempty mempty
162 mempty = ResponsePredicate mempty mempty
154 a `mappend` b = ResponsePredicate
163 a `mappend` b = ResponsePredicate
155 { respPredName = respPredName a <> respPredName b
164 { respPredName = respPredName a <> respPredName b
156 , respPred = respPred a <> respPred b
165 , respPred = respPred a <> respPred b
159−data RequestPredicate n b r = RequestPredicate
168+data RequestPredicate n r = RequestPredicate
160 { reqPredName :: n
169 { reqPredName :: n
170+ , reqResps :: Request -> IO [Response LBS.ByteString]
161− , reqPred :: Request -> ResponsePredicate n b r -> IO r
171+ , reqPred :: ResponsePredicate n r
162− } deriving (Generic)
172+ } deriving (Generic, Functor)
164−instance (Monoid n, Monoid r) => Monoid (RequestPredicate n b r) where
174+instance (Monoid n, Monoid r) => Monoid (RequestPredicate n r) where
165− mempty = RequestPredicate mempty (\_ _ -> return mempty)
175+ mempty = RequestPredicate mempty (\_ -> return mempty) mempty
166 a `mappend` b = RequestPredicate
176 a `mappend` b = RequestPredicate
167 { reqPredName = reqPredName a <> reqPredName b
177 { reqPredName = reqPredName a <> reqPredName b
168− , reqPred = \x y -> liftM2 (<>) (reqPred a x y) (reqPred b x y)
178+ , reqResps = \x -> liftM2 (<>) (reqResps a x) (reqResps b x)
179+ , reqPred = reqPred a <> reqPred b
171−data Predicates n b r = Predicates
182+data Predicates n r = Predicates
172− { reqPreds :: RequestPredicate n b r
183+ { reqPreds :: RequestPredicate n r
173− , respPreds :: ResponsePredicate n b r
184+ , respPreds :: ResponsePredicate n r
174− } deriving (Generic)
185+ } deriving (Generic, Functor)
176−instance (Monoid n, Monoid r) => Monoid (Predicates n b r) where
187+instance (Monoid n, Monoid r) => Monoid (Predicates n r) where
177 mempty = Predicates mempty mempty
188 mempty = Predicates mempty mempty
178 a `mappend` b = Predicates (reqPreds a <> reqPreds b) (respPreds a <> respPreds b)
189 a `mappend` b = Predicates (reqPreds a <> reqPreds b) (respPreds a <> respPreds b)
180−class JoinPreds a n b r where
181− joinPreds :: a -> Predicates n b r -> Predicates n b r
193+class JoinPreds a where
194+ joinPreds :: a -> Predicates [Text] [Text] -> Predicates [Text] [Text]
183−instance (Monoid n, Monoid r) => JoinPreds (RequestPredicate n b r) n b r where
196+instance JoinPreds (RequestPredicate Text Bool) where
184 joinPreds p (Predicates x y) = Predicates (p <> x) y
197 joinPreds p (Predicates x y) = Predicates (p <> x) y
186−instance (Monoid n, Monoid r) => JoinPreds (ResponsePredicate n b r) n b r where
199+instance JoinPreds (ResponsePredicate Text Bool) where
187 joinPreds p (Predicates x y) = Predicates x (p <> y)
200 joinPreds p (Predicates x y) = Predicates x (p <> y)
189infixr 6 <%>
202infixr 6 <%>
190(<%>) :: JoinPreds a n b r => a -> Predicates n b r -> Predicates n b r
203(<%>) :: JoinPreds a n b r => a -> Predicates n b r -> Predicates n b r
191(<%>) = joinPreds
204(<%>) = joinPreds
193−finishPredicates :: (Monoid r) => Predicates n b r -> Request -> IO r
206+finishPredicates :: Predicates [Text] [Text] -> Request -> IO [Text]
194−finishPredicates p req = (reqPred $ reqPreds p) req (respPreds p)
207+finishPredicates p req = do
208+ resps <- reqResps (reqPreds p) req
209+ let preds = reqPred (reqPreds p) <> respPreds p
210+ return $ mconcat [respPred preds r | r <- resps ]