feat: code

This commit is contained in:
2025-12-28 23:34:25 +08:00
parent 486f1dc150
commit a63c841076
4 changed files with 199 additions and 167 deletions

View File

@@ -68,6 +68,9 @@ public class ChannelListener {
String jsonPayload = in.readUTF();
ProxyNetworkRequest req = gson.fromJson(jsonPayload, ProxyNetworkRequest.class);
reqId = req.getRequestId();
long startTime = System.currentTimeMillis();
plugin.getLogger().info("[Debug] Received API Request: " + req.getEndpoint() + " (ID: " + reqId + ")");
CompletableFuture<NetworkResponse> future;
@@ -89,34 +92,37 @@ public class ChannelListener {
future = httpService.postData(req.getEndpoint(), bodyJson);
} else {
// replace placeholders
// ... (GET logic)
// ...
java.util.Map<String, String> finalParams = new java.util.HashMap<>();
if (req.getParams() != null) {
for (java.util.Map.Entry<String, String> entry : req.getParams().entrySet()) {
String originalKey = entry.getKey();
String originalValue = entry.getValue();
String replacedValue = resolvePlaceholders(originalValue);
finalParams.put(originalKey, replacedValue);
}
}
plugin.getLogger().info("Proxy GET [ " + req.getEndpoint() + " ]\n\t" + finalParams.toString());
plugin.getLogger().info("Proxy GET [ " + req.getEndpoint() + " ] params: " + finalParams);
future = httpService.getData(req.getEndpoint(), finalParams);
}
// 3. Handle the result and send back the response
String finalReqId = reqId;
future.thenAccept(response -> {
long duration = System.currentTimeMillis() - startTime;
plugin.getLogger().info("[Debug] API Response received for " + finalReqId + " in " + duration + "ms. Status: " + response.getStatusCode());
// Check if HTTP status code is 2xx
boolean success = response.getStatusCode() >= 200 && response.getStatusCode() < 300;
sendResponse(backendServer, finalReqId, success, response.getBody());
}).exceptionally(e -> {
plugin.getLogger().error("API Proxy Request Failed", e);
long duration = System.currentTimeMillis() - startTime;
plugin.getLogger().error("[Debug] API Request Failed for " + finalReqId + " after " + duration + "ms", e);
sendResponse(backendServer, finalReqId, false, "Proxy Error: " + e.getMessage());
return null;
});