|
| 1 | +/* |
| 2 | + * Nextcloud Android Library |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-FileCopyrightText: 2025 Tobias Kaminsky <tobias@kaminsky.me> |
| 6 | + * SPDX-License-Identifier: MIT |
| 7 | + */ |
| 8 | +package com.nextcloud.android.lib.resources.clientintegration |
| 9 | + |
| 10 | +import com.google.gson.GsonBuilder |
| 11 | +import com.google.gson.JsonArray |
| 12 | +import com.google.gson.reflect.TypeToken |
| 13 | +import com.nextcloud.common.NextcloudClient |
| 14 | +import com.nextcloud.operations.GetMethod |
| 15 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult |
| 16 | +import com.owncloud.android.lib.common.utils.Log_OC |
| 17 | +import com.owncloud.android.lib.ocs.ServerResponse |
| 18 | +import com.owncloud.android.lib.resources.OCSRemoteOperation |
| 19 | +import org.apache.commons.httpclient.HttpStatus |
| 20 | + |
| 21 | +/** |
| 22 | + * Get client integration |
| 23 | + */ |
| 24 | +class GetClientIntegrationJsonOperation( |
| 25 | + val url: String |
| 26 | +) : OCSRemoteOperation<JsonArray>() { |
| 27 | + @Suppress("TooGenericExceptionCaught") |
| 28 | + override fun run(client: NextcloudClient): RemoteOperationResult<JsonArray> { |
| 29 | + var result: RemoteOperationResult<JsonArray> |
| 30 | + var getMethod: GetMethod? = null |
| 31 | + try { |
| 32 | + getMethod = |
| 33 | + GetMethod( |
| 34 | + client.baseUri.toString() + url + JSON_FORMAT, |
| 35 | + true |
| 36 | + ) |
| 37 | + val status = client.execute(getMethod) |
| 38 | + if (status == HttpStatus.SC_OK) { |
| 39 | + val terms = |
| 40 | + getServerResponse( |
| 41 | + getMethod, |
| 42 | + object : TypeToken<ServerResponse<JsonArray>>() {} |
| 43 | + )?.ocs?.data |
| 44 | + |
| 45 | + val json = parseResult(getMethod.getResponseBodyAsString()) |
| 46 | + |
| 47 | + if (terms != null) { |
| 48 | + result = RemoteOperationResult(true, getMethod) |
| 49 | + result.resultData = terms |
| 50 | + } else { |
| 51 | + result = RemoteOperationResult(false, getMethod) |
| 52 | + } |
| 53 | + } else { |
| 54 | + result = RemoteOperationResult(false, getMethod) |
| 55 | + } |
| 56 | + } catch (e: Exception) { |
| 57 | + result = RemoteOperationResult(e) |
| 58 | + Log_OC.e( |
| 59 | + TAG, |
| 60 | + "Get client integration failed: " + result.logMessage, |
| 61 | + result.exception |
| 62 | + ) |
| 63 | + } finally { |
| 64 | + getMethod?.releaseConnection() |
| 65 | + } |
| 66 | + return result |
| 67 | + } |
| 68 | + |
| 69 | + fun parseResult(response: String): ClientIntegrationUI { |
| 70 | + val gson = |
| 71 | + GsonBuilder() |
| 72 | + .registerTypeHierarchyAdapter(Element::class.java, ElementTypeAdapter()) |
| 73 | + .create() |
| 74 | + |
| 75 | + return gson.fromJson(response, ClientIntegrationUI::class.java) |
| 76 | + } |
| 77 | + |
| 78 | + companion object { |
| 79 | + private val TAG = GetClientIntegrationJsonOperation::class.java.simpleName |
| 80 | + } |
| 81 | +} |
0 commit comments