1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Refactor httm methods to return response instead of response code

This commit is contained in:
davigonz 2018-06-06 13:53:14 +02:00
parent 25bba85ee1
commit 01d4592de0
6 changed files with 30 additions and 35 deletions

View File

@ -59,6 +59,7 @@ import java.util.Arrays;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Response;
public class OwnCloudClient extends HttpClient {
@ -309,21 +310,21 @@ public class OwnCloudClient extends HttpClient {
return status;
}
public int executeHttpMethod (HttpBaseMethod method) throws Exception {
public Response executeHttpMethod (HttpBaseMethod method) throws Exception {
boolean repeatWithFreshCredentials;
int repeatCounter = 0;
int status;
Response response;
do {
status = method.execute();
repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
response = method.execute();
repeatWithFreshCredentials = checkUnauthorizedAccess(response.code(), repeatCounter);
if (repeatWithFreshCredentials) {
repeatCounter++;
}
} while (repeatWithFreshCredentials);
return status;
return response;
}
private void checkFirstRedirection(HttpMethod method) {

View File

@ -4,15 +4,10 @@ import okhttp3.Request;
import okhttp3.Response;
public abstract class HttpBaseMethod {
public abstract int execute() throws Exception;
public abstract Response execute() throws Exception;
protected Request mRequest;
protected Response mResponse;
public Request getRequest() {
return mRequest;
}
public Response getResponse() {
return mResponse;
}
}

View File

@ -4,6 +4,7 @@ import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class GetMethod extends HttpMethod {
@ -12,13 +13,12 @@ public class GetMethod extends HttpMethod {
}
@Override
public int execute() throws IOException {
public Response execute() throws IOException {
mRequest = mBaseRequest
.newBuilder()
.get()
.build();
mResponse = mOkHttpClient.newCall(mRequest).execute();
return mResponse.code();
return mOkHttpClient.newCall(mRequest).execute();
}
}

View File

@ -10,6 +10,7 @@ import at.bitfire.dav4android.exception.HttpException;
import at.bitfire.dav4android.exception.UnauthorizedException;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Response;
public class PropfindMethod extends DavMethod {
@ -22,8 +23,7 @@ public class PropfindMethod extends DavMethod {
};
@Override
public int execute() throws IOException, HttpException, DavException {
public Response execute() throws IOException, HttpException, DavException {
try {
mDavResource.propfind(mDepth, PropertyUtils.INSTANCE.getAllPropSet());
mMembers = mDavResource.getMembers();
@ -32,9 +32,7 @@ public class PropfindMethod extends DavMethod {
}
mRequest = mDavResource.getRequest();
mResponse = mDavResource.getResponse();
return mResponse.code();
return mDavResource.getResponse();
}
public int getDepth() {

View File

@ -35,6 +35,7 @@ import com.owncloud.android.lib.common.utils.Log_OC;
import org.apache.commons.httpclient.HttpStatus;
import okhttp3.HttpUrl;
import okhttp3.Response;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
@ -86,7 +87,7 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mPath)),
0);
int status = client.executeHttpMethod(propfindMethod);
Response response = client.executeHttpMethod(propfindMethod);
// if (previousFollowRedirects) {
// mRedirectionPath = client.followRedirection(propfind);
@ -101,21 +102,22 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
* 404 NOT FOUND: path doesn't exist,
* 207 MULTI_STATUS: path exists.
*/
boolean isSuccess = ((status == HttpStatus.SC_OK || status == HttpStatus.SC_MULTI_STATUS) &&
!mSuccessIfAbsent) ||
(status == HttpStatus.SC_MULTI_STATUS && !mSuccessIfAbsent) ||
(status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
boolean isSuccess = ((response.code() == HttpStatus.SC_OK
|| response.code() == HttpStatus.SC_MULTI_STATUS)
&& !mSuccessIfAbsent)
|| (response.code() == HttpStatus.SC_MULTI_STATUS && !mSuccessIfAbsent)
|| (response.code() == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
result = isSuccess
? new RemoteOperationResult(OK)
: new RemoteOperationResult(
false, propfindMethod.getRequest(), propfindMethod.getResponse()
false, propfindMethod.getRequest(), response
);
Log_OC.d(TAG, "Existence check for " + client.getWebdavUri() +
WebdavUtils.encodePath(mPath) + " targeting for " +
(mSuccessIfAbsent ? " absence " : " existence ") +
"finished with HTTP status " + status + (!isSuccess?"(FAIL)":""));
"finished with HTTP status " + response.code() + (!isSuccess?"(FAIL)":""));
} catch (Exception e) {
result = new RemoteOperationResult(e);
@ -130,7 +132,6 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
return result;
}
/**
* Gets the sequence of redirections followed during the execution of the operation.
*
@ -146,4 +147,4 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
public boolean wasRedirected() {
return (mRedirectionPath != null && mRedirectionPath.getRedirectionsCount() > 0);
}
}
}

View File

@ -34,6 +34,7 @@ import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.common.http.nonwebdav.GetMethod;
import okhttp3.Request;
import okhttp3.Response;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
@ -75,13 +76,12 @@ public class GetRemoteUserInfoOperation extends RemoteOperation {
.build();
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request);
int status = client.executeHttpMethod(getMethod);
String response = getMethod.getResponse().body().string();
Response response = client.executeHttpMethod(getMethod);
if (isSuccess(status)) {
if (isSuccess(response.code())) {
Log_OC.d(TAG, "Successful response");
JSONObject respJSON = new JSONObject(response);
JSONObject respJSON = new JSONObject(response.body().string());
JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
JSONObject respData = respOCS.getJSONObject(NODE_DATA);
@ -97,12 +97,12 @@ public class GetRemoteUserInfoOperation extends RemoteOperation {
result.setData(data);
} else {
result = new RemoteOperationResult(false, getMethod.getRequest(), getMethod.getResponse());
result = new RemoteOperationResult(false, getMethod.getRequest(), response);
Log_OC.e(TAG, "Failed response while getting user information ");
if (response != null) {
Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
Log_OC.e(TAG, "*** status code: " + response.code() + " ; response message: " + response);
} else {
Log_OC.e(TAG, "*** status code: " + status);
Log_OC.e(TAG, "*** status code: " + response.code());
}
}
} catch (Exception e) {