Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 7b29f8f

Browse files
committed
Create a common response to parse json
1 parent 48ee263 commit 7b29f8f

File tree

3 files changed

+83
-78
lines changed

3 files changed

+83
-78
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/response/CapabilityResponse.kt

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import com.squareup.moshi.JsonClass
3131
data class CapabilityResponse(
3232
@Json(name = NODE_SERVER_VERSION)
3333
val serverVersion: ServerVersion,
34-
@Json(name = NODE_CAPABILITIES)
3534
val capabilities: Capabilities
3635
)
3736

@@ -47,8 +46,7 @@ data class Capabilities(
4746

4847
@JsonClass(generateAdapter = true)
4948
data class CoreCapabilities(
50-
@Json(name = PROPERTY_CORE_POLLINTERVAL)
51-
val pollInterval: Int
49+
val pollinterval: Int
5250
)
5351

5452
@JsonClass(generateAdapter = true)
@@ -112,39 +110,23 @@ data class FileSharingFederation(
112110

113111
@JsonClass(generateAdapter = true)
114112
data class FilesCapabilities(
115-
@Json(name = PROPERTY_FILES_BIGFILECHUNKING)
116-
val filesBigFileChunking: Boolean,
117-
@Json(name = PROPERTY_FILES_UNDELETE)
118-
val filesUnDelete: Boolean,
119-
@Json(name = PROPERTY_FILES_VERSIONING)
120-
val filesVersioning: Boolean
113+
val bigfilechunking: Boolean,
114+
val undelete: Boolean,
115+
val versioning: Boolean
121116
)
122117

123118
@JsonClass(generateAdapter = true)
124119
data class ServerVersion(
125-
@Json(name = PROPERTY_VERSION_MAJOR)
126-
var versionMayor: Int = 0,
127-
@Json(name = PROPERTY_VERSION_MINOR)
128-
var versionMinor: Int = 0,
129-
@Json(name = PROPERTY_VERSION_MICRO)
130-
var versionMicro: Int = 0,
131-
@Json(name = PROPERTY_VERSION_STRING)
132-
var versionString: String = "",
133-
@Json(name = PROPERTY_VERSION_EDITION)
134-
var versionEdition: String = ""
120+
var major: Int = 0,
121+
var minor: Int = 0,
122+
var micro: Int = 0,
123+
var string: String = "",
124+
var edition: String = ""
135125
)
136126

137127
private const val NODE_SERVER_VERSION = "version"
138-
private const val PROPERTY_VERSION_MAJOR = "major"
139-
private const val PROPERTY_VERSION_MINOR = "minor"
140-
private const val PROPERTY_VERSION_MICRO = "micro"
141-
private const val PROPERTY_VERSION_STRING = "string"
142-
private const val PROPERTY_VERSION_EDITION = "edition"
143-
144-
private const val NODE_CAPABILITIES = "capabilities"
145128

146129
private const val NODE_CORE = "core"
147-
private const val PROPERTY_CORE_POLLINTERVAL = "pollinterval"
148130

149131
private const val NODE_FILES_SHARING = "files_sharing"
150132
private const val PROPERTY_FILES_SHARING_API_ENABLED = "api_enabled"
@@ -167,6 +149,3 @@ private const val PROPERTY_FILES_SHARING_PUBLIC_PASSWORD_ENFORCED_UPLOAD_ONLY =
167149
private const val NODE_EXPIRE_DATE = "expire_date"
168150

169151
private const val NODE_FILES = "files"
170-
private const val PROPERTY_FILES_BIGFILECHUNKING = "bigfilechunking"
171-
private const val PROPERTY_FILES_UNDELETE = "undelete"
172-
private const val PROPERTY_FILES_VERSIONING = "versioning"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* ownCloud Android Library is available under MIT license
2+
*
3+
* @author Abel García de Prada
4+
* Copyright (C) 2020 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+
package com.owncloud.android.lib.resources.response
26+
27+
import com.squareup.moshi.JsonClass
28+
29+
@JsonClass(generateAdapter = true)
30+
data class CommonResponse<T>(
31+
val ocs: OCSResponse<T>
32+
)
33+
34+
@JsonClass(generateAdapter = true)
35+
data class OCSResponse<T>(
36+
val meta: MetaData,
37+
val data: T
38+
)
39+
40+
@JsonClass(generateAdapter = true)
41+
data class MetaData(
42+
val status: String,
43+
val statuscode: Int,
44+
val message: String?
45+
)

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
* THE SOFTWARE.
2727
*
2828
*/
29-
3029
package com.owncloud.android.lib.resources.status
3130

3231
import com.owncloud.android.lib.common.OwnCloudClient
@@ -36,11 +35,14 @@ import com.owncloud.android.lib.common.operations.RemoteOperation
3635
import com.owncloud.android.lib.common.operations.RemoteOperationResult
3736
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK
3837
import com.owncloud.android.lib.resources.response.CapabilityResponse
38+
import com.owncloud.android.lib.resources.response.CommonResponse
3939
import com.owncloud.android.lib.resources.status.RemoteCapability.CapabilityBooleanType.Companion.fromBooleanValue
4040
import com.squareup.moshi.JsonAdapter
4141
import com.squareup.moshi.Moshi
42-
import org.json.JSONObject
42+
import com.squareup.moshi.Types
43+
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
4344
import timber.log.Timber
45+
import java.lang.reflect.Type
4446
import java.net.URL
4547

4648
/**
@@ -50,9 +52,6 @@ import java.net.URL
5052
* @author masensio
5153
* @author David González Verdugo
5254
*/
53-
/**
54-
* Constructor
55-
*/
5655
class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
5756

5857
override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteCapability> {
@@ -72,7 +71,7 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
7271

7372
val response = getMethod.responseBodyAsString
7473

75-
if (!isSuccess(status)) {
74+
if (status != HttpConstants.HTTP_OK) {
7675
result = RemoteOperationResult(getMethod)
7776
Timber.e("Failed response while getting capabilities from the server status code: $status; response message: $response")
7877
return result
@@ -81,31 +80,27 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
8180
Timber.d("Successful response: $response")
8281

8382
// Parse the response
84-
val respJSON = JSONObject(response)
85-
val respOCS = respJSON.getJSONObject(NODE_OCS)
86-
val respMeta = respOCS.getJSONObject(NODE_META)
87-
val respData = respOCS.getJSONObject(NODE_DATA)
88-
89-
// Read meta
90-
val statusProp = respMeta.getString(PROPERTY_STATUS).equals(PROPERTY_STATUS_OK, ignoreCase = true)
91-
val statuscode = respMeta.getInt(PROPERTY_STATUSCODE)
92-
val message = respMeta.getString(PROPERTY_MESSAGE)
93-
94-
if (statusProp) {
95-
val moshi: Moshi = Moshi.Builder().build()
96-
val adapter: JsonAdapter<CapabilityResponse> = moshi.adapter(CapabilityResponse::class.java)
97-
val capabilityResponse: CapabilityResponse? = adapter.fromJson(respData.toString())
98-
99-
val remoteCapability = capabilityResponse?.let { mapToModel(it) } ?: RemoteCapability()
83+
val moshi: Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
84+
val type: Type = Types.newParameterizedType(CommonResponse::class.java, CapabilityResponse::class.java)
85+
val adapter: JsonAdapter<CommonResponse<CapabilityResponse>> = moshi.adapter(type)
86+
val commonResponse: CommonResponse<CapabilityResponse>? = adapter.fromJson(response)
87+
88+
// Read MetaData
89+
val statusMessage = commonResponse?.ocs?.meta?.status
90+
val statusCode = commonResponse?.ocs?.meta?.statuscode
91+
val message = commonResponse?.ocs?.meta?.message
92+
93+
if (statusMessage.equals(PROPERTY_STATUS_OK, ignoreCase = true)) {
94+
val remoteCapability = commonResponse?.ocs?.data?.let { mapToModel(it) } ?: RemoteCapability()
10095
// Result
10196
result = RemoteOperationResult(OK)
10297
result.data = remoteCapability
10398

104-
Timber.d("*** Get Capabilities completed $remoteCapability")
99+
Timber.d("*** Get Capabilities completed and parsed to: $remoteCapability")
105100
} else {
106-
result = RemoteOperationResult(statuscode, message, null)
101+
result = RemoteOperationResult(statusCode!!, message, null)
107102
Timber.e("Failed response while getting capabilities from the server ")
108-
Timber.e("*** status: $statusProp; message: $message")
103+
Timber.e("*** statusCode: $status; status: $statusMessage; message: $message")
109104
}
110105

111106
} catch (e: Exception) {
@@ -116,19 +111,15 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
116111
return result
117112
}
118113

119-
private fun isSuccess(status: Int): Boolean {
120-
return status == HttpConstants.HTTP_OK
121-
}
122-
123114
private fun mapToModel(capabilityResponse: CapabilityResponse): RemoteCapability =
124115
with(capabilityResponse) {
125116
RemoteCapability(
126-
versionMayor = serverVersion.versionMayor,
127-
versionMinor = serverVersion.versionMinor,
128-
versionMicro = serverVersion.versionMicro,
129-
versionString = serverVersion.versionString,
130-
versionEdition = serverVersion.versionEdition,
131-
corePollinterval = capabilities.coreCapabilities.pollInterval,
117+
versionMayor = serverVersion.major,
118+
versionMinor = serverVersion.minor,
119+
versionMicro = serverVersion.micro,
120+
versionString = serverVersion.string,
121+
versionEdition = serverVersion.edition,
122+
corePollinterval = capabilities.coreCapabilities.pollinterval,
132123
filesSharingApiEnabled = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingApiEnabled),
133124
filesSharingResharing = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingReSharing),
134125
filesSharingPublicEnabled = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingPublic.enabled),
@@ -142,9 +133,9 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
142133
filesSharingPublicExpireDateEnabled = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingPublic.fileSharingPublicExpireDate.enabled),
143134
filesSharingPublicExpireDateDays = capabilities.fileSharingCapabilities.fileSharingPublic.fileSharingPublicExpireDate.days ?: 0,
144135
filesSharingPublicExpireDateEnforced = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingPublic.fileSharingPublicExpireDate.enforced?:false),
145-
filesBigFileChunking = fromBooleanValue(capabilities.filesCapabilities.filesBigFileChunking),
146-
filesUndelete = fromBooleanValue(capabilities.filesCapabilities.filesUnDelete),
147-
filesVersioning = fromBooleanValue(capabilities.filesCapabilities.filesVersioning),
136+
filesBigFileChunking = fromBooleanValue(capabilities.filesCapabilities.bigfilechunking),
137+
filesUndelete = fromBooleanValue(capabilities.filesCapabilities.undelete),
138+
filesVersioning = fromBooleanValue(capabilities.filesCapabilities.versioning),
148139
filesSharingFederationIncoming = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingFederation.incoming),
149140
filesSharingFederationOutgoing = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingFederation.outgoing)
150141
)
@@ -161,16 +152,6 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
161152
// Arguments - constant values
162153
private const val VALUE_FORMAT = "json"
163154

164-
// JSON Node names
165-
private const val NODE_OCS = "ocs"
166-
167-
private const val NODE_META = "meta"
168-
169-
private const val NODE_DATA = "data"
170-
171-
private const val PROPERTY_STATUS = "status"
172155
private const val PROPERTY_STATUS_OK = "ok"
173-
private const val PROPERTY_STATUSCODE = "statuscode"
174-
private const val PROPERTY_MESSAGE = "message"
175156
}
176157
}

0 commit comments

Comments
 (0)