forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequests.java
More file actions
105 lines (84 loc) · 3.83 KB
/
HttpRequests.java
File metadata and controls
105 lines (84 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package sqlancer.feldera.client;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpTimeoutException;
import java.time.Duration;
import java.util.Map;
public class HttpRequests {
private final String baseUrl;
private final ObjectMapper objectMapper;
private final HttpClient httpClient;
public HttpRequests(String baseUrl) {
this.baseUrl = baseUrl;
this.objectMapper = new ObjectMapper();
this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(2)).build();
}
private HttpRequest.Builder createRequestBuilder(String path) {
return HttpRequest.newBuilder().timeout(Duration.ofSeconds(2)).uri(URI.create(baseUrl + path))
.header("Content-Type", "application/json").header("User-Agent", "feldera-java-client/v1");
}
private <T> HttpRequest.BodyPublisher serializeBody(T body) throws JsonProcessingException {
if (body == null) {
return HttpRequest.BodyPublishers.noBody();
} else if (body instanceof String) {
return HttpRequest.BodyPublishers.ofString((String) body);
} else {
return HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(body));
}
}
private void validateResponse(HttpResponse<String> response) throws Exception {
int statusCode = response.statusCode();
if (statusCode >= 200 && statusCode < 300) {
return; // Successful response
}
String contentType = response.headers().firstValue("content-type").orElse("");
String errorMessage;
if (contentType.equals("application/json")) {
errorMessage = objectMapper.readTree(response.body()).toPrettyString();
} else {
errorMessage = response.body();
}
throw new Exception("HTTP Error: " + statusCode + " " + errorMessage);
}
private String sendRequest(HttpRequest request) throws Exception {
try {
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
validateResponse(response);
return response.body();
} catch (HttpTimeoutException e) {
throw new Exception("Request timed out: " + e.getMessage(), e);
} catch (Exception e) {
String path = request.uri().getPath();
throw new Exception("Failed to send request: " + path + "\nErr: " + e.getMessage(), e);
}
}
public String get(String path) throws Exception {
HttpRequest request = createRequestBuilder(path).GET().build();
return sendRequest(request);
}
public String get(String path, Map<String, String> queryParams) throws Exception {
if (queryParams == null) {
return get(path);
}
String q = "?" + queryParams.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue())
.reduce((a, b) -> a + "&" + b).orElse("");
HttpRequest request = createRequestBuilder(path + q).GET().build();
return sendRequest(request);
}
public <T> String post(String path, T body) throws Exception {
HttpRequest request = createRequestBuilder(path).POST(serializeBody(body)).build();
return sendRequest(request);
}
public <T> String patch(String path, T body) throws Exception {
HttpRequest request = createRequestBuilder(path).method("PATCH", serializeBody(body)).build();
return sendRequest(request);
}
public <T> String put(String path, T body) throws Exception {
HttpRequest request = createRequestBuilder(path).PUT(serializeBody(body)).build();
return sendRequest(request);
}
}