Skip to content

Instantly share code, notes, and snippets.

@fizruk
Created July 22, 2016 22:04
Show Gist options
  • Save fizruk/0e1b87d8a917d479f02ca5148861c9ae to your computer and use it in GitHub Desktop.
Save fizruk/0e1b87d8a917d479f02ca5148861c9ae to your computer and use it in GitHub Desktop.
Servant Client structure example (for servant-0.8)
{-# 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