|
| 1 | +{-# LANGUAGE UndecidableInstances #-} |
| 2 | +module Test.WebApi.StateModel |
| 3 | + ( WebApiAction (..) |
| 4 | + , ApiState (..) |
| 5 | + , WebApiActionCxt |
| 6 | + , mkWebApiAction |
| 7 | + , getOpIdFromRequest |
| 8 | + ) where |
| 9 | + |
| 10 | +import Test.WebApi |
| 11 | +import Test.QuickCheck.StateModel |
| 12 | +import Test.QuickCheck.DynamicLogic (DynLogicModel (..)) |
| 13 | +import WebApi.Contract |
| 14 | +import WebApi.Param |
| 15 | +import WebApi.ContentTypes |
| 16 | +import Control.Exception (SomeException) |
| 17 | +import Data.Kind |
| 18 | +import Data.Typeable |
| 19 | +import Data.Coerce |
| 20 | +import GHC.TypeLits |
| 21 | + |
| 22 | +type WebApiActionCxt (apps :: [Type]) meth (app :: Type) r = |
| 23 | + ( ToParam 'PathParam (PathParam meth (app://r)) |
| 24 | + , ToParam 'QueryParam (QueryParam meth (app://r)) |
| 25 | + , FromHeader (HeaderOut meth (app :// r)) |
| 26 | + , FromParam Cookie (CookieOut meth (app :// r)) |
| 27 | + , Decodings (ContentTypes meth (app :// r)) (ApiOut meth (app :// r)) |
| 28 | + , Decodings (ContentTypes meth (app :// r)) (ApiErr meth (app :// r)) |
| 29 | + , SingMethod meth |
| 30 | + , WebApi app |
| 31 | + , Typeable app |
| 32 | + , Typeable (ApiOut meth (app :// r)) |
| 33 | + , Typeable (ApiErr meth (app :// r)) |
| 34 | + , Typeable r |
| 35 | + , AppIsElem app apps |
| 36 | + , KnownSymbol (GetOpIdName (OperationId meth (app :// r))) |
| 37 | + ) |
| 38 | + |
| 39 | +data WebApiAction (apps :: [Type]) (a :: Type) where |
| 40 | + SuccessCall :: WebApiActionCxt apps meth app r => ClientRequest meth (app :// r) -> WebApiAction apps (ApiOut meth (app :// r)) |
| 41 | + ErrorCall :: WebApiActionCxt apps meth app r => ClientRequest meth (app :// r) -> WebApiAction apps (ApiErr meth (app :// r)) |
| 42 | + SomeExceptionCall :: WebApiActionCxt apps meth app r => ClientRequest meth (app :// r) -> WebApiAction apps (SomeException) |
| 43 | + |
| 44 | +instance Show (WebApiAction apps a) where |
| 45 | + show = \case |
| 46 | + SuccessCall creq -> show . toWaiRequest . fromClientRequest $ creq |
| 47 | + ErrorCall creq -> show . toWaiRequest . fromClientRequest $ creq |
| 48 | + SomeExceptionCall creq -> show . toWaiRequest . fromClientRequest $ creq |
| 49 | + |
| 50 | +-- TODO: Revisit |
| 51 | +instance Eq (WebApiAction apps a) where |
| 52 | + (==) (SuccessCall creq1) = \case |
| 53 | + SuccessCall creq2 -> (show . toWaiRequest . fromClientRequest $ creq1) == (show . toWaiRequest . fromClientRequest $ creq2) |
| 54 | + _ -> False |
| 55 | + (==) (ErrorCall creq1) = \case |
| 56 | + ErrorCall creq2 -> (show . toWaiRequest . fromClientRequest $ creq1) == (show . toWaiRequest . fromClientRequest $ creq2) |
| 57 | + _ -> False |
| 58 | + (==) (SomeExceptionCall creq1) = \case |
| 59 | + SomeExceptionCall creq2 -> (show . toWaiRequest . fromClientRequest $ creq1) == (show . toWaiRequest . fromClientRequest $ creq2) |
| 60 | + _ -> False |
| 61 | + |
| 62 | +instance HasVariables (WebApiAction apps a) where |
| 63 | + getAllVariables = mempty |
| 64 | + |
| 65 | +data ApiState (apps :: [Type]) = ApiState |
| 66 | + deriving (Show, Eq) |
| 67 | + |
| 68 | +instance HasVariables (ApiState apps) where |
| 69 | + getAllVariables = mempty |
| 70 | + |
| 71 | +mkWebApiAction :: WebApiAction apps a -> Action (ApiState apps) a |
| 72 | +mkWebApiAction = coerce |
| 73 | + |
| 74 | +instance StateModel (ApiState apps) where |
| 75 | + newtype Action (ApiState apps) a = MkWebApiAction (WebApiAction apps a) |
| 76 | + deriving newtype (Show, Eq, HasVariables) |
| 77 | + |
| 78 | + actionName = \case |
| 79 | + MkWebApiAction (SuccessCall creq) -> getOpIdFromRequest creq |
| 80 | + MkWebApiAction (ErrorCall creq) -> getOpIdFromRequest creq |
| 81 | + MkWebApiAction (SomeExceptionCall creq) -> getOpIdFromRequest creq |
| 82 | + |
| 83 | + arbitraryAction = undefined |
| 84 | + initialState = undefined |
| 85 | + |
| 86 | +instance RunModel (ApiState apps) (WebApiSessions apps) where |
| 87 | + perform _ act _ = case act of |
| 88 | + MkWebApiAction (SuccessCall creq) -> do |
| 89 | + testClients creq >>= \case |
| 90 | + Success _ out _ _ -> pure out |
| 91 | + _ -> error "Fail" |
| 92 | + MkWebApiAction (ErrorCall creq) -> do |
| 93 | + testClients creq >>= \case |
| 94 | + Failure (Left (ApiError _ err _ _)) -> pure err |
| 95 | + _ -> error "Fail" |
| 96 | + MkWebApiAction (SomeExceptionCall creq) -> do |
| 97 | + testClients creq >>= \case |
| 98 | + Failure (Right (OtherError e)) -> pure e |
| 99 | + _ -> error "Fail" |
| 100 | + |
| 101 | +instance DynLogicModel (ApiState apps) where |
| 102 | + restricted _ = False |
| 103 | + |
| 104 | +getOpIdFromRequest :: forall meth app r. (KnownSymbol (GetOpIdName (OperationId meth (app://r))), Typeable app, Typeable r) => ClientRequest meth (app://r) -> String |
| 105 | +getOpIdFromRequest _ = |
| 106 | + let |
| 107 | + routeName = symbolVal (Proxy @(GetOpIdName (OperationId meth (app://r)))) |
| 108 | + appName = show $ typeRep (Proxy @app) |
| 109 | + in appName ++ "/" ++ routeName |
| 110 | + |
| 111 | +type family GetOpIdName (oid :: OpId) :: Symbol where |
| 112 | + GetOpIdName ('OpId _ n) = n |
| 113 | + GetOpIdName ('UndefinedOpId m r) = TypeError ('Text "OperationId is not set for " ':<>: 'ShowType m ':<>: 'Text " " ':<>: 'ShowType r) |
0 commit comments