Skip to content

Instantly share code, notes, and snippets.

@jhrcek
Last active November 13, 2020 09:37
Show Gist options
  • Save jhrcek/1208813b7d7087f666a17c6036d63ec4 to your computer and use it in GitHub Desktop.
Save jhrcek/1208813b7d7087f666a17c6036d63ec4 to your computer and use it in GitHub Desktop.
Pattern matching servant client functions out of generic record
module PI.Demo where
import Servant (Handler, Put)
import Servant.API.Generic (AsApi, ToServant, fromServant)
import Servant.Client (ClientM)
import Servant.Client.Generic (AsClientT, genericClient)
import Servant.Server.Generic (AsServer, genericServer)
data Parent mode = Parent
{ parent1 :: mode :- Capture "x" Int :> Get '[JSON] String
, parent2 :: mode :- ToServant Child AsApi
} deriving (Generic)
data Child mode = Child
{ child1 :: mode :- Capture "x" Int :> Get '[JSON] String
, child2 :: mode :- ReqBody '[JSON] Int :> Put '[JSON] Bool
} deriving (Generic)
type ParentChildApi = Parent AsApi
type ParentChildServer = ToServant Parent AsServer
type ParentChildClient = Parent (AsClientT ClientM)
demoServer :: ParentChildServer
demoServer = genericServer $ Parent
{ parent1 = undefined :: Int -> Handler String
, parent2 = genericServer $ Child
{ child1 = undefined :: Int -> Handler String
, child2 = undefined :: Int -> Handler Bool
}
}
callParent1 :: Int -> ClientM String
callParent2Child1 :: Int -> ClientM String
callParent2Child2 :: Int -> ClientM Bool
Parent
{ parent1 = callParent1
, parent2 = fromServant @Child @(AsClientT ClientM) -> Child
{ child1 = callParent2Child1
, child2 = callParent2Child2
}
} = genericClient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment