Skip to content

Commit 54c27a6

Browse files
committed
real world examples and documentation
1 parent d684aea commit 54c27a6

File tree

8 files changed

+697
-0
lines changed

8 files changed

+697
-0
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 4.5.0
2+
* Added support for Server Sent Events. See Documentation.
3+
4+
## 4.4.11
5+
* Updated dependencies
6+
* versions 4.4.8 - 4.4.10 were lost to Maven Central authentication changes
7+
8+
## 4.4.7
9+
* issue #542 should pass default response to dynamically created invocations
10+
111
## 4.4.5
212
* Issue #536 UnirestInstance should implement AutoCloseable
313

mkdocs/docs/server-sent-events.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
## About Server Sent Events
3+
Server-Sent Events (SSE) is a server push technology enabling a client to receive automatic updates from a server via an HTTP connection, and describes how servers can initiate data transmission towards clients once an initial client connection has been established. They are commonly used to send message updates or continuous data streams to a browser client and designed to enhance native, cross-browser streaming through a JavaScript API called EventSource, through which a client requests a particular URL in order to receive an event stream. The EventSource API is standardized as part of [HTML Living Standard by the WHATWG](https://html.spec.whatwg.org/multipage/server-sent-events.html).
4+
The media type for SSE is ```text/event-stream```.
5+
6+
## Consuming Server Sent Events With Unirest-Java
7+
Unirest has two ways to consume a SSE web service; one async and one synchronous. Please be mindful that SSE is a persistent connection and unirest will keep the connection open as long as the server is willing and able. For this reason you may find the async method a better fit for most production systems.
8+
9+
### Async Call
10+
The following subscribes to wikipedia's SSE stream of recently changed pages. Maps each event into a POJO, and outputs the name of the changed page.
11+
Note that Unirest will return you a CompletableFuture<Void> which you can hold on to to monitor the process.
12+
```java
13+
var future = Unirest.sse("https://stream.wikimedia.org/v2/stream/recentchange")
14+
.connect(event -> {
15+
var change = event.asObject(RecentChange.class);
16+
System.out.println("Changed Page: " + change.getTitle());
17+
});
18+
19+
```
20+
21+
22+
### Synchronous Call
23+
The following subscribes to wikipedia's SSE stream of recently changed pages. Maps each event into a POJO, and outputs the name of the changed page.
24+
```java
25+
Unirest.sse("https://stream.wikimedia.org/v2/stream/recentchange")
26+
.connect()
27+
.map(e -> e.asObject(RecentChange.class))
28+
.forEach(r -> System.out.println("Changed Page: " + r.getTitle()));
29+
30+
```

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828

2929
import kong.unirest.core.SseRequest;
30+
import kong.unirest.core.Unirest;
3031
import org.junit.jupiter.api.AfterEach;
3132
import org.junit.jupiter.api.BeforeEach;
3233
import org.junit.jupiter.api.Disabled;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 BehaviorTests;
27+
28+
29+
import BehaviorTests.wikipedia.RecentChange;
30+
import kong.unirest.core.Unirest;
31+
import org.junit.jupiter.api.Disabled;
32+
import org.junit.jupiter.api.Test;
33+
34+
@Disabled
35+
public class SseRealExamples {
36+
37+
@Test
38+
void sync() {
39+
Unirest.sse("https://stream.wikimedia.org/v2/stream/recentchange")
40+
.connect()
41+
.map(e -> e.asObject(RecentChange.class))
42+
.forEach(r -> System.out.println("Changed Page: " + r.getTitle()));
43+
}
44+
45+
@Test
46+
void async() {
47+
var future = Unirest.sse("https://stream.wikimedia.org/v2/stream/recentchange")
48+
.connect(event -> {
49+
var change = event.asObject(RecentChange.class);
50+
System.out.println("Changed Page: " + change.getTitle());
51+
});
52+
while (!future.isDone()){}
53+
}
54+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
27+
package BehaviorTests.wikipedia;
28+
29+
import com.fasterxml.jackson.annotation.JsonInclude;
30+
import com.fasterxml.jackson.annotation.JsonProperty;
31+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
32+
33+
@JsonInclude(JsonInclude.Include.NON_NULL)
34+
@JsonPropertyOrder({
35+
"old",
36+
"new"
37+
})
38+
public class Length {
39+
40+
@JsonProperty("old")
41+
private Long old;
42+
@JsonProperty("new")
43+
private Long _new;
44+
45+
@JsonProperty("old")
46+
public Long getOld() {
47+
return old;
48+
}
49+
50+
@JsonProperty("old")
51+
public void setOld(Long old) {
52+
this.old = old;
53+
}
54+
55+
@JsonProperty("new")
56+
public Long getNew() {
57+
return _new;
58+
}
59+
60+
@JsonProperty("new")
61+
public void setNew(Long _new) {
62+
this._new = _new;
63+
}
64+
65+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
27+
package BehaviorTests.wikipedia;
28+
29+
30+
import com.fasterxml.jackson.annotation.JsonInclude;
31+
import com.fasterxml.jackson.annotation.JsonProperty;
32+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
33+
34+
@JsonInclude(JsonInclude.Include.NON_NULL)
35+
@JsonPropertyOrder({
36+
"uri",
37+
"request_id",
38+
"id",
39+
"dt",
40+
"domain",
41+
"stream",
42+
"topic",
43+
"partition",
44+
"offset"
45+
})
46+
public class Meta {
47+
48+
@JsonProperty("uri")
49+
private String uri;
50+
@JsonProperty("request_id")
51+
private String requestId;
52+
@JsonProperty("id")
53+
private String id;
54+
@JsonProperty("dt")
55+
private String dt;
56+
@JsonProperty("domain")
57+
private String domain;
58+
@JsonProperty("stream")
59+
private String stream;
60+
@JsonProperty("topic")
61+
private String topic;
62+
@JsonProperty("partition")
63+
private Long partition;
64+
@JsonProperty("offset")
65+
private Long offset;
66+
67+
@JsonProperty("uri")
68+
public String getUri() {
69+
return uri;
70+
}
71+
72+
@JsonProperty("uri")
73+
public void setUri(String uri) {
74+
this.uri = uri;
75+
}
76+
77+
@JsonProperty("request_id")
78+
public String getRequestId() {
79+
return requestId;
80+
}
81+
82+
@JsonProperty("request_id")
83+
public void setRequestId(String requestId) {
84+
this.requestId = requestId;
85+
}
86+
87+
@JsonProperty("id")
88+
public String getId() {
89+
return id;
90+
}
91+
92+
@JsonProperty("id")
93+
public void setId(String id) {
94+
this.id = id;
95+
}
96+
97+
@JsonProperty("dt")
98+
public String getDt() {
99+
return dt;
100+
}
101+
102+
@JsonProperty("dt")
103+
public void setDt(String dt) {
104+
this.dt = dt;
105+
}
106+
107+
@JsonProperty("domain")
108+
public String getDomain() {
109+
return domain;
110+
}
111+
112+
@JsonProperty("domain")
113+
public void setDomain(String domain) {
114+
this.domain = domain;
115+
}
116+
117+
@JsonProperty("stream")
118+
public String getStream() {
119+
return stream;
120+
}
121+
122+
@JsonProperty("stream")
123+
public void setStream(String stream) {
124+
this.stream = stream;
125+
}
126+
127+
@JsonProperty("topic")
128+
public String getTopic() {
129+
return topic;
130+
}
131+
132+
@JsonProperty("topic")
133+
public void setTopic(String topic) {
134+
this.topic = topic;
135+
}
136+
137+
@JsonProperty("partition")
138+
public Long getPartition() {
139+
return partition;
140+
}
141+
142+
@JsonProperty("partition")
143+
public void setPartition(Long partition) {
144+
this.partition = partition;
145+
}
146+
147+
@JsonProperty("offset")
148+
public Long getOffset() {
149+
return offset;
150+
}
151+
152+
@JsonProperty("offset")
153+
public void setOffset(Long offset) {
154+
this.offset = offset;
155+
}
156+
157+
}

0 commit comments

Comments
 (0)