减少Haskell Servant中的重复

时间:2018-07-08 15:58:55

标签: haskell servant

我正在从事的项目涉及调用CloudFlare API。我已经使用Servant(客户端)定义了API,并且能够使用它来创建客户端。但是,CloudFlare API需要身份验证标头,因此我所有的API类型都以重复结尾。有什么方法可以消除类型级别的那些?我对客户端派生的函数需要这些参数感到满意。

示例代码:

type ListZones = "zones"
  :> Header "X-Auth-Email" Text
  :> Header "X-Auth-Key" Text
  :> Get '[JSON] (Result [Zone])

type ListRecords = "zones"
  :> Header "X-Auth-Email" Text
  :> Header "X-Auth-Key" Text
  :> Capture "zone_uuid" Text
  :> "dns_records"
  :> Get '[JSON] (Result [Record])

type CreateRecord = "zones"
  :> Header "X-Auth-Email" Text
  :> Header "X-Auth-Key" Text
  :> Capture "zone_uuid" Text
  :> "dns_records"
  :> ReqBody '[JSON] Record
  :> Post '[JSON] (Result Record)

type UpdateRecord = "zones"
  :> Header "X-Auth-Email" Text
  :> Header "X-Auth-Key" Text
  :> Capture "zone_uuid" Text
  :> "dns_records"
  :> Capture "record_uuid" Text
  :> ReqBody '[JSON] Record
  :> Patch '[JSON] (Result Record)

1 个答案:

答案 0 :(得分:1)

我相信您会希望以与此处概述的方式类似的方式提取公用位:http://www.parsonsmatt.org/2018/03/14/servant_route_smooshing.html

他来自:

type Api
    = "player" 
        :> Capture "playerId" Int 
        :> "x" 
        :> Get '[JSON] Int
    :<|> "player" 
        :> Capture "playerId" Int 
        :> "y" 
        :> Get '[JSON] Int

type Api'
    = "player" 
    :> Capture "playerId" Int
    :> (     "y" :> Get '[JSON] Int
        :<|> "x" :> Get '[JSON] Int
       )

这比您需要做的要简单,但是显示了如何入门。

希望有帮助。