|
| 1 | +/* ownCloud Android Library is available under MIT license |
| 2 | + * Copyright (C) 2022 ownCloud GmbH. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 18 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 19 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | +package com.owncloud.android.lib.resources.spaces |
| 24 | + |
| 25 | +import android.net.Uri |
| 26 | +import com.owncloud.android.lib.common.OwnCloudClient |
| 27 | +import com.owncloud.android.lib.common.http.HttpConstants |
| 28 | +import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod |
| 29 | +import com.owncloud.android.lib.common.operations.RemoteOperation |
| 30 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult |
| 31 | +import com.owncloud.android.lib.resources.CommonOcsResponse |
| 32 | +import com.owncloud.android.lib.resources.shares.responses.ShareeOcsResponse |
| 33 | +import com.owncloud.android.lib.resources.spaces.responses.SpaceResponse |
| 34 | +import com.owncloud.android.lib.resources.spaces.responses.SpacesResponseWrapper |
| 35 | +import com.squareup.moshi.JsonAdapter |
| 36 | +import com.squareup.moshi.Moshi |
| 37 | +import com.squareup.moshi.Types |
| 38 | +import timber.log.Timber |
| 39 | +import java.lang.reflect.Type |
| 40 | +import java.net.URL |
| 41 | + |
| 42 | +class GetRemoteSpacesOperation : RemoteOperation<List<SpaceResponse>>() { |
| 43 | + override fun run(client: OwnCloudClient): RemoteOperationResult<List<SpaceResponse>> { |
| 44 | + val requestUri = buildRequestUri(client.baseUri) |
| 45 | + |
| 46 | + val getMethod = GetMethod(URL(requestUri.toString())) |
| 47 | + |
| 48 | + return try { |
| 49 | + val status = client.executeHttpMethod(getMethod) |
| 50 | + val response = getMethod.getResponseBodyAsString() |
| 51 | + |
| 52 | + if (isSuccess(status)) { |
| 53 | + onRequestSuccessful(response) |
| 54 | + } else { |
| 55 | + onResultUnsuccessful(getMethod, response, status) |
| 56 | + } |
| 57 | + } catch (e: Exception) { |
| 58 | + Timber.e(e, "Exception while getting remote shares") |
| 59 | + RemoteOperationResult(e) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private fun buildRequestUri(baseUri: Uri) = |
| 64 | + baseUri.buildUpon() |
| 65 | + .appendEncodedPath(GRAPH_API_PATH) |
| 66 | + .appendEncodedPath(ENDPOINT_SPACES_LIST) |
| 67 | + .build() |
| 68 | + |
| 69 | + private fun parseResponse(response: String): List<SpaceResponse> { |
| 70 | + val moshi = Moshi.Builder().build() |
| 71 | + val adapter: JsonAdapter<SpacesResponseWrapper> = moshi.adapter(SpacesResponseWrapper::class.java) |
| 72 | + return adapter.fromJson(response)?.value ?: listOf() |
| 73 | + } |
| 74 | + |
| 75 | + private fun onResultUnsuccessful( |
| 76 | + method: GetMethod, |
| 77 | + response: String?, |
| 78 | + status: Int |
| 79 | + ): RemoteOperationResult<List<SpaceResponse>> { |
| 80 | + Timber.e("Failed response while getting spaces for user") |
| 81 | + if (response != null) { |
| 82 | + Timber.e("*** status code: $status; response message: $response") |
| 83 | + } else { |
| 84 | + Timber.e("*** status code: $status") |
| 85 | + } |
| 86 | + return RemoteOperationResult(method) |
| 87 | + } |
| 88 | + |
| 89 | + private fun onRequestSuccessful(response: String?): RemoteOperationResult<List<SpaceResponse>> { |
| 90 | + val result = RemoteOperationResult<List<SpaceResponse>>(RemoteOperationResult.ResultCode.OK) |
| 91 | + Timber.d("Successful response: $response") |
| 92 | + result.data = response?.let { parseResponse(it) } ?: listOf() |
| 93 | + Timber.d("*** Fetch of spaces completed and parsed to ${result.data}") |
| 94 | + return result |
| 95 | + } |
| 96 | + |
| 97 | + private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK |
| 98 | + |
| 99 | + companion object { |
| 100 | + private const val GRAPH_API_PATH = "graph/v1.0" |
| 101 | + private const val ENDPOINT_SPACES_LIST = "me/drives" |
| 102 | + } |
| 103 | +} |
0 commit comments