Skip to content

Commit 3782506

Browse files
committed
add a way to get deserialized objects from the Event
1 parent 4a9c1f4 commit 3782506

File tree

7 files changed

+92
-9
lines changed

7 files changed

+92
-9
lines changed

unirest-bdd-tests/src/test/java/BehaviorTests/SSETest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ void headers(){
118118
.assertHeader("number", "1")
119119
.assertHeader("fruit", "apple")
120120
.assertCookie("snack", "snickerdoodle");
121-
122121
}
123122

124123
@Test
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* The MIT License
3+
*
4+
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package kong.unirest.core;
27+
28+
import BehaviorTests.Foo;
29+
import BehaviorTests.JacksonObjectMapper;
30+
import kong.unirest.core.java.Event;
31+
import org.junit.jupiter.api.Test;
32+
33+
import java.util.List;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
public class EventSerializationTest {
38+
@Test
39+
void canDeserialize() {
40+
var config = new Config();
41+
var om = new JacksonObjectMapper();
42+
config.setObjectMapper(om);
43+
44+
var foo = new Foo("qux");
45+
var event = new Event("", "message", om.writeValue(foo), config);
46+
47+
assertThat(event.asObject(Foo.class))
48+
.isEqualTo(foo);
49+
}
50+
51+
@Test
52+
void canDeserializeWithGenericTypes(){
53+
var config = new Config();
54+
var om = new JacksonObjectMapper();
55+
config.setObjectMapper(om);
56+
57+
var foo = List.of(new Foo("qux"));
58+
var event = new Event("", "message", om.writeValue(foo), config);
59+
60+
assertThat(event.asObject(new GenericType<List<Foo>>() {}))
61+
.isEqualTo(foo);
62+
}
63+
}

unirest/src/main/java/kong/unirest/core/SseListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
import kong.unirest.core.java.Event;
2929

30+
@FunctionalInterface
3031
public interface SseListener {
3132
void onEvent(Event event);
32-
void onComment(String line);
33+
default void onComment(String line) {}
3334
}

unirest/src/main/java/kong/unirest/core/java/Event.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,29 @@
2525

2626
package kong.unirest.core.java;
2727

28+
import kong.unirest.core.Config;
29+
import kong.unirest.core.GenericType;
30+
2831
import java.util.Objects;
2932

3033
public class Event {
34+
private final Config config;
3135
private final String id;
32-
private final Object data;
36+
private final String data;
3337
private final String field;
3438

35-
public Event(String id, String field, Object data){
39+
public Event(String id, String field, String data, Config config) {
40+
this.config = config;
3641
this.id = id;
3742
this.field = field;
3843
this.data = data;
3944
}
4045

41-
public Object data() {
46+
public Event(String id, String field, String data) {
47+
this(id, field, data, null);
48+
}
49+
50+
public String data() {
4251
return data;
4352
}
4453

@@ -72,4 +81,12 @@ public String toString() {
7281
", id=" + id +
7382
'}';
7483
}
84+
85+
public <T> T asObject(Class<? extends T> responseClass) {
86+
return config.getObjectMapper().readValue(data, responseClass);
87+
}
88+
89+
public <T> T asObject(GenericType<T> genericType){
90+
return config.getObjectMapper().readValue(data, genericType);
91+
}
7592
}

unirest/src/main/java/kong/unirest/core/java/JavaClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public CompletableFuture<Void> sse(SseRequest request, SseListener listener) {
165165
var r = SseRequestBuilder.getHttpRequest(request);
166166

167167
return client.sendAsync(r, java.net.http.HttpResponse.BodyHandlers.ofLines())
168-
.thenAccept(new SseResponseHandler(listener))
168+
.thenAccept(new SseResponseHandler(config, listener))
169169
.exceptionally(ex -> {
170170
System.err.println("Error: " + ex.getMessage());
171171
return null;

unirest/src/main/java/kong/unirest/core/java/SseResponseHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@
2525

2626
package kong.unirest.core.java;
2727

28+
import kong.unirest.core.Config;
2829
import kong.unirest.core.SseListener;
2930

3031
import java.net.http.HttpResponse;
3132
import java.util.function.Consumer;
3233
import java.util.stream.Stream;
3334

3435
class SseResponseHandler implements Consumer<HttpResponse<Stream<String>>> {
36+
private final Config config;
3537
private final SseListener listener;
3638
private EventBuffer databuffer = new EventBuffer();
3739

38-
public SseResponseHandler(SseListener listener) {
40+
public SseResponseHandler(Config config, SseListener listener) {
41+
this.config = config;
3942
this.listener = listener;
4043
}
4144

@@ -144,7 +147,7 @@ private class EventBuffer {
144147
StringBuffer buffer = new StringBuffer();
145148

146149
public Event toEvent() {
147-
return new Event(id, type, getValue());
150+
return new Event(id, type, getValue(), config);
148151
}
149152

150153
private String getValue() {

unirest/src/test/java/kong/unirest/core/java/SseResponseHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SseResponseHandlerTest {
4646
@BeforeEach
4747
void setUp() {
4848
listener = new TestListener();
49-
handler = new SseResponseHandler(listener);
49+
handler = new SseResponseHandler(null, listener);
5050
}
5151

5252
@Test

0 commit comments

Comments
 (0)