mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
update GetRemoteShareesOperation to use moshi
This commit is contained in:
parent
1c08a942d1
commit
b86638412e
@ -35,9 +35,15 @@ 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 com.owncloud.android.lib.resources.CommonOcsResponse
|
||||||
|
import com.owncloud.android.lib.resources.shares.responses.ShareeOcsResponse
|
||||||
|
import com.squareup.moshi.JsonAdapter
|
||||||
|
import com.squareup.moshi.Moshi
|
||||||
|
import com.squareup.moshi.Types
|
||||||
import org.json.JSONArray
|
import org.json.JSONArray
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import java.lang.reflect.Type
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
|
|
||||||
@ -80,7 +86,7 @@ class GetRemoteShareesOperation
|
|||||||
* @param perPage maximum number of results in a single page
|
* @param perPage maximum number of results in a single page
|
||||||
*/
|
*/
|
||||||
(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<ShareeOcsResponse>() {
|
||||||
|
|
||||||
private fun buildRequestUri(baseUri: Uri) =
|
private fun buildRequestUri(baseUri: Uri) =
|
||||||
baseUri.buildUpon()
|
baseUri.buildUpon()
|
||||||
@ -92,32 +98,18 @@ class GetRemoteShareesOperation
|
|||||||
.appendQueryParameter(PARAM_PER_PAGE, perPage.toString())
|
.appendQueryParameter(PARAM_PER_PAGE, perPage.toString())
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
private fun parseResponse(response: String): Array<JSONArray> {
|
private fun parseResponse(response: String): ShareeOcsResponse? {
|
||||||
val respJSON = JSONObject(response)
|
val moshi = Moshi.Builder().build()
|
||||||
val respOCS = respJSON.getJSONObject(NODE_OCS)
|
val type: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareeOcsResponse::class.java)
|
||||||
val respData = respOCS.getJSONObject(NODE_DATA)
|
val adapter: JsonAdapter<CommonOcsResponse<ShareeOcsResponse>> = moshi.adapter(type)
|
||||||
val respExact = respData.getJSONObject(NODE_EXACT)
|
return adapter.fromJson(response)!!.ocs.data
|
||||||
val respExactUsers = respExact.getJSONArray(NODE_USERS)
|
|
||||||
val respExactGroups = respExact.getJSONArray(NODE_GROUPS)
|
|
||||||
val respExactRemotes = respExact.getJSONArray(NODE_REMOTES)
|
|
||||||
val respPartialUsers = respData.getJSONArray(NODE_USERS)
|
|
||||||
val respPartialGroups = respData.getJSONArray(NODE_GROUPS)
|
|
||||||
val respPartialRemotes = respData.getJSONArray(NODE_REMOTES)
|
|
||||||
return arrayOf(
|
|
||||||
respExactUsers,
|
|
||||||
respExactGroups,
|
|
||||||
respExactRemotes,
|
|
||||||
respPartialUsers,
|
|
||||||
respPartialGroups,
|
|
||||||
respPartialRemotes
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onResultUnsuccessful(
|
private fun onResultUnsuccessful(
|
||||||
method: GetMethod,
|
method: GetMethod,
|
||||||
response: String?,
|
response: String?,
|
||||||
status: Int
|
status: Int
|
||||||
): RemoteOperationResult<ArrayList<JSONObject>> {
|
): RemoteOperationResult<ShareeOcsResponse> {
|
||||||
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")
|
||||||
@ -139,19 +131,15 @@ class GetRemoteShareesOperation
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onRequestSuccessful(response: String?): RemoteOperationResult<ArrayList<JSONObject>> {
|
private fun onRequestSuccessful(response: String?): RemoteOperationResult<ShareeOcsResponse> {
|
||||||
|
val result = RemoteOperationResult<ShareeOcsResponse>(OK)
|
||||||
Timber.d("Successful response: $response")
|
Timber.d("Successful response: $response")
|
||||||
|
result.data = parseResponse(response!!)
|
||||||
// Parse the response
|
|
||||||
val jsonResults = parseResponse(response!!)
|
|
||||||
|
|
||||||
Timber.d("*** Get Users or groups completed ")
|
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>> {
|
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareeOcsResponse> {
|
||||||
val requestUri = buildRequestUri(client.baseUri)
|
val requestUri = buildRequestUri(client.baseUri)
|
||||||
|
|
||||||
val getMethod = GetMethod(URL(requestUri.toString()))
|
val getMethod = GetMethod(URL(requestUri.toString()))
|
||||||
@ -191,12 +179,6 @@ class GetRemoteShareesOperation
|
|||||||
private const val VALUE_ITEM_TYPE = "file" // to get the server search for users / groups
|
private const val VALUE_ITEM_TYPE = "file" // to get the server search for users / groups
|
||||||
|
|
||||||
// JSON Node names
|
// JSON Node names
|
||||||
private const val NODE_OCS = "ocs"
|
|
||||||
private const val NODE_DATA = "data"
|
|
||||||
private const val NODE_EXACT = "exact"
|
|
||||||
private const val NODE_USERS = "users"
|
|
||||||
private const val NODE_GROUPS = "groups"
|
|
||||||
private const val NODE_REMOTES = "remotes"
|
|
||||||
const val NODE_VALUE = "value"
|
const val NODE_VALUE = "value"
|
||||||
const val PROPERTY_LABEL = "label"
|
const val PROPERTY_LABEL = "label"
|
||||||
const val PROPERTY_SHARE_TYPE = "shareType"
|
const val PROPERTY_SHARE_TYPE = "shareType"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user