gpt4 book ai didi

java - 当唯一部分在循环/try-catch 中时避免重复代码

转载 作者:搜寻专家 更新时间:2023-11-01 02:21:48 25 4
gpt4 key购买 nike

我有一个类有两个方法,它们有很多重复代码,但唯一的一点是在整个事情的中间。根据我的研究,我认为我应该执行“围绕方法执行”模式,但我找不到我可以遵循的资源,因为它们似乎都使用我无法复制的代码。

我有两个方法,apiPost 和 apiGet,我已将它们粘贴在下面。我用显示独特部分开始和结束位置的注释包装了这些方法的独特部分:

/**
* Class that handles authorising the connection and handles posting and getting data
*
* @version %I%, %G%
* @since 1.0
*/
public class CallHandler {
private static PropertyLoader props = PropertyLoader.getInstance();
final static int MAX = props.getPropertyAsInteger(props.MAX_REQUESTS);
private final Logger log = LoggerFactory.getLogger(CallHandler.class);
private final static String POST = "POST";
private final static String GET = "GET";

/**
* Makes a POST call to the API URL provided and returns the JSON response as a string
* http://stackoverflow.com/questions/15570656/how-to-send-request-payload-to-rest-api-in-java
*
* @param urlString the API URL to send the data to, as a string
* @param payload the serialised JSON payload string
* @return and value returned as a JSON string, ready to be deserialised
*/
public String apiPost(String urlString, String payload) {
boolean keepGoing = true;
int tries = 0;

String line;
StringBuilder jsonString = new StringBuilder();

log.debug("Making API Call: {}", urlString);

while (keepGoing && tries < MAX) {
tries++;
try {
URL url = new URL(urlString);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// UNIQUE CODE START
prepareConnection(connection, POST);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();
// UNIQUE CODE END

BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect();
keepGoing = false;
} catch (Exception e) {
log.warn("Try #{}. Error posting: {}", tries, e.getMessage());
log.warn("Pausing for 1 second then trying again...");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException f) {
log.warn("Sleeping has been interrupted: {}", f.getMessage());
}
}
}
return jsonString.toString();
}

/**
* Makes a GET call to the API URL provided and returns the JSON response as a string
* http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests
*
* @param urlString the API URL to request the data from, as a string
* @return the json response as a string, ready to be deserialised
*/
public String apiGet(String urlString) {
boolean keepGoing = true;
int tries = 0;

String line;
StringBuilder jsonString = new StringBuilder();

log.debug("Making API Call: {}", urlString);

while (keepGoing && tries < MAX) {
tries++;
try {
URL url = new URL(urlString);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// UNIQUE CODE START
prepareConnection(connection, GET);
connection.connect();
// UNIQUE CODE END

BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect();
keepGoing = false;
} catch (Exception e) {
log.warn("Try #{}. Error getting from API: {}", tries, e.getMessage());
log.warn("Pausing for 1 second then trying again...");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException f) {
log.warn("Sleeping has been interrupted: {}", f.getMessage());
}
}
}
return jsonString.toString();
}

/**
* Prepares the HTTP Url connection depending on whether this is a POST or GET call
*
* @param connection the connection to prepare
* @param method whether the call is a POST or GET call
*/
private void prepareConnection(HttpURLConnection connection, String method) {
String charset = "UTF-8";
try {
connection.setRequestMethod(method);
if (method.equals(GET)) {
connection.setRequestProperty("Accept-Charset", charset);
} else if (method.equals(POST)) {
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json; charset=" + charset);
}
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", "Bearer " + apiKey);
} catch (Exception e) {
log.error("Error preparing HTTP URL connection: {}", e.getMessage());
throw new RuntimeException(e.getMessage());
}
}

我可以在这里使用“Execute around method”模式来减少代码重复吗?如果可以,有人可以帮我弄清楚如何重构这段代码以利用它。如果这是错误的解决方法,有人可以提出一个明智的替代方案吗?

最佳答案

这可以通过将“唯一”代码提取到特殊工作程序中来完成。更具体地说,例如,您可以使用 lambda 表达式:

public String apiPost(String urlString, String payload) {
return commonMethod(urlString, payload, (connection) -> {
// UNIQUE CODE START
prepareConnection(connection, POST);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();
// UNIQUE CODE END
});
}

interface ConnectionWorker {
void run(HttpURLConnection connection) throws IOException;
}

public String commonMethod(String urlString, String payload, ConnectionWorker worker) {
boolean keepGoing = true;
int tries = 0;

String line;
StringBuilder jsonString = new StringBuilder();

log.debug("Making API Call: {}", urlString);

while (keepGoing && tries < MAX) {
tries++;
try {
URL url = new URL(urlString);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

worker.run(connection);

BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect();
keepGoing = false;
} catch (Exception e) {
log.warn("Try #{}. Error posting: {}", tries, e.getMessage());
log.warn("Pausing for 1 second then trying again...");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException f) {
log.warn("Sleeping has been interrupted: {}", f.getMessage());
}
}
}
return jsonString.toString();
}

更新:如果您不能使用 java 8 和 lambda,您可以随时切换到创建匿名类:

    return commonMethod(urlString, payload, new ConnectionWorker() {
@Override
public void run(HttpURLConnection connection) throws IOException {
// UNIQUE CODE START
CallHandler.this.prepareConnection(connection, POST);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();
// UNIQUE CODE END
}
});

关于java - 当唯一部分在循环/try-catch 中时避免重复代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38715768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com