{-# LANGUAGE ConstraintKinds     #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE KindSignatures      #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}
module  Servant.Client.Generic (
    AsClientT,
    genericClient,
    genericClientHoist,
    ) where

import           Data.Proxy
                 (Proxy (..))

import           Servant.API.Generic
import           Servant.Client.Core

-- | A type that specifies that an API record contains a client implementation.
data AsClientT (m :: * -> *)
instance GenericMode (AsClientT m) where
    type AsClientT m :- api = Client m api

-- | Generate a record of client functions.
genericClient
    :: forall routes m.
       ( HasClient m (ToServantApi routes)
       , GenericServant routes (AsClientT m)
       , Client m (ToServantApi routes) ~ ToServant routes (AsClientT m)
       )
    => routes (AsClientT m)
genericClient :: routes (AsClientT m)
genericClient
    = ToServant routes (AsClientT m) -> routes (AsClientT m)
forall (routes :: * -> *) mode.
GenericServant routes mode =>
ToServant routes mode -> routes mode
fromServant
    (ToServant routes (AsClientT m) -> routes (AsClientT m))
-> ToServant routes (AsClientT m) -> routes (AsClientT m)
forall a b. (a -> b) -> a -> b
$ Proxy (GToServant (Rep (routes AsApi)))
-> Proxy m -> Client m (GToServant (Rep (routes AsApi)))
forall (m :: * -> *) api.
HasClient m api =>
Proxy api -> Proxy m -> Client m api
clientIn (Proxy (GToServant (Rep (routes AsApi)))
forall k (t :: k). Proxy t
Proxy :: Proxy (ToServantApi routes)) (Proxy m
forall k (t :: k). Proxy t
Proxy :: Proxy m)

-- | 'genericClient' but with 'hoistClientMonad' in between.
genericClientHoist
    :: forall routes m n.
       ( HasClient m (ToServantApi routes)
       , GenericServant routes (AsClientT n)
       , Client n (ToServantApi routes) ~ ToServant routes (AsClientT n)
       )
    => (forall x. m x -> n x)  -- ^ natural transformation
    -> routes (AsClientT n)
genericClientHoist :: (forall x. m x -> n x) -> routes (AsClientT n)
genericClientHoist forall x. m x -> n x
nt
    = ToServant routes (AsClientT n) -> routes (AsClientT n)
forall (routes :: * -> *) mode.
GenericServant routes mode =>
ToServant routes mode -> routes mode
fromServant
    (ToServant routes (AsClientT n) -> routes (AsClientT n))
-> ToServant routes (AsClientT n) -> routes (AsClientT n)
forall a b. (a -> b) -> a -> b
$ Proxy m
-> Proxy (GToServant (Rep (routes AsApi)))
-> (forall x. m x -> n x)
-> Client m (GToServant (Rep (routes AsApi)))
-> Client n (GToServant (Rep (routes AsApi)))
forall (m :: * -> *) api (mon :: * -> *) (mon' :: * -> *).
HasClient m api =>
Proxy m
-> Proxy api
-> (forall x. mon x -> mon' x)
-> Client mon api
-> Client mon' api
hoistClientMonad Proxy m
m Proxy (GToServant (Rep (routes AsApi)))
api forall x. m x -> n x
nt
    (Client m (GToServant (Rep (routes AsApi)))
 -> Client n (GToServant (Rep (routes AsApi))))
-> Client m (GToServant (Rep (routes AsApi)))
-> Client n (GToServant (Rep (routes AsApi)))
forall a b. (a -> b) -> a -> b
$ Proxy (GToServant (Rep (routes AsApi)))
-> Proxy m -> Client m (GToServant (Rep (routes AsApi)))
forall (m :: * -> *) api.
HasClient m api =>
Proxy api -> Proxy m -> Client m api
clientIn Proxy (GToServant (Rep (routes AsApi)))
api Proxy m
m
  where
    m :: Proxy m
m = Proxy m
forall k (t :: k). Proxy t
Proxy :: Proxy m
    api :: Proxy (GToServant (Rep (routes AsApi)))
api = Proxy (GToServant (Rep (routes AsApi)))
forall k (t :: k). Proxy t
Proxy :: Proxy (ToServantApi routes)