2016-06-12 49 views
1

我想創建客戶端綁定到Web API使用servant庫。 我希望能夠發送任何JSON對象。僕人客戶端與類型變量與FromJSON約束

import   Control.Monad.Trans.Except (ExceptT, runExceptT) 
import   Data.Proxy 
import   Network.HTTP.Client (Manager) 
import   Servant.API 
import   Servant.Client 
import   Data.Aeson 

-- | This methods accepts any instance of 'ToJSON' 
-- I would like to have only this method exported from the module 
send :: ToJSON a => a -> Manager -> IO (Either ServantError Result) 
send x manager = runExceptT $ send_ x manager baseUrl 

type MyAPI a = "acceptAnyJson" 
    :> ReqBody '[JSON] a 
    :> Post '[JSON] Result 

api :: ToJSON a => Proxy (MyAPI a) 
api = Proxy 

send_ :: ToJSON a => a -> Manager -> BaseUrl -> ExceptT ServantError IO Result 
send_ = client api 

現在,當我嘗試編譯它,我有錯誤消息:

Couldn't match type ‘a0’ with ‘a’ 
     because type variable ‘a’ would escape its scope 
    This (rigid, skolem) type variable is bound by 
     the inferred type for ‘send_’: 
     ... 

我如何參數我MyAPIclientProxy接受類型的變量?

回答

1

你需要配合的api類型的你發送的東西類型:

send_ = client (Proxy :: Proxy (MyAPI a)) 

{-# LANGUAGE ScopedTypeVariables #-} 
send_ :: forall a. (FromJSON a) => a -> Manager -> BaseUrl -> ExceptT ServantError IO Result 
send_ = client (api :: Proxy (MyAPI a)) 

爲什麼在這一點上與api甚至不屑