Skip to content

Commit 7421b34

Browse files
committed
Add support for sending IReturnVoid requests async
1 parent bdfdaf2 commit 7421b34

File tree

4 files changed

+148
-23
lines changed

4 files changed

+148
-23
lines changed

src/AndroidClient/android/src/androidTest/java/net/servicestack/android/TestServiceTestsAsync.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import android.text.TextUtils;
66

77
import net.servicestack.client.AsyncResult;
8+
import net.servicestack.client.AsyncResultVoid;
89
import net.servicestack.client.ConnectionFilter;
910
import net.servicestack.client.ExceptionFilter;
11+
import net.servicestack.client.HttpMethods;
1012
import net.servicestack.client.JsonServiceClient;
1113
import net.servicestack.client.Log;
1214
import net.servicestack.client.ResponseStatus;
@@ -233,6 +235,26 @@ public void complete() {
233235
});
234236
}
235237

238+
public void test_Can_send_ReturnVoid_Async(){
239+
final CountDownLatch signal = new CountDownLatch(1);
240+
241+
final List<String> sentMethods = new ArrayList<>();
242+
client.RequestFilter = new ConnectionFilter() {
243+
@Override
244+
public void exec(HttpURLConnection conn) {
245+
sentMethods.add(conn.getRequestMethod());
246+
}
247+
};
248+
249+
client.sendAsync(new HelloReturnVoid().setId(1), new AsyncResultVoid() {
250+
@Override
251+
public void success() {
252+
assertEquals(HttpMethods.Post, sentMethods.get(sentMethods.size() - 1));
253+
signal.countDown();
254+
}
255+
});
256+
}
257+
236258

237259
/* TEST HELPERS */
238260
public static HelloAllTypes createHelloAllTypes(){

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import android.os.AsyncTask;
77

88
import net.servicestack.client.AsyncResult;
9+
import net.servicestack.client.AsyncResultVoid;
910
import net.servicestack.client.AsyncServiceClient;
1011
import net.servicestack.client.IReturn;
12+
import net.servicestack.client.IReturnVoid;
1113
import net.servicestack.client.JsonServiceClient;
1214
import net.servicestack.client.Utils;
1315

@@ -50,6 +52,28 @@ protected void onPostExecute(T response) {
5052
}, request);
5153
}
5254

55+
@Override
56+
public void sendAsync(IReturnVoid request, final AsyncResultVoid asyncResult) {
57+
final AndroidServiceClient client = this;
58+
execTask(new AsyncTask<IReturnVoid, Void, Void>() {
59+
@Override
60+
protected Void doInBackground(IReturnVoid... params) {
61+
try {
62+
client.send(params[0]);
63+
} catch (Exception e) {
64+
asyncResult.setError(e);
65+
}
66+
return null;
67+
}
68+
69+
@Override
70+
protected void onPostExecute(Void noResponse) {
71+
asyncResult.completeResult();
72+
}
73+
74+
}, request);
75+
}
76+
5377
/* GET */
5478
public <T> void getAsync(IReturn<T> request, final AsyncResult<T> asyncResult){
5579
final AndroidServiceClient client = this;
@@ -181,6 +205,28 @@ protected void onPostExecute(T response) {
181205
}, request);
182206
}
183207

208+
@Override
209+
public void postAsync(IReturnVoid request, final AsyncResultVoid asyncResult) {
210+
final AndroidServiceClient client = this;
211+
execTask(new AsyncTask<IReturnVoid, Void, Void>() {
212+
@Override
213+
protected Void doInBackground(IReturnVoid... params) {
214+
try {
215+
client.post(params[0]);
216+
} catch (Exception e) {
217+
asyncResult.setError(e);
218+
}
219+
return null;
220+
}
221+
222+
@Override
223+
protected void onPostExecute(Void noResponse) {
224+
asyncResult.completeResult();
225+
}
226+
227+
}, request);
228+
}
229+
184230
@Override
185231
public <T> void postAsync(String path, final Object request, final Class responseType, final AsyncResult<T> asyncResult) {
186232
final AndroidServiceClient client = this;
@@ -316,6 +362,28 @@ protected void onPostExecute(T response) {
316362
}, request);
317363
}
318364

365+
@Override
366+
public void putAsync(IReturnVoid request, final AsyncResultVoid asyncResult) {
367+
final AndroidServiceClient client = this;
368+
execTask(new AsyncTask<IReturnVoid, Void, Void>() {
369+
@Override
370+
protected Void doInBackground(IReturnVoid... params) {
371+
try {
372+
client.put(params[0]);
373+
} catch (Exception e) {
374+
asyncResult.setError(e);
375+
}
376+
return null;
377+
}
378+
379+
@Override
380+
protected void onPostExecute(Void noResponse) {
381+
asyncResult.completeResult();
382+
}
383+
384+
}, request);
385+
}
386+
319387
@Override
320388
public <T> void putAsync(String path, final Object request, final Class responseType, final AsyncResult<T> asyncResult) {
321389
final AndroidServiceClient client = this;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 abstract class AsyncResultVoid {
7+
boolean completed = false;
8+
Exception ex;
9+
10+
public final Exception getError() { return ex; }
11+
public final void setError(Exception value) {
12+
completed = true;
13+
ex = value;
14+
}
15+
16+
public final void completeResult(){
17+
try {
18+
if (ex == null){
19+
success();
20+
}
21+
else {
22+
error(ex);
23+
}
24+
} finally {
25+
complete();
26+
}
27+
}
28+
29+
public void success(){}
30+
public void error(Exception ex){}
31+
public void complete(){}
32+
}

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,34 @@
88
import java.util.Map;
99

1010
public interface AsyncServiceClient {
11-
public <T> void sendAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
11+
<T> void sendAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
12+
void sendAsync(IReturnVoid request, final AsyncResultVoid asyncResult);
1213

13-
public <T> void getAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
14-
public <T> void getAsync(IReturn<T> request, final Map<String, String> queryParams, final AsyncResult<T> asyncResult);
15-
public <T> void getAsync(String path, final Class responseType, final AsyncResult<T> asyncResult);
16-
public <T> void getAsync(String path, final Type responseType, final AsyncResult<T> asyncResult);
17-
public void getAsync(String path, final AsyncResult<byte[]> asyncResult);
14+
<T> void getAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
15+
<T> void getAsync(IReturn<T> request, final Map<String, String> queryParams, final AsyncResult<T> asyncResult);
16+
<T> void getAsync(String path, final Class responseType, final AsyncResult<T> asyncResult);
17+
<T> void getAsync(String path, final Type responseType, final AsyncResult<T> asyncResult);
18+
void getAsync(String path, final AsyncResult<byte[]> asyncResult);
1819

19-
public <T> void postAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
20-
public <T> void postAsync(String path, final Object request, final Class responseType, final AsyncResult<T> asyncResult);
21-
public <T> void postAsync(String path, final Object request, final Type responseType, final AsyncResult<T> asyncResult);
22-
public <T> void postAsync(String path, final byte[] requestBody, final String contentType, final Class responseType, final AsyncResult<T> asyncResult);
23-
public <T> void postAsync(String path, final byte[] requestBody, final String contentType, final Type responseType, final AsyncResult<T> asyncResult);
24-
public void postAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<byte[]> asyncResult);
20+
<T> void postAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
21+
void postAsync(IReturnVoid request, final AsyncResultVoid asyncResult);
22+
<T> void postAsync(String path, final Object request, final Class responseType, final AsyncResult<T> asyncResult);
23+
<T> void postAsync(String path, final Object request, final Type responseType, final AsyncResult<T> asyncResult);
24+
<T> void postAsync(String path, final byte[] requestBody, final String contentType, final Class responseType, final AsyncResult<T> asyncResult);
25+
<T> void postAsync(String path, final byte[] requestBody, final String contentType, final Type responseType, final AsyncResult<T> asyncResult);
26+
void postAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<byte[]> asyncResult);
2527

26-
public <T> void putAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
27-
public <T> void putAsync(String path, final Object request, final Class responseType, final AsyncResult<T> asyncResult);
28-
public <T> void putAsync(String path, final Object request, final Type responseType, final AsyncResult<T> asyncResult);
29-
public <T> void putAsync(String path, final byte[] requestBody, final String contentType, final Class responseType, final AsyncResult<T> asyncResult);
30-
public <T> void putAsync(String path, final byte[] requestBody, final String contentType, final Type responseType, final AsyncResult<T> asyncResult);
31-
public void putAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<byte[]> asyncResult);
28+
<T> void putAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
29+
void putAsync(IReturnVoid request, final AsyncResultVoid asyncResult);
30+
<T> void putAsync(String path, final Object request, final Class responseType, final AsyncResult<T> asyncResult);
31+
<T> void putAsync(String path, final Object request, final Type responseType, final AsyncResult<T> asyncResult);
32+
<T> void putAsync(String path, final byte[] requestBody, final String contentType, final Class responseType, final AsyncResult<T> asyncResult);
33+
<T> void putAsync(String path, final byte[] requestBody, final String contentType, final Type responseType, final AsyncResult<T> asyncResult);
34+
void putAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<byte[]> asyncResult);
3235

33-
public <T> void deleteAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
34-
public <T> void deleteAsync(IReturn<T> request, final Map<String, String> queryParams, final AsyncResult<T> asyncResult);
35-
public <T> void deleteAsync(String path, final Class responseType, final AsyncResult<T> asyncResult);
36-
public <T> void deleteAsync(String path, final Type responseType, final AsyncResult<T> asyncResult);
37-
public void deleteAsync(String path, final AsyncResult<byte[]> asyncResult);
36+
<T> void deleteAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
37+
<T> void deleteAsync(IReturn<T> request, final Map<String, String> queryParams, final AsyncResult<T> asyncResult);
38+
<T> void deleteAsync(String path, final Class responseType, final AsyncResult<T> asyncResult);
39+
<T> void deleteAsync(String path, final Type responseType, final AsyncResult<T> asyncResult);
40+
void deleteAsync(String path, final AsyncResult<byte[]> asyncResult);
3841
}

0 commit comments

Comments
 (0)