1

我正在进行的项目涉及调用 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)
4

1 回答 1

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
       )

这比您需要做的更简单,但显示了如何开始。

希望有帮助。

于 2018-07-08T16:20:30.123 回答