Skip to content

Commit 0d8f66e

Browse files
committed
Add single interface overloads for AsyncServiceClient to provide a better call-site story for Kotlin and Java 8
1 parent 53253cf commit 0d8f66e

File tree

15 files changed

+155
-9
lines changed

15 files changed

+155
-9
lines changed

src/AndroidClient/android/src/main/java/net/servicestack/android/AndroidServiceClient.java

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55

66
import android.os.AsyncTask;
77

8+
import net.servicestack.client.AsyncComplete;
9+
import net.servicestack.client.AsyncError;
810
import net.servicestack.client.AsyncResult;
911
import net.servicestack.client.AsyncResultVoid;
1012
import net.servicestack.client.AsyncServiceClient;
13+
import net.servicestack.client.AsyncSuccessVoid;
1114
import net.servicestack.client.IReturn;
1215
import net.servicestack.client.IReturnVoid;
1316
import net.servicestack.client.JsonServiceClient;
17+
import net.servicestack.client.AsyncSuccess;
1418
import net.servicestack.client.Utils;
1519

1620
import java.lang.reflect.Type;
@@ -28,6 +32,36 @@ public <T,TResponse> void execTask(AsyncTask<T,Void,TResponse> asyncTask, T... r
2832
asyncTask.execute(request);
2933
}
3034

35+
<T> AsyncResult<T> createAsyncResult(final AsyncSuccess<T> success, final AsyncError error){
36+
return new AsyncResult<T>() {
37+
@Override
38+
public void success(T response) {
39+
success.success(response);
40+
}
41+
42+
@Override
43+
public void error(Exception ex) {
44+
if (error != null)
45+
error.error(ex);
46+
}
47+
};
48+
}
49+
50+
AsyncResultVoid createAsyncResultVoid(final AsyncSuccessVoid success, final AsyncError error){
51+
return new AsyncResultVoid() {
52+
@Override
53+
public void success() {
54+
success.success();
55+
}
56+
57+
@Override
58+
public void error(Exception ex) {
59+
if (error != null)
60+
error.error(ex);
61+
}
62+
};
63+
}
64+
3165
/* SEND */
3266

3367
@Override
@@ -77,12 +111,12 @@ protected void onPostExecute(Void noResponse) {
77111
/* GET */
78112
public <T> void getAsync(IReturn<T> request, final AsyncResult<T> asyncResult){
79113
final AndroidServiceClient client = this;
80-
execTask(new AsyncTask<IReturn<T>, Void, T>(){
114+
execTask(new AsyncTask<IReturn<T>, Void, T>() {
81115
@Override
82116
protected T doInBackground(IReturn<T>... params) {
83117
try {
84118
return client.get(params[0]);
85-
} catch (Exception e){
119+
} catch (Exception e) {
86120
asyncResult.setError(e);
87121
return null;
88122
}
@@ -96,6 +130,14 @@ protected void onPostExecute(T response) {
96130
}, request);
97131
}
98132

133+
public <T> void getAsync(IReturn<T> request, final AsyncSuccess<T> successCallback) {
134+
getAsync(request, successCallback, null);
135+
}
136+
137+
public <T> void getAsync(IReturn<T> request, final AsyncSuccess<T> success, final AsyncError error){
138+
getAsync(request, createAsyncResult(success, error));
139+
}
140+
99141
@Override
100142
public void getAsync(IReturnVoid request, final AsyncResultVoid asyncResult) {
101143
final AndroidServiceClient client = this;
@@ -118,6 +160,16 @@ protected void onPostExecute(Void noResponse) {
118160
}, request);
119161
}
120162

163+
@Override
164+
public <T> void getAsync(IReturnVoid request, final AsyncSuccessVoid success) {
165+
getAsync(request, success, null);
166+
}
167+
168+
@Override
169+
public <T> void getAsync(IReturnVoid request, AsyncSuccessVoid success, AsyncError error) {
170+
getAsync(request, createAsyncResultVoid(success, error));
171+
}
172+
121173
public <T> void getAsync(IReturn<T> request, final Map<String, String> queryParams, final AsyncResult<T> asyncResult){
122174
final AndroidServiceClient client = this;
123175
execTask(new AsyncTask<IReturn<T>, Void, T>() {
@@ -139,6 +191,11 @@ protected void onPostExecute(T response) {
139191
}, request);
140192
}
141193

194+
@Override
195+
public <T> void getAsync(IReturn<T> request, Map<String, String> queryParams, AsyncSuccess<T> success) {
196+
getAsync(request, queryParams, createAsyncResult(success, null));
197+
}
198+
142199
public <T> void getAsync(String path, final Class responseType, final AsyncResult<T> asyncResult) {
143200
final AndroidServiceClient client = this;
144201
execTask(new AsyncTask<String, Void, T>() {
@@ -160,6 +217,11 @@ protected void onPostExecute(T response) {
160217
}, path);
161218
}
162219

220+
@Override
221+
public <T> void getAsync(String path, Class responseType, AsyncSuccess<T> success) {
222+
getAsync(path, responseType, createAsyncResult(success, null));
223+
}
224+
163225
@Override
164226
public <T> void getAsync(String path, final Type responseType, final AsyncResult<T> asyncResult) {
165227
final AndroidServiceClient client = this;
@@ -182,6 +244,11 @@ protected void onPostExecute(T response) {
182244
}, path);
183245
}
184246

247+
@Override
248+
public <T> void getAsync(String path, Type responseType, AsyncSuccess<T> success) {
249+
getAsync(path, responseType, createAsyncResult(success, null));
250+
}
251+
185252
public void getAsync(String path, final AsyncResult<byte[]> asyncResult) {
186253
final AndroidServiceClient client = this;
187254
execTask(new AsyncTask<String, Void, byte[]>() {
@@ -203,6 +270,11 @@ protected void onPostExecute(byte[] bytes) {
203270
}, path);
204271
}
205272

273+
@Override
274+
public void getAsync(String path, AsyncSuccess<byte[]> success) {
275+
getAsync(path, createAsyncResult(success, null));
276+
}
277+
206278
/* POST */
207279

208280
@Override
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.servicestack.client;
2+
3+
public interface AsyncComplete {
4+
void complete();
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.servicestack.client;
2+
3+
public interface AsyncError {
4+
void error(Exception ex);
5+
}

src/AndroidClient/android/src/main/java/net/servicestack/client/AsyncResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
package net.servicestack.client;
55

6-
public abstract class AsyncResult<T> {
6+
public abstract class AsyncResult<T> implements AsyncSuccess<T>, AsyncError, AsyncComplete {
77
T result;
88
boolean completed = false;
99
Exception ex;

src/AndroidClient/android/src/main/java/net/servicestack/client/AsyncServiceClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ public interface AsyncServiceClient {
1212
void sendAsync(IReturnVoid request, final AsyncResultVoid asyncResult);
1313

1414
<T> void getAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
15+
<T> void getAsync(IReturn<T> request, final AsyncSuccess<T> success);
16+
<T> void getAsync(IReturn<T> request, final AsyncSuccess<T> success, final AsyncError error);
1517
void getAsync(IReturnVoid request, final AsyncResultVoid asyncResult);
18+
<T> void getAsync(IReturnVoid request, final AsyncSuccessVoid success);
19+
<T> void getAsync(IReturnVoid request, final AsyncSuccessVoid success, final AsyncError error);
1620
<T> void getAsync(IReturn<T> request, final Map<String, String> queryParams, final AsyncResult<T> asyncResult);
21+
<T> void getAsync(IReturn<T> request, final Map<String, String> queryParams, AsyncSuccess<T> success);
1722
<T> void getAsync(String path, final Class responseType, final AsyncResult<T> asyncResult);
23+
<T> void getAsync(String path, final Class responseType, final AsyncSuccess<T> success);
1824
<T> void getAsync(String path, final Type responseType, final AsyncResult<T> asyncResult);
25+
<T> void getAsync(String path, final Type responseType, final AsyncSuccess<T> success);
1926
void getAsync(String path, final AsyncResult<byte[]> asyncResult);
27+
void getAsync(String path, final AsyncSuccess<byte[]> success);
2028

2129
<T> void postAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
2230
void postAsync(IReturnVoid request, final AsyncResultVoid asyncResult);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2015 ServiceStack LLC. All rights reserved.
2+
// License: https://servicestack.net/bsd-license.txt
3+
4+
package net.servicestack.client;
5+
6+
public interface AsyncSuccess<T> {
7+
void success(T response);
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2015 ServiceStack LLC. All rights reserved.
2+
// License: https://servicestack.net/bsd-license.txt
3+
4+
package net.servicestack.client;
5+
6+
public interface AsyncSuccessVoid {
7+
void success();
8+
}

src/AndroidClient/android/src/main/java/net/servicestack/client/Utils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.google.gson.JsonObject;
99
import com.google.gson.JsonParseException;
1010

11+
import net.servicestack.func.Function;
12+
1113
import java.io.BufferedInputStream;
1214
import java.io.BufferedReader;
1315
import java.io.ByteArrayOutputStream;
@@ -30,8 +32,6 @@
3032
import java.util.Map;
3133
import java.util.UUID;
3234

33-
import net.servicestack.func.Function;
34-
3535
import static net.servicestack.func.Func.last;
3636

3737
// Generic Utils
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.servicestack.client;
2+
3+
public interface AsyncComplete {
4+
void complete();
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.servicestack.client;
2+
3+
public interface AsyncError {
4+
void error(Exception ex);
5+
}

0 commit comments

Comments
 (0)