From 105151969c0473e0c87d7a0a1d0e3faf4f655f5e Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Sun, 24 Nov 2024 10:06:52 +0800 Subject: [PATCH] fix and implement convenience APIs for postFileWithRequest/postFilesWithRequest --- .../android/src/main/AndroidManifest.xml | 3 +- .../android/AndroidServiceClient.java | 45 +++++++++++++++++++ .../client/AsyncServiceClient.java | 8 ++++ .../client/JsonServiceClient.java | 41 ++++++++++------- .../servicestack/client/ServiceClient.java | 8 ++++ .../client/AsyncServiceClient.java | 8 ++++ .../client/JsonServiceClient.java | 41 ++++++++++------- .../servicestack/client/ServiceClient.java | 8 ++++ 8 files changed, 130 insertions(+), 32 deletions(-) diff --git a/src/AndroidClient/android/src/main/AndroidManifest.xml b/src/AndroidClient/android/src/main/AndroidManifest.xml index c4ca74e9..6edf95c4 100644 --- a/src/AndroidClient/android/src/main/AndroidManifest.xml +++ b/src/AndroidClient/android/src/main/AndroidManifest.xml @@ -1,5 +1,4 @@ - + diff --git a/src/AndroidClient/android/src/main/java/net/servicestack/android/AndroidServiceClient.java b/src/AndroidClient/android/src/main/java/net/servicestack/android/AndroidServiceClient.java index 37d129a2..a742f635 100644 --- a/src/AndroidClient/android/src/main/java/net/servicestack/android/AndroidServiceClient.java +++ b/src/AndroidClient/android/src/main/java/net/servicestack/android/AndroidServiceClient.java @@ -17,6 +17,7 @@ import net.servicestack.client.JsonServiceClient; import net.servicestack.client.Utils; import net.servicestack.cookies.SerializableCookieStore; +import net.servicestack.client.FileUpload; import java.lang.reflect.Type; import java.net.CookieHandler; @@ -908,4 +909,48 @@ protected void onPostExecute(byte[] bytes) { public void deleteAsync(String path, AsyncSuccess success) { deleteAsync(path, createAsyncResult(success, null)); } + + @Override + public void postFileWithRequestAsync(IReturn request, FileUpload file, final AsyncResult asyncResult) { + this.postFilesWithRequestAsync(this.apiUrl(request), request, new FileUpload[]{file}, request.getResponseType(), asyncResult); + } + @Override + public void postFileWithRequestAsync(Object request, FileUpload file, Object responseType, final AsyncResult asyncResult) { + this.postFilesWithRequestAsync(this.apiUrl(request), request, new FileUpload[]{file}, responseType, asyncResult); + } + @Override + public void postFileWithRequestAsync(String path, Object request, FileUpload file, Object responseType, final AsyncResult asyncResult) { + this.postFilesWithRequestAsync(path, request, new FileUpload[]{file}, responseType, asyncResult); + } + + @Override + public void postFilesWithRequestAsync(IReturn request, FileUpload[] files, final AsyncResult asyncResult) { + this.postFilesWithRequestAsync(this.apiUrl(request), request, files, request.getResponseType(), asyncResult); + } + @Override + public void postFilesWithRequestAsync(Object request, FileUpload[] files, Object responseType, final AsyncResult asyncResult) { + this.postFilesWithRequestAsync(this.apiUrl(request), request, files, responseType, asyncResult); + } + + @Override + public void postFilesWithRequestAsync(String path, Object request, FileUpload[] files, Object responseType, final AsyncResult asyncResult) { + final AndroidServiceClient client = this; + execTask(new AsyncTask() { + @Override + protected T doInBackground(String... params) { + try { + return client.postFilesWithRequest(params[0], request, files, responseType); + } catch (Exception e) { + asyncResult.setError(e); + return null; + } + } + + @Override + protected void onPostExecute(T response) { + asyncResult.completeResult(response); + } + + }, path); + } } diff --git a/src/AndroidClient/android/src/main/java/net/servicestack/client/AsyncServiceClient.java b/src/AndroidClient/android/src/main/java/net/servicestack/client/AsyncServiceClient.java index 7a775b2b..280da155 100644 --- a/src/AndroidClient/android/src/main/java/net/servicestack/client/AsyncServiceClient.java +++ b/src/AndroidClient/android/src/main/java/net/servicestack/client/AsyncServiceClient.java @@ -78,4 +78,12 @@ public interface AsyncServiceClient { void deleteAsync(String path, final Type responseType, final AsyncSuccess success); void deleteAsync(String path, final AsyncResult asyncResult); void deleteAsync(String path, final AsyncSuccess success); + + void postFileWithRequestAsync(IReturn request, FileUpload file, final AsyncResult asyncResult); + void postFileWithRequestAsync(Object request, FileUpload file, Object responseType, final AsyncResult asyncResult); + void postFileWithRequestAsync(String path, Object request, FileUpload file, Object responseType, final AsyncResult asyncResult); + + void postFilesWithRequestAsync(IReturn request, FileUpload[] files, final AsyncResult asyncResult); + void postFilesWithRequestAsync(Object request, FileUpload[] files, Object responseType, final AsyncResult asyncResult); + void postFilesWithRequestAsync(String path, Object request, FileUpload[] files, Object responseType, final AsyncResult asyncResult); } diff --git a/src/AndroidClient/android/src/main/java/net/servicestack/client/JsonServiceClient.java b/src/AndroidClient/android/src/main/java/net/servicestack/client/JsonServiceClient.java index bad6f446..671f5e7d 100644 --- a/src/AndroidClient/android/src/main/java/net/servicestack/client/JsonServiceClient.java +++ b/src/AndroidClient/android/src/main/java/net/servicestack/client/JsonServiceClient.java @@ -131,12 +131,16 @@ public Object fromJson(String json, Class c) { public void setGson(Gson gson) { this.gson = gson; } + public String apiUrl(Object requestDto){ + return this.replyUrl + requestDto.getClass().getSimpleName(); + } + public String createUrl(Object requestDto) { return createUrl(requestDto, null); } public String createUrl(Object requestDto, Map query) { - String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName(); + String requestUrl = this.apiUrl(requestDto); StringBuilder sb = new StringBuilder(); Field lastField = null; @@ -679,21 +683,33 @@ public void clearCookies() { cookieManager.getCookieStore().removeAll(); } - // Add these methods to JsonServiceClient class: - - public TResponse postFilesWithRequest(IReturn request, FileUpload[] filesresponseType) { - String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName(); - return postFilesWithRequest(requestUrl, request, files, request.getResponseType()); + // Convenience method for single file upload + @Override + public TResponse postFileWithRequest(IReturn request, FileUpload file) { + return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, request.getResponseType()); + } + @Override + public TResponse postFileWithRequest(Object request, FileUpload file, Object responseType) { + return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, responseType); + } + @Override + public TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType) { + return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType); } - public TResponse postFilesWithRequest(Object request, FileUpload[] files, Class responseType) { - String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName(); - return postFilesWithRequest(requestUrl, request, files, responseType); + @Override + public TResponse postFilesWithRequest(IReturn request, FileUpload[] files) { + return this.postFilesWithRequest(this.apiUrl(request), request, files, request.getResponseType()); + } + @Override + public TResponse postFilesWithRequest(Object request, FileUpload[] files, Object responseType) { + return this.postFilesWithRequest(this.apiUrl(request), request, files, responseType); } private static final String BOUNDARY = "---" + UUID.randomUUID().toString() + "---"; - public TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Class responseType) { + @Override + public TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType) { try { // Prepare multipart data ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -744,9 +760,4 @@ private void writeMultipartFile(DataOutputStream dos, FileUpload file) throws IO dos.write(file.getFileBytes()); dos.writeBytes("\r\n"); } - - // Convenience method for single file upload - public TResponse postFileWithRequest(String path, Object request, FileUpload file, Class responseType) { - return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType); - } } diff --git a/src/AndroidClient/android/src/main/java/net/servicestack/client/ServiceClient.java b/src/AndroidClient/android/src/main/java/net/servicestack/client/ServiceClient.java index 7e6ef4b8..dd8443c2 100644 --- a/src/AndroidClient/android/src/main/java/net/servicestack/client/ServiceClient.java +++ b/src/AndroidClient/android/src/main/java/net/servicestack/client/ServiceClient.java @@ -60,4 +60,12 @@ public interface ServiceClient { String getCookieValue(String name); String getTokenCookie(); String getRefreshTokenCookie(); + + TResponse postFileWithRequest(IReturn request, FileUpload file); + TResponse postFileWithRequest(Object request, FileUpload file, Object responseType); + TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType); + + TResponse postFilesWithRequest(IReturn request, FileUpload[] files); + TResponse postFilesWithRequest(Object request, FileUpload[] files, Object responseType); + TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType); } diff --git a/src/AndroidClient/client/src/main/java/net/servicestack/client/AsyncServiceClient.java b/src/AndroidClient/client/src/main/java/net/servicestack/client/AsyncServiceClient.java index 7a775b2b..280da155 100644 --- a/src/AndroidClient/client/src/main/java/net/servicestack/client/AsyncServiceClient.java +++ b/src/AndroidClient/client/src/main/java/net/servicestack/client/AsyncServiceClient.java @@ -78,4 +78,12 @@ public interface AsyncServiceClient { void deleteAsync(String path, final Type responseType, final AsyncSuccess success); void deleteAsync(String path, final AsyncResult asyncResult); void deleteAsync(String path, final AsyncSuccess success); + + void postFileWithRequestAsync(IReturn request, FileUpload file, final AsyncResult asyncResult); + void postFileWithRequestAsync(Object request, FileUpload file, Object responseType, final AsyncResult asyncResult); + void postFileWithRequestAsync(String path, Object request, FileUpload file, Object responseType, final AsyncResult asyncResult); + + void postFilesWithRequestAsync(IReturn request, FileUpload[] files, final AsyncResult asyncResult); + void postFilesWithRequestAsync(Object request, FileUpload[] files, Object responseType, final AsyncResult asyncResult); + void postFilesWithRequestAsync(String path, Object request, FileUpload[] files, Object responseType, final AsyncResult asyncResult); } diff --git a/src/AndroidClient/client/src/main/java/net/servicestack/client/JsonServiceClient.java b/src/AndroidClient/client/src/main/java/net/servicestack/client/JsonServiceClient.java index bad6f446..671f5e7d 100644 --- a/src/AndroidClient/client/src/main/java/net/servicestack/client/JsonServiceClient.java +++ b/src/AndroidClient/client/src/main/java/net/servicestack/client/JsonServiceClient.java @@ -131,12 +131,16 @@ public Object fromJson(String json, Class c) { public void setGson(Gson gson) { this.gson = gson; } + public String apiUrl(Object requestDto){ + return this.replyUrl + requestDto.getClass().getSimpleName(); + } + public String createUrl(Object requestDto) { return createUrl(requestDto, null); } public String createUrl(Object requestDto, Map query) { - String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName(); + String requestUrl = this.apiUrl(requestDto); StringBuilder sb = new StringBuilder(); Field lastField = null; @@ -679,21 +683,33 @@ public void clearCookies() { cookieManager.getCookieStore().removeAll(); } - // Add these methods to JsonServiceClient class: - - public TResponse postFilesWithRequest(IReturn request, FileUpload[] filesresponseType) { - String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName(); - return postFilesWithRequest(requestUrl, request, files, request.getResponseType()); + // Convenience method for single file upload + @Override + public TResponse postFileWithRequest(IReturn request, FileUpload file) { + return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, request.getResponseType()); + } + @Override + public TResponse postFileWithRequest(Object request, FileUpload file, Object responseType) { + return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, responseType); + } + @Override + public TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType) { + return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType); } - public TResponse postFilesWithRequest(Object request, FileUpload[] files, Class responseType) { - String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName(); - return postFilesWithRequest(requestUrl, request, files, responseType); + @Override + public TResponse postFilesWithRequest(IReturn request, FileUpload[] files) { + return this.postFilesWithRequest(this.apiUrl(request), request, files, request.getResponseType()); + } + @Override + public TResponse postFilesWithRequest(Object request, FileUpload[] files, Object responseType) { + return this.postFilesWithRequest(this.apiUrl(request), request, files, responseType); } private static final String BOUNDARY = "---" + UUID.randomUUID().toString() + "---"; - public TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Class responseType) { + @Override + public TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType) { try { // Prepare multipart data ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -744,9 +760,4 @@ private void writeMultipartFile(DataOutputStream dos, FileUpload file) throws IO dos.write(file.getFileBytes()); dos.writeBytes("\r\n"); } - - // Convenience method for single file upload - public TResponse postFileWithRequest(String path, Object request, FileUpload file, Class responseType) { - return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType); - } } diff --git a/src/AndroidClient/client/src/main/java/net/servicestack/client/ServiceClient.java b/src/AndroidClient/client/src/main/java/net/servicestack/client/ServiceClient.java index 7e6ef4b8..dd8443c2 100644 --- a/src/AndroidClient/client/src/main/java/net/servicestack/client/ServiceClient.java +++ b/src/AndroidClient/client/src/main/java/net/servicestack/client/ServiceClient.java @@ -60,4 +60,12 @@ public interface ServiceClient { String getCookieValue(String name); String getTokenCookie(); String getRefreshTokenCookie(); + + TResponse postFileWithRequest(IReturn request, FileUpload file); + TResponse postFileWithRequest(Object request, FileUpload file, Object responseType); + TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType); + + TResponse postFilesWithRequest(IReturn request, FileUpload[] files); + TResponse postFilesWithRequest(Object request, FileUpload[] files, Object responseType); + TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType); }