Skip to content

Commit 56cd169

Browse files
committed
Mark execute as Nullable and add convenience method for safe calls.
1 parent 5b35435 commit 56cd169

5 files changed

Lines changed: 23 additions & 8 deletions

File tree

library/src/main/java/me/proxer/library/api/ProxerCall.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public final class ProxerCall<T> implements Cloneable {
3333
* Upon success, the result entity is returned. If an error occurs, the respective {@link ProxerException} is
3434
* thrown.
3535
*/
36+
@Nullable
3637
public T execute() throws ProxerException {
3738
try {
3839
return processResponse(internalCall.execute());
@@ -43,6 +44,16 @@ public T execute() throws ProxerException {
4344
}
4445
}
4546

47+
public T safeExecute() throws ProxerException {
48+
final T result = execute();
49+
50+
if (result == null) {
51+
throw processNonProxerError(new NullPointerException("Response is null."));
52+
} else {
53+
return result;
54+
}
55+
}
56+
4657
/**
4758
* Executes the request asynchronous.
4859
* <p>
@@ -112,6 +123,7 @@ public Request request() {
112123
return internalCall.request();
113124
}
114125

126+
@Nullable
115127
private T processResponse(final Response<ProxerResponse<T>> response) throws ProxerException {
116128
if (response.isSuccessful()) {
117129
final ProxerResponse<T> proxerResponse = response.body();

library/src/main/java/me/proxer/library/api/ProxerCallback.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.proxer.library.api;
22

3+
import javax.annotation.Nullable;
4+
35
/**
46
* A callback for a successful request.
57
*
@@ -11,5 +13,5 @@ public interface ProxerCallback<T> {
1113
/**
1214
* Called upon success for each Request with the {@code result}.
1315
*/
14-
void onSuccess(T result);
16+
void onSuccess(@Nullable T result);
1517
}

library/src/main/java/me/proxer/library/api/ProxerResponse.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.squareup.moshi.Json;
44
import lombok.*;
55

6+
import javax.annotation.Nullable;
7+
68
/**
79
* @author Ruben Gees
810
*/
@@ -27,6 +29,7 @@ final class ProxerResponse<T> {
2729
@Json(name = "code")
2830
private int code;
2931

32+
@Nullable
3033
@SuppressWarnings("unused")
3134
@Getter(AccessLevel.PACKAGE)
3235
@Json(name = "data")

library/src/test/java/me/proxer/library/api/messenger/SendMessageEndpointTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,9 @@ public void testActionParameters() throws ProxerException, IOException, Interrup
6666

6767
@Test
6868
public void testInvalidAction() throws ProxerException, IOException, InterruptedException {
69-
//noinspection CodeBlock2Expr
70-
assertThatThrownBy(() -> {
71-
api.messenger().sendMessage("id", MessageAction.NONE, "")
72-
.build()
73-
.execute();
74-
}).isInstanceOf(IllegalArgumentException.class);
69+
assertThatThrownBy(() -> api.messenger().sendMessage("id", MessageAction.NONE, "")
70+
.build()
71+
.execute())
72+
.isInstanceOf(IllegalArgumentException.class);
7573
}
7674
}

sample/src/main/java/me/proxer/sample/News.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static void main(String[] args) {
1717
try {
1818
final String news = api.notifications().news()
1919
.build()
20-
.execute()
20+
.safeExecute()
2121
.stream()
2222
.map(newsArticle -> newsArticle.getSubject() + " written by " + newsArticle.getAuthor())
2323
.collect(Collectors.joining("\n"));

0 commit comments

Comments
 (0)