Created
July 22, 2016 22:04
-
-
Save fizruk/0e1b87d8a917d479f02ca5148861c9ae to your computer and use it in GitHub Desktop.
Servant Client structure example (for servant-0.8)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE RecordWildCards #-} | |
{-# LANGUAGE TypeOperators #-} | |
module ApiClient where | |
import Data.Aeson | |
import Data.Proxy | |
import Network.HTTP.Client (Manager) | |
import Servant.API | |
import Servant.Client | |
type Bar = String | |
type Foo = String | |
type FooId = Int | |
type API | |
= "bar" :> Get '[JSON] Bar | |
:<|> "foo" :> Capture "foo_id" FooId :> | |
( "a" :> Get '[JSON] Foo | |
:<|> "b" :> Get '[JSON] Foo ) | |
data ApiClient = ApiClient | |
{ getBar :: Manager -> BaseUrl -> ClientM Bar | |
, mkFooClient :: FooId -> FooClient | |
} | |
data FooClient = FooClient | |
{ getFooA :: Manager -> BaseUrl -> ClientM Foo | |
, getFooB :: Manager -> BaseUrl -> ClientM Foo | |
} | |
mkClient :: ApiClient | |
mkClient = ApiClient{..} | |
where | |
apiClient = client (Proxy :: Proxy API) | |
getBar :<|> fooClient = apiClient | |
mkFooClient fooId = FooClient{..} | |
where | |
getFooA :<|> getFooB = fooClient fooId |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment