Skip to content

Commit f754702

Browse files
committed
feat: added HttpClientService that allows sending http requests
1 parent 6e6d931 commit f754702

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4+
* See LICENSE for details.
5+
*/
6+
7+
package com.almasb.fxgl.net.http;
8+
9+
import com.almasb.fxgl.core.EngineService;
10+
import com.almasb.fxgl.core.concurrent.IOTask;
11+
import com.almasb.fxgl.core.util.LazyValue;
12+
13+
import java.net.URI;
14+
import java.net.http.HttpClient;
15+
import java.net.http.HttpRequest;
16+
import java.net.http.HttpResponse;
17+
import java.util.Collections;
18+
import java.util.Map;
19+
20+
/**
21+
* Service that allows sending HTTP requests.
22+
*
23+
* @author Almas Baimagambetov (almaslvl@gmail.com)
24+
*/
25+
public final class HttpClientService extends EngineService {
26+
27+
// once built it is immutable, so safe to share
28+
private LazyValue<HttpClient> client = new LazyValue<>(HttpClient::newHttpClient);
29+
30+
/**
31+
* @return task that sends a GET request
32+
*/
33+
public IOTask<HttpResponse<String>> sendGETRequestTask(String url) {
34+
return sendGETRequestTask(url, Collections.emptyMap());
35+
}
36+
37+
/**
38+
* @return task that sends a GET request with given [headers]
39+
*/
40+
public IOTask<HttpResponse<String>> sendGETRequestTask(String url, Map<String, String> headers) {
41+
var builder = HttpRequest.newBuilder(URI.create(url))
42+
.GET();
43+
44+
headers.forEach(builder::header);
45+
46+
return sendRequestTask(builder.build());
47+
}
48+
49+
/**
50+
* @return task that sends a GET request with given [headers] and given response body [handler]
51+
*/
52+
public <T> IOTask<HttpResponse<T>> sendGETRequestTask(String url, Map<String, String> headers, HttpResponse.BodyHandler<T> handler) {
53+
var builder = HttpRequest.newBuilder(URI.create(url))
54+
.GET();
55+
56+
headers.forEach(builder::header);
57+
58+
return sendRequestTask(builder.build(), handler);
59+
}
60+
61+
/**
62+
* @return task that sends a PUT request with given [body]
63+
*/
64+
public IOTask<HttpResponse<String>> sendPUTRequestTask(String url, String body) {
65+
return sendPUTRequestTask(url, body, Collections.emptyMap());
66+
}
67+
68+
/**
69+
* @return task that sends a PUT request with given [body] and [headers]
70+
*/
71+
public IOTask<HttpResponse<String>> sendPUTRequestTask(String url, String body, Map<String, String> headers) {
72+
var builder = HttpRequest.newBuilder(URI.create(url))
73+
.PUT(HttpRequest.BodyPublishers.ofString(body));
74+
75+
headers.forEach(builder::header);
76+
77+
return sendRequestTask(builder.build());
78+
}
79+
80+
/**
81+
* @return task that sends a POST request with given [body]
82+
*/
83+
public IOTask<HttpResponse<String>> sendPOSTRequestTask(String url, String body) {
84+
return sendPOSTRequestTask(url, body, Collections.emptyMap());
85+
}
86+
87+
/**
88+
* @return task that sends a POST request with given [body] and [headers]
89+
*/
90+
public IOTask<HttpResponse<String>> sendPOSTRequestTask(String url, String body, Map<String, String> headers) {
91+
var builder = HttpRequest.newBuilder(URI.create(url))
92+
.POST(HttpRequest.BodyPublishers.ofString(body));
93+
94+
headers.forEach(builder::header);
95+
96+
return sendRequestTask(builder.build());
97+
}
98+
99+
/**
100+
* @return task that sends a DELETE request
101+
*/
102+
public IOTask<HttpResponse<String>> sendDELETERequestTask(String url) {
103+
return sendDELETERequestTask(url, Collections.emptyMap());
104+
}
105+
106+
/**
107+
* @return task that sends a DELETE request with given [headers]
108+
*/
109+
public IOTask<HttpResponse<String>> sendDELETERequestTask(String url, Map<String, String> headers) {
110+
var builder = HttpRequest.newBuilder(URI.create(url))
111+
.DELETE();
112+
113+
headers.forEach(builder::header);
114+
115+
return sendRequestTask(builder.build());
116+
}
117+
118+
/**
119+
* Constructs a HTTP IO task.
120+
* The response body will be received as a String.
121+
*
122+
* @return task that sends given [request]
123+
*/
124+
public IOTask<HttpResponse<String>> sendRequestTask(HttpRequest request) {
125+
return sendRequestTask(request, HttpResponse.BodyHandlers.ofString());
126+
}
127+
128+
/**
129+
* Constructs a HTTP IO task.
130+
* The response body will be received as a type T.
131+
*
132+
* @return task that sends given [request]
133+
*/
134+
public <T> IOTask<HttpResponse<T>> sendRequestTask(HttpRequest request, HttpResponse.BodyHandler<T> handler) {
135+
return IOTask.of("sendRequestTask",
136+
() -> client.get().send(request, handler)
137+
);
138+
}
139+
}

fxgl-io/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
module com.almasb.fxgl.io {
1111
requires com.almasb.fxgl.core;
1212
requires com.gluonhq.attach.storage;
13+
requires java.net.http;
1314

1415
exports com.almasb.fxgl.io;
1516
exports com.almasb.fxgl.net;

0 commit comments

Comments
 (0)