|
| 1 | +/* ownCloud Android Library is available under MIT license |
| 2 | + * @author Abel García de Prada |
| 3 | + * |
| 4 | + * Copyright (C) 2023 ownCloud GmbH. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + * |
| 25 | + */ |
| 26 | +package com.owncloud.android.lib.resources.appregistry |
| 27 | + |
| 28 | +import com.owncloud.android.lib.common.OwnCloudClient |
| 29 | +import com.owncloud.android.lib.common.http.HttpConstants |
| 30 | +import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod |
| 31 | +import com.owncloud.android.lib.common.operations.RemoteOperation |
| 32 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult |
| 33 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK |
| 34 | +import com.owncloud.android.lib.resources.appregistry.responses.AppRegistryResponse |
| 35 | +import com.squareup.moshi.JsonAdapter |
| 36 | +import com.squareup.moshi.Moshi |
| 37 | +import timber.log.Timber |
| 38 | +import java.net.URL |
| 39 | + |
| 40 | +class GetRemoteAppRegistryOperation : RemoteOperation<AppRegistryResponse>() { |
| 41 | + |
| 42 | + override fun run(client: OwnCloudClient): RemoteOperationResult<AppRegistryResponse> { |
| 43 | + var result: RemoteOperationResult<AppRegistryResponse> |
| 44 | + |
| 45 | + try { |
| 46 | + val uriBuilder = client.baseUri.buildUpon().apply { |
| 47 | + appendEncodedPath(APP_REGISTRY_ENDPOINT) |
| 48 | + } |
| 49 | + val getMethod = GetMethod(URL(uriBuilder.build().toString())) |
| 50 | + val status = client.executeHttpMethod(getMethod) |
| 51 | + |
| 52 | + val response = getMethod.getResponseBodyAsString() |
| 53 | + |
| 54 | + if (status == HttpConstants.HTTP_OK) { |
| 55 | + Timber.d("Successful response $response") |
| 56 | + |
| 57 | + // Parse the response |
| 58 | + val moshi: Moshi = Moshi.Builder().build() |
| 59 | + val adapter: JsonAdapter<AppRegistryResponse> = moshi.adapter(AppRegistryResponse::class.java) |
| 60 | + val appRegistryResponse: AppRegistryResponse = response?.let { adapter.fromJson(it) } ?: AppRegistryResponse(value = emptyList()) |
| 61 | + |
| 62 | + result = RemoteOperationResult(OK) |
| 63 | + result.data = appRegistryResponse |
| 64 | + |
| 65 | + Timber.d("Get AppRegistry completed and parsed to ${result.data}") |
| 66 | + } else { |
| 67 | + result = RemoteOperationResult(getMethod) |
| 68 | + Timber.e("Failed response while getting app registry from the server status code: $status; response message: $response") |
| 69 | + } |
| 70 | + |
| 71 | + } catch (e: Exception) { |
| 72 | + result = RemoteOperationResult(e) |
| 73 | + Timber.e(e, "Exception while getting app registry") |
| 74 | + } |
| 75 | + |
| 76 | + return result |
| 77 | + } |
| 78 | + |
| 79 | + companion object { |
| 80 | + private const val APP_REGISTRY_ENDPOINT = "app/list" |
| 81 | + } |
| 82 | +} |
0 commit comments