如何重构在Groovy中作为方法调用参数的闭包的重复? [英] How to refactor duplication out of closures that are an argument to a method call in Groovy?

查看:116
本文介绍了如何重构在Groovy中作为方法调用参数的闭包的重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个真实世界的例子,它是通过Rest Client Builder(Grails插件)编写的代码,用于对同一个REST服务端点进行GET和POST调用。我不喜欢重复的头文件和内容类型设置是相同的,但我不知道如何重构常见的部分,因为他们正在调用传递给get()或post()方法调用的闭包方法。请提供一个具体的例子,说明你的答案中重复的重构。

  private def doGetCall(String endpoint,def config ){
def response = new RestBuilder()。get(config.baseURI + endpoint){
contentType(application / json)
header(Authorization,ApiKey+ config .base64EncodedApiKey)
header(ClientId,config.clientId)
}

handleResponse(response,config,endpoint);
返回响应;


private def doPostCall(String endpoint,def payload,def config){
def response = new RestBuilder()。post(config.baseURI + endpoint){
contentType(application / json)
header(Authorization,ApiKey+ config.base64EncodedApiKey)
header(ClientId,config.clientId)
json(payload)
}

handleResponse(响应,配置,端点,有效载荷)
返回响应; Groovy 1.8新增了Closure组合,所以如果您使用的是使用Groovy 1.8或更高版本的Grails版本:

  private def doGetCall(String endpoint,def config ){
def response = new RestBuilder()。get(config.baseURI + endpoint,composeRequest(config))

handleResponse(response,config,endpoint);
返回响应;


private def doPostCall(String endpoint,def payload,def config){
def response = new RestBuilder()。post(config.baseURI + endpoint,composeRequest(config ,{json(payload)}))

handleResponse(response,config,endpoint,payload)
return response;

$ b $ private private composeRequest(def config,Closure clos = null){
def request = {
contentType(application / json)
header(Authorization,ApiKey+ config.base64EncodedApiKey)
header(ClientId,config.clientId)
}
if(clos!= null){
request =请求<<结束
}
请求
}


Consider this real world example, which is code written to make 'GET' and 'POST' calls to the same REST service endpoint via the Rest Client Builder (Grails plugin). I dislike the duplication in that the header and content type setup is identical, but am not sure how to refactor the common pieces out given that they are calling methods on the closure that is being passed to the get() or post() method call. Please provide a concrete example of a good refactoring out of the duplication in your answer.

   private def doGetCall(String endpoint, def config) {
       def response = new RestBuilder().get(config.baseURI+endpoint) {
           contentType("application/json")
           header("Authorization", "ApiKey " + config.base64EncodedApiKey)
           header("ClientId", config.clientId)
       }

       handleResponse(response, config, endpoint);
       return response;
   }

   private def doPostCall(String endpoint, def payload, def config) {
       def response = new RestBuilder().post(config.baseURI+endpoint) {
           contentType("application/json")
           header("Authorization", "ApiKey " + config.base64EncodedApiKey)
           header("ClientId", config.clientId)
           json(payload)
       }

       handleResponse(response, config, endpoint, payload)
       return response;
   }

解决方案

Groovy 1.8 added Closure composition, so if you're using a version of Grails which uses Groovy 1.8 or greater:

private def doGetCall(String endpoint, def config) {
    def response = new RestBuilder().get(config.baseURI+endpoint, composeRequest(config))

    handleResponse(response, config, endpoint);
    return response;
}

private def doPostCall(String endpoint, def payload, def config) {
    def response = new RestBuilder().post(config.baseURI+endpoint, composeRequest(config, { json(payload) }))

    handleResponse(response, config, endpoint, payload)
    return response;
}

private def composeRequest(def config, Closure clos = null) {
    def request = {
        contentType("application/json")
        header("Authorization", "ApiKey " + config.base64EncodedApiKey)
        header("ClientId", config.clientId)
    }
    if (clos != null) {
        request = request << clos
    }
    request
}

这篇关于如何重构在Groovy中作为方法调用参数的闭包的重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆