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

clean run method by braking down into small functions

This commit is contained in:
Christian Schabesberger 2020-10-06 15:30:24 +02:00 committed by Abel García de Prada
parent 8a04231473
commit 4f72f93f8b

View File

@ -28,12 +28,14 @@
package com.owncloud.android.lib.resources.shares package com.owncloud.android.lib.resources.shares
import android.net.Uri
import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.common.http.HttpConstants import com.owncloud.android.lib.common.http.HttpConstants
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperation
import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK
import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import timber.log.Timber import timber.log.Timber
import java.net.URL import java.net.URL
@ -80,30 +82,17 @@ class GetRemoteShareesOperation
(private val searchString: String, private val page: Int, private val perPage: Int) : (private val searchString: String, private val page: Int, private val perPage: Int) :
RemoteOperation<ArrayList<JSONObject>>() { RemoteOperation<ArrayList<JSONObject>>() {
override fun run(client: OwnCloudClient): RemoteOperationResult<ArrayList<JSONObject>> { private fun buildRequestUri(baseUri: Uri) =
var result: RemoteOperationResult<ArrayList<JSONObject>> baseUri.buildUpon()
try {
val requestUri = client.baseUri
val uriBuilder = requestUri.buildUpon()
.appendEncodedPath(OCS_ROUTE) .appendEncodedPath(OCS_ROUTE)
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT) .appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
.appendQueryParameter(PARAM_ITEM_TYPE, VALUE_ITEM_TYPE) .appendQueryParameter(PARAM_ITEM_TYPE, VALUE_ITEM_TYPE)
.appendQueryParameter(PARAM_SEARCH, searchString) .appendQueryParameter(PARAM_SEARCH, searchString)
.appendQueryParameter(PARAM_PAGE, page.toString()) .appendQueryParameter(PARAM_PAGE, page.toString())
.appendQueryParameter(PARAM_PER_PAGE, perPage.toString()) .appendQueryParameter(PARAM_PER_PAGE, perPage.toString())
.build()
val getMethod = GetMethod(URL(uriBuilder.build().toString())) private fun parseResponse(response: String): Array<JSONArray> {
getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
val status = client.executeHttpMethod(getMethod)
val response = getMethod.getResponseBodyAsString()
if (isSuccess(status)) {
Timber.d("Successful response: $response")
// Parse the response
val respJSON = JSONObject(response) val respJSON = JSONObject(response)
val respOCS = respJSON.getJSONObject(NODE_OCS) val respOCS = respJSON.getJSONObject(NODE_OCS)
val respData = respOCS.getJSONObject(NODE_DATA) val respData = respOCS.getJSONObject(NODE_DATA)
@ -114,7 +103,7 @@ class GetRemoteShareesOperation
val respPartialUsers = respData.getJSONArray(NODE_USERS) val respPartialUsers = respData.getJSONArray(NODE_USERS)
val respPartialGroups = respData.getJSONArray(NODE_GROUPS) val respPartialGroups = respData.getJSONArray(NODE_GROUPS)
val respPartialRemotes = respData.getJSONArray(NODE_REMOTES) val respPartialRemotes = respData.getJSONArray(NODE_REMOTES)
val jsonResults = arrayOf( return arrayOf(
respExactUsers, respExactUsers,
respExactGroups, respExactGroups,
respExactRemotes, respExactRemotes,
@ -122,38 +111,67 @@ class GetRemoteShareesOperation
respPartialGroups, respPartialGroups,
respPartialRemotes respPartialRemotes
) )
val data = ArrayList<JSONObject>() // For result data
for (i in 0..5) {
for (j in 0 until jsonResults[i].length()) {
val jsonResult = jsonResults[i].getJSONObject(j)
data.add(jsonResult)
Timber.d("*** Added item: ${jsonResult.getString(PROPERTY_LABEL)}")
}
} }
result = RemoteOperationResult(OK) private fun onResultUnsuccessful(
result.data = data method: GetMethod,
response: String?,
Timber.d("*** Get Users or groups completed ") status: Int
): RemoteOperationResult<ArrayList<JSONObject>> {
} else {
result = RemoteOperationResult(getMethod)
Timber.e("Failed response while getting users/groups from the server ") Timber.e("Failed response while getting users/groups from the server ")
if (response != null) { if (response != null) {
Timber.e("*** status code: $status; response message: $response") Timber.e("*** status code: $status; response message: $response")
} else { } else {
Timber.e("*** status code: $status") Timber.e("*** status code: $status")
} }
} return RemoteOperationResult(method)
} catch (e: Exception) {
result = RemoteOperationResult(e)
Timber.e(e, "Exception while getting users/groups")
} }
private fun flattenResultData(jsonResults: Array<JSONArray>):ArrayList<JSONObject> {
val data = ArrayList<JSONObject>() // For result data
for (i in 0..jsonResults.size) {
for (j in 0 until jsonResults[i].length()) {
val jsonResult = jsonResults[i].getJSONObject(j)
data.add(jsonResult)
Timber.d("*** Added item: ${jsonResult.getString(PROPERTY_LABEL)}")
}
}
return data
}
private fun onRequestSuccessful(response: String?): RemoteOperationResult<ArrayList<JSONObject>> {
Timber.d("Successful response: $response")
// Parse the response
val jsonResults = parseResponse(response!!)
Timber.d("*** Get Users or groups completed ")
val result = RemoteOperationResult<ArrayList<JSONObject>>(OK)
result.data = flattenResultData(jsonResults)
return result return result
} }
override fun run(client: OwnCloudClient): RemoteOperationResult<ArrayList<JSONObject>> {
val requestUri = buildRequestUri(client.baseUri)
val getMethod = GetMethod(URL(requestUri.toString()))
getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
return try {
val status = client.executeHttpMethod(getMethod)
val response = getMethod.getResponseBodyAsString()
if (!isSuccess(status)) {
onResultUnsuccessful(getMethod, response, status)
} else {
onRequestSuccessful(response)
}
} catch (e: Exception) {
Timber.e(e, "Exception while getting users/groups")
RemoteOperationResult(e)
}
}
private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK
companion object { companion object {