feat: HttpUtils共享类,注入为http变量,PAR和mediabo两边统一API
This commit is contained in:
@@ -71,6 +71,7 @@ public class GroovyScriptEngine {
|
|||||||
binding.setVariable("jsoup", new JsoupHelper());
|
binding.setVariable("jsoup", new JsoupHelper());
|
||||||
binding.setVariable("json", objectMapper);
|
binding.setVariable("json", objectMapper);
|
||||||
binding.setVariable("utils", new ScriptUtils());
|
binding.setVariable("utils", new ScriptUtils());
|
||||||
|
binding.setVariable("http", new HttpUtils());
|
||||||
|
|
||||||
GroovyShell shell = new GroovyShell(classLoader, binding);
|
GroovyShell shell = new GroovyShell(classLoader, binding);
|
||||||
String fullScript = SCRIPT_PREFIX + groovyScript + "\n// PAR 调用入口\nreturn parse(content, url)";
|
String fullScript = SCRIPT_PREFIX + groovyScript + "\n// PAR 调用入口\nreturn parse(content, url)";
|
||||||
|
|||||||
55
par-core/src/main/java/com/par/core/adapter/HttpUtils.java
Normal file
55
par-core/src/main/java/com/par/core/adapter/HttpUtils.java
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package com.par.core.adapter;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP 工具类,注入到 Groovy 脚本中(变量名 http)。
|
||||||
|
* 提供 get/post 两个方法,PAR 和 mediabot 各注入自己的实现。
|
||||||
|
* 脚本中使用:http.get(url, cookie) / http.post(url, body, auth)
|
||||||
|
*/
|
||||||
|
public class HttpUtils {
|
||||||
|
|
||||||
|
private static final HttpClient client = HttpClient.newBuilder()
|
||||||
|
.connectTimeout(Duration.ofSeconds(10))
|
||||||
|
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
/** GET 请求 */
|
||||||
|
public String get(String url, String cookie) {
|
||||||
|
try {
|
||||||
|
var builder = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(url))
|
||||||
|
.timeout(Duration.ofSeconds(15))
|
||||||
|
.header("User-Agent", "PAR/1.0")
|
||||||
|
.GET();
|
||||||
|
if (cookie != null && !cookie.isBlank()) builder.header("Cookie", cookie);
|
||||||
|
return client.send(builder.build(), HttpResponse.BodyHandlers.ofString()).body();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("HTTP GET failed: " + url, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** POST JSON 请求 */
|
||||||
|
public String post(String url, String body, String auth) {
|
||||||
|
try {
|
||||||
|
var builder = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(url))
|
||||||
|
.timeout(Duration.ofSeconds(15))
|
||||||
|
.header("User-Agent", "PAR/1.0")
|
||||||
|
.header("Content-Type", "application/json");
|
||||||
|
if (auth != null && !auth.isBlank()) {
|
||||||
|
if (auth.startsWith("Bearer") || auth.toLowerCase().startsWith("x-api-key"))
|
||||||
|
builder.header("Authorization", auth);
|
||||||
|
else builder.header("Cookie", auth);
|
||||||
|
}
|
||||||
|
builder.POST(HttpRequest.BodyPublishers.ofString(body != null ? body : ""));
|
||||||
|
return client.send(builder.build(), HttpResponse.BodyHandlers.ofString()).body();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("HTTP POST failed: " + url, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user