Skip to content

Commit

Permalink
fix and implement convenience APIs for postFileWithRequest/postFilesW…
Browse files Browse the repository at this point in the history
…ithRequest
  • Loading branch information
mythz committed Nov 24, 2024
1 parent bde61ae commit 1051519
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/AndroidClient/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.servicestack.android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -908,4 +909,48 @@ protected void onPostExecute(byte[] bytes) {
public void deleteAsync(String path, AsyncSuccess<byte[]> success) {
deleteAsync(path, createAsyncResult(success, null));
}

@Override
public <T> void postFileWithRequestAsync(IReturn<T> request, FileUpload file, final AsyncResult<T> asyncResult) {
this.<T>postFilesWithRequestAsync(this.apiUrl(request), request, new FileUpload[]{file}, request.getResponseType(), asyncResult);
}
@Override
public <T> void postFileWithRequestAsync(Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult) {
this.<T>postFilesWithRequestAsync(this.apiUrl(request), request, new FileUpload[]{file}, responseType, asyncResult);
}
@Override
public <T> void postFileWithRequestAsync(String path, Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult) {
this.<T>postFilesWithRequestAsync(path, request, new FileUpload[]{file}, responseType, asyncResult);
}

@Override
public <T> void postFilesWithRequestAsync(IReturn<T> request, FileUpload[] files, final AsyncResult<T> asyncResult) {
this.<T>postFilesWithRequestAsync(this.apiUrl(request), request, files, request.getResponseType(), asyncResult);
}
@Override
public <T> void postFilesWithRequestAsync(Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult) {
this.<T>postFilesWithRequestAsync(this.apiUrl(request), request, files, responseType, asyncResult);
}

@Override
public <T> void postFilesWithRequestAsync(String path, Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult) {
final AndroidServiceClient client = this;
execTask(new AsyncTask<String, Void, T>() {
@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ public interface AsyncServiceClient {
<T> void deleteAsync(String path, final Type responseType, final AsyncSuccess<T> success);
void deleteAsync(String path, final AsyncResult<byte[]> asyncResult);
void deleteAsync(String path, final AsyncSuccess<byte[]> success);

<T> void postFileWithRequestAsync(IReturn<T> request, FileUpload file, final AsyncResult<T> asyncResult);
<T> void postFileWithRequestAsync(Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult);
<T> void postFileWithRequestAsync(String path, Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult);

<T> void postFilesWithRequestAsync(IReturn<T> request, FileUpload[] files, final AsyncResult<T> asyncResult);
<T> void postFilesWithRequestAsync(Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult);
<T> void postFilesWithRequestAsync(String path, Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,String> query) {
String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName();
String requestUrl = this.apiUrl(requestDto);

StringBuilder sb = new StringBuilder();
Field lastField = null;
Expand Down Expand Up @@ -679,21 +683,33 @@ public void clearCookies() {
cookieManager.getCookieStore().removeAll();
}

// Add these methods to JsonServiceClient class:

public <TResponse> TResponse postFilesWithRequest(IReturn<TResponse> 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> TResponse postFileWithRequest(IReturn<TResponse> request, FileUpload file) {
return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, request.getResponseType());
}
@Override
public <TResponse> TResponse postFileWithRequest(Object request, FileUpload file, Object responseType) {
return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, responseType);
}
@Override
public <TResponse> TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType) {
return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType);
}

public <TResponse> TResponse postFilesWithRequest(Object request, FileUpload[] files, Class<TResponse> responseType) {
String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName();
return postFilesWithRequest(requestUrl, request, files, responseType);
@Override
public <TResponse> TResponse postFilesWithRequest(IReturn<TResponse> request, FileUpload[] files) {
return this.postFilesWithRequest(this.apiUrl(request), request, files, request.getResponseType());
}
@Override
public <TResponse> 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> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Class<TResponse> responseType) {
@Override
public <TResponse> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType) {
try {
// Prepare multipart data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand Down Expand Up @@ -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> TResponse postFileWithRequest(String path, Object request, FileUpload file, Class<TResponse> responseType) {
return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public interface ServiceClient {
String getCookieValue(String name);
String getTokenCookie();
String getRefreshTokenCookie();

<TResponse> TResponse postFileWithRequest(IReturn<TResponse> request, FileUpload file);
<TResponse> TResponse postFileWithRequest(Object request, FileUpload file, Object responseType);
<TResponse> TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType);

<TResponse> TResponse postFilesWithRequest(IReturn<TResponse> request, FileUpload[] files);
<TResponse> TResponse postFilesWithRequest(Object request, FileUpload[] files, Object responseType);
<TResponse> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ public interface AsyncServiceClient {
<T> void deleteAsync(String path, final Type responseType, final AsyncSuccess<T> success);
void deleteAsync(String path, final AsyncResult<byte[]> asyncResult);
void deleteAsync(String path, final AsyncSuccess<byte[]> success);

<T> void postFileWithRequestAsync(IReturn<T> request, FileUpload file, final AsyncResult<T> asyncResult);
<T> void postFileWithRequestAsync(Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult);
<T> void postFileWithRequestAsync(String path, Object request, FileUpload file, Object responseType, final AsyncResult<T> asyncResult);

<T> void postFilesWithRequestAsync(IReturn<T> request, FileUpload[] files, final AsyncResult<T> asyncResult);
<T> void postFilesWithRequestAsync(Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult);
<T> void postFilesWithRequestAsync(String path, Object request, FileUpload[] files, Object responseType, final AsyncResult<T> asyncResult);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,String> query) {
String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName();
String requestUrl = this.apiUrl(requestDto);

StringBuilder sb = new StringBuilder();
Field lastField = null;
Expand Down Expand Up @@ -679,21 +683,33 @@ public void clearCookies() {
cookieManager.getCookieStore().removeAll();
}

// Add these methods to JsonServiceClient class:

public <TResponse> TResponse postFilesWithRequest(IReturn<TResponse> 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> TResponse postFileWithRequest(IReturn<TResponse> request, FileUpload file) {
return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, request.getResponseType());
}
@Override
public <TResponse> TResponse postFileWithRequest(Object request, FileUpload file, Object responseType) {
return postFilesWithRequest(this.apiUrl(request), request, new FileUpload[]{file}, responseType);
}
@Override
public <TResponse> TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType) {
return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType);
}

public <TResponse> TResponse postFilesWithRequest(Object request, FileUpload[] files, Class<TResponse> responseType) {
String requestUrl = this.replyUrl + requestDto.getClass().getSimpleName();
return postFilesWithRequest(requestUrl, request, files, responseType);
@Override
public <TResponse> TResponse postFilesWithRequest(IReturn<TResponse> request, FileUpload[] files) {
return this.postFilesWithRequest(this.apiUrl(request), request, files, request.getResponseType());
}
@Override
public <TResponse> 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> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Class<TResponse> responseType) {
@Override
public <TResponse> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType) {
try {
// Prepare multipart data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand Down Expand Up @@ -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> TResponse postFileWithRequest(String path, Object request, FileUpload file, Class<TResponse> responseType) {
return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public interface ServiceClient {
String getCookieValue(String name);
String getTokenCookie();
String getRefreshTokenCookie();

<TResponse> TResponse postFileWithRequest(IReturn<TResponse> request, FileUpload file);
<TResponse> TResponse postFileWithRequest(Object request, FileUpload file, Object responseType);
<TResponse> TResponse postFileWithRequest(String path, Object request, FileUpload file, Object responseType);

<TResponse> TResponse postFilesWithRequest(IReturn<TResponse> request, FileUpload[] files);
<TResponse> TResponse postFilesWithRequest(Object request, FileUpload[] files, Object responseType);
<TResponse> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Object responseType);
}

0 comments on commit 1051519

Please sign in to comment.