Skip to content

Commit 1051519

Browse files
committed
fix and implement convenience APIs for postFileWithRequest/postFilesWithRequest
1 parent bde61ae commit 1051519

File tree

8 files changed

+130
-32
lines changed

8 files changed

+130
-32
lines changed

src/AndroidClient/android/src/main/AndroidManifest.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="net.servicestack.android">
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
32

43
<uses-permission android:name="android.permission.INTERNET" />
54

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

+45
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import net.servicestack.client.JsonServiceClient;
1818
import net.servicestack.client.Utils;
1919
import net.servicestack.cookies.SerializableCookieStore;
20+
import net.servicestack.client.FileUpload;
2021

2122
import java.lang.reflect.Type;
2223
import java.net.CookieHandler;
@@ -908,4 +909,48 @@ protected void onPostExecute(byte[] bytes) {
908909
public void deleteAsync(String path, AsyncSuccess<byte[]> success) {
909910
deleteAsync(path, createAsyncResult(success, null));
910911
}
912+
913+
@Override
914+
public <T> void postFileWithRequestAsync(IReturn<T> request, FileUpload file, final AsyncResult<T> asyncResult) {
915+
this.<T>postFilesWithRequestAsync(this.apiUrl(request), request, new FileUpload[]{file}, request.getResponseType(), asyncResult);
916+
}
917+
@Override
918+
public <T> void postFileWithRequestAsync(Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult) {
919+
this.<T>postFilesWithRequestAsync(this.apiUrl(request), request, new FileUpload[]{file}, responseType, asyncResult);
920+
}
921+
@Override
922+
public <T> void postFileWithRequestAsync(String path, Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult) {
923+
this.<T>postFilesWithRequestAsync(path, request, new FileUpload[]{file}, responseType, asyncResult);
924+
}
925+
926+
@Override
927+
public <T> void postFilesWithRequestAsync(IReturn<T> request, FileUpload[] files, final AsyncResult<T> asyncResult) {
928+
this.<T>postFilesWithRequestAsync(this.apiUrl(request), request, files, request.getResponseType(), asyncResult);
929+
}
930+
@Override
931+
public <T> void postFilesWithRequestAsync(Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult) {
932+
this.<T>postFilesWithRequestAsync(this.apiUrl(request), request, files, responseType, asyncResult);
933+
}
934+
935+
@Override
936+
public <T> void postFilesWithRequestAsync(String path, Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult) {
937+
final AndroidServiceClient client = this;
938+
execTask(new AsyncTask<String, Void, T>() {
939+
@Override
940+
protected T doInBackground(String... params) {
941+
try {
942+
return client.postFilesWithRequest(params[0], request, files, responseType);
943+
} catch (Exception e) {
944+
asyncResult.setError(e);
945+
return null;
946+
}
947+
}
948+
949+
@Override
950+
protected void onPostExecute(T response) {
951+
asyncResult.completeResult(response);
952+
}
953+
954+
}, path);
955+
}
911956
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ public interface AsyncServiceClient {
7878
<T> void deleteAsync(String path, final Type responseType, final AsyncSuccess<T> success);
7979
void deleteAsync(String path, final AsyncResult<byte[]> asyncResult);
8080
void deleteAsync(String path, final AsyncSuccess<byte[]> success);
81+
82+
<T> void postFileWithRequestAsync(IReturn<T> request, FileUpload file, final AsyncResult<T> asyncResult);
83+
<T> void postFileWithRequestAsync(Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult);
84+
<T> void postFileWithRequestAsync(String path, Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult);
85+
86+
<T> void postFilesWithRequestAsync(IReturn<T> request, FileUpload[] files, final AsyncResult<T> asyncResult);
87+
<T> void postFilesWithRequestAsync(Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult);
88+
<T> void postFilesWithRequestAsync(String path, Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult);
8189
}

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

+26-15
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,16 @@ public Object fromJson(String json, Class c) {
131131

132132
public void setGson(Gson gson) { this.gson = gson; }
133133

134+
public String apiUrl(Object requestDto){
135+
return this.replyUrl + requestDto.getClass().getSimpleName();
136+
}
137+
134138
public String createUrl(Object requestDto) {
135139
return createUrl(requestDto, null);
136140
}
137141

138142
public String createUrl(Object requestDto, Map<String,String> query) {
139-
String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName();
143+
String requestUrl = this.apiUrl(requestDto);
140144

141145
StringBuilder sb = new StringBuilder();
142146
Field lastField = null;
@@ -679,21 +683,33 @@ public void clearCookies() {
679683
cookieManager.getCookieStore().removeAll();
680684
}
681685

682-
// Add these methods to JsonServiceClient class:
683-
684-
public <TResponse> TResponse postFilesWithRequest(IReturn<TResponse> request, FileUpload[] filesresponseType) {
685-
String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName();
686-
return postFilesWithRequest(requestUrl, request, files, request.getResponseType());
686+
// Convenience method for single file upload
687+
@Override
688+
public <TResponse> TResponse postFileWithRequest(IReturn<TResponse> request, FileUpload file) {
689+
return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, request.getResponseType());
690+
}
691+
@Override
692+
public <TResponse> TResponse postFileWithRequest(Object request, FileUpload file, Object responseType) {
693+
return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, responseType);
694+
}
695+
@Override
696+
public <TResponse> TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType) {
697+
return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType);
687698
}
688699

689-
public <TResponse> TResponse postFilesWithRequest(Object request, FileUpload[] files, Class<TResponse> responseType) {
690-
String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName();
691-
return postFilesWithRequest(requestUrl, request, files, responseType);
700+
@Override
701+
public <TResponse> TResponse postFilesWithRequest(IReturn<TResponse> request, FileUpload[] files) {
702+
return this.postFilesWithRequest(this.apiUrl(request), request, files, request.getResponseType());
703+
}
704+
@Override
705+
public <TResponse> TResponse postFilesWithRequest(Object request, FileUpload[] files, Object responseType) {
706+
return this.postFilesWithRequest(this.apiUrl(request), request, files, responseType);
692707
}
693708

694709
private static final String BOUNDARY = "---" + UUID.randomUUID().toString() + "---";
695710

696-
public <TResponse> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Class<TResponse> responseType) {
711+
@Override
712+
public <TResponse> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType) {
697713
try {
698714
// Prepare multipart data
699715
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -744,9 +760,4 @@ private void writeMultipartFile(DataOutputStream dos, FileUpload file) throws IO
744760
dos.write(file.getFileBytes());
745761
dos.writeBytes("\r\n");
746762
}
747-
748-
// Convenience method for single file upload
749-
public <TResponse> TResponse postFileWithRequest(String path, Object request, FileUpload file, Class<TResponse> responseType) {
750-
return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType);
751-
}
752763
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ public interface ServiceClient {
6060
String getCookieValue(String name);
6161
String getTokenCookie();
6262
String getRefreshTokenCookie();
63+
64+
<TResponse> TResponse postFileWithRequest(IReturn<TResponse> request, FileUpload file);
65+
<TResponse> TResponse postFileWithRequest(Object request, FileUpload file, Object responseType);
66+
<TResponse> TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType);
67+
68+
<TResponse> TResponse postFilesWithRequest(IReturn<TResponse> request, FileUpload[] files);
69+
<TResponse> TResponse postFilesWithRequest(Object request, FileUpload[] files, Object responseType);
70+
<TResponse> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType);
6371
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ public interface AsyncServiceClient {
7878
<T> void deleteAsync(String path, final Type responseType, final AsyncSuccess<T> success);
7979
void deleteAsync(String path, final AsyncResult<byte[]> asyncResult);
8080
void deleteAsync(String path, final AsyncSuccess<byte[]> success);
81+
82+
<T> void postFileWithRequestAsync(IReturn<T> request, FileUpload file, final AsyncResult<T> asyncResult);
83+
<T> void postFileWithRequestAsync(Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult);
84+
<T> void postFileWithRequestAsync(String path, Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult);
85+
86+
<T> void postFilesWithRequestAsync(IReturn<T> request, FileUpload[] files, final AsyncResult<T> asyncResult);
87+
<T> void postFilesWithRequestAsync(Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult);
88+
<T> void postFilesWithRequestAsync(String path, Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult);
8189
}

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

+26-15
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,16 @@ public Object fromJson(String json, Class c) {
131131

132132
public void setGson(Gson gson) { this.gson = gson; }
133133

134+
public String apiUrl(Object requestDto){
135+
return this.replyUrl + requestDto.getClass().getSimpleName();
136+
}
137+
134138
public String createUrl(Object requestDto) {
135139
return createUrl(requestDto, null);
136140
}
137141

138142
public String createUrl(Object requestDto, Map<String,String> query) {
139-
String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName();
143+
String requestUrl = this.apiUrl(requestDto);
140144

141145
StringBuilder sb = new StringBuilder();
142146
Field lastField = null;
@@ -679,21 +683,33 @@ public void clearCookies() {
679683
cookieManager.getCookieStore().removeAll();
680684
}
681685

682-
// Add these methods to JsonServiceClient class:
683-
684-
public <TResponse> TResponse postFilesWithRequest(IReturn<TResponse> request, FileUpload[] filesresponseType) {
685-
String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName();
686-
return postFilesWithRequest(requestUrl, request, files, request.getResponseType());
686+
// Convenience method for single file upload
687+
@Override
688+
public <TResponse> TResponse postFileWithRequest(IReturn<TResponse> request, FileUpload file) {
689+
return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, request.getResponseType());
690+
}
691+
@Override
692+
public <TResponse> TResponse postFileWithRequest(Object request, FileUpload file, Object responseType) {
693+
return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, responseType);
694+
}
695+
@Override
696+
public <TResponse> TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType) {
697+
return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType);
687698
}
688699

689-
public <TResponse> TResponse postFilesWithRequest(Object request, FileUpload[] files, Class<TResponse> responseType) {
690-
String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName();
691-
return postFilesWithRequest(requestUrl, request, files, responseType);
700+
@Override
701+
public <TResponse> TResponse postFilesWithRequest(IReturn<TResponse> request, FileUpload[] files) {
702+
return this.postFilesWithRequest(this.apiUrl(request), request, files, request.getResponseType());
703+
}
704+
@Override
705+
public <TResponse> TResponse postFilesWithRequest(Object request, FileUpload[] files, Object responseType) {
706+
return this.postFilesWithRequest(this.apiUrl(request), request, files, responseType);
692707
}
693708

694709
private static final String BOUNDARY = "---" + UUID.randomUUID().toString() + "---";
695710

696-
public <TResponse> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Class<TResponse> responseType) {
711+
@Override
712+
public <TResponse> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType) {
697713
try {
698714
// Prepare multipart data
699715
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -744,9 +760,4 @@ private void writeMultipartFile(DataOutputStream dos, FileUpload file) throws IO
744760
dos.write(file.getFileBytes());
745761
dos.writeBytes("\r\n");
746762
}
747-
748-
// Convenience method for single file upload
749-
public <TResponse> TResponse postFileWithRequest(String path, Object request, FileUpload file, Class<TResponse> responseType) {
750-
return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType);
751-
}
752763
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ public interface ServiceClient {
6060
String getCookieValue(String name);
6161
String getTokenCookie();
6262
String getRefreshTokenCookie();
63+
64+
<TResponse> TResponse postFileWithRequest(IReturn<TResponse> request, FileUpload file);
65+
<TResponse> TResponse postFileWithRequest(Object request, FileUpload file, Object responseType);
66+
<TResponse> TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType);
67+
68+
<TResponse> TResponse postFilesWithRequest(IReturn<TResponse> request, FileUpload[] files);
69+
<TResponse> TResponse postFilesWithRequest(Object request, FileUpload[] files, Object responseType);
70+
<TResponse> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType);
6371
}

0 commit comments

Comments
 (0)