mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Create a common response to parse json
This commit is contained in:
		
							parent
							
								
									48ee263951
								
							
						
					
					
						commit
						7b29f8f09e
					
				| @ -31,7 +31,6 @@ import com.squareup.moshi.JsonClass | ||||
| data class CapabilityResponse( | ||||
|     @Json(name = NODE_SERVER_VERSION) | ||||
|     val serverVersion: ServerVersion, | ||||
|     @Json(name = NODE_CAPABILITIES) | ||||
|     val capabilities: Capabilities | ||||
| ) | ||||
| 
 | ||||
| @ -47,8 +46,7 @@ data class Capabilities( | ||||
| 
 | ||||
| @JsonClass(generateAdapter = true) | ||||
| data class CoreCapabilities( | ||||
|     @Json(name = PROPERTY_CORE_POLLINTERVAL) | ||||
|     val pollInterval: Int | ||||
|     val pollinterval: Int | ||||
| ) | ||||
| 
 | ||||
| @JsonClass(generateAdapter = true) | ||||
| @ -112,39 +110,23 @@ data class FileSharingFederation( | ||||
| 
 | ||||
| @JsonClass(generateAdapter = true) | ||||
| data class FilesCapabilities( | ||||
|     @Json(name = PROPERTY_FILES_BIGFILECHUNKING) | ||||
|     val filesBigFileChunking: Boolean, | ||||
|     @Json(name = PROPERTY_FILES_UNDELETE) | ||||
|     val filesUnDelete: Boolean, | ||||
|     @Json(name = PROPERTY_FILES_VERSIONING) | ||||
|     val filesVersioning: Boolean | ||||
|     val bigfilechunking: Boolean, | ||||
|     val undelete: Boolean, | ||||
|     val versioning: Boolean | ||||
| ) | ||||
| 
 | ||||
| @JsonClass(generateAdapter = true) | ||||
| data class ServerVersion( | ||||
|     @Json(name = PROPERTY_VERSION_MAJOR) | ||||
|     var versionMayor: Int = 0, | ||||
|     @Json(name = PROPERTY_VERSION_MINOR) | ||||
|     var versionMinor: Int = 0, | ||||
|     @Json(name = PROPERTY_VERSION_MICRO) | ||||
|     var versionMicro: Int = 0, | ||||
|     @Json(name = PROPERTY_VERSION_STRING) | ||||
|     var versionString: String = "", | ||||
|     @Json(name = PROPERTY_VERSION_EDITION) | ||||
|     var versionEdition: String = "" | ||||
|     var major: Int = 0, | ||||
|     var minor: Int = 0, | ||||
|     var micro: Int = 0, | ||||
|     var string: String = "", | ||||
|     var edition: String = "" | ||||
| ) | ||||
| 
 | ||||
| private const val NODE_SERVER_VERSION = "version" | ||||
| private const val PROPERTY_VERSION_MAJOR = "major" | ||||
| private const val PROPERTY_VERSION_MINOR = "minor" | ||||
| private const val PROPERTY_VERSION_MICRO = "micro" | ||||
| private const val PROPERTY_VERSION_STRING = "string" | ||||
| private const val PROPERTY_VERSION_EDITION = "edition" | ||||
| 
 | ||||
| private const val NODE_CAPABILITIES = "capabilities" | ||||
| 
 | ||||
| private const val NODE_CORE = "core" | ||||
| private const val PROPERTY_CORE_POLLINTERVAL = "pollinterval" | ||||
| 
 | ||||
| private const val NODE_FILES_SHARING = "files_sharing" | ||||
| 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 = | ||||
| private const val NODE_EXPIRE_DATE = "expire_date" | ||||
| 
 | ||||
| private const val NODE_FILES = "files" | ||||
| private const val PROPERTY_FILES_BIGFILECHUNKING = "bigfilechunking" | ||||
| private const val PROPERTY_FILES_UNDELETE = "undelete" | ||||
| private const val PROPERTY_FILES_VERSIONING = "versioning" | ||||
|  | ||||
| @ -0,0 +1,45 @@ | ||||
| /* ownCloud Android Library is available under MIT license | ||||
|  * | ||||
|  *   @author Abel García de Prada | ||||
|  *   Copyright (C) 2020 ownCloud GmbH. | ||||
|  * | ||||
|  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  *   of this software and associated documentation files (the "Software"), to deal | ||||
|  *   in the Software without restriction, including without limitation the rights | ||||
|  *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  *   copies of the Software, and to permit persons to whom the Software is | ||||
|  *   furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  *   The above copyright notice and this permission notice shall be included in | ||||
|  *   all copies or substantial portions of the Software. | ||||
|  * | ||||
|  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
|  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||
|  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||
|  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||||
|  *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||||
|  *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||
|  *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  *   THE SOFTWARE. | ||||
|  */ | ||||
| package com.owncloud.android.lib.resources.response | ||||
| 
 | ||||
| import com.squareup.moshi.JsonClass | ||||
| 
 | ||||
| @JsonClass(generateAdapter = true) | ||||
| data class CommonResponse<T>( | ||||
|     val ocs: OCSResponse<T> | ||||
| ) | ||||
| 
 | ||||
| @JsonClass(generateAdapter = true) | ||||
| data class OCSResponse<T>( | ||||
|     val meta: MetaData, | ||||
|     val data: T | ||||
| ) | ||||
| 
 | ||||
| @JsonClass(generateAdapter = true) | ||||
| data class MetaData( | ||||
|     val status: String, | ||||
|     val statuscode: Int, | ||||
|     val message: String? | ||||
| ) | ||||
| @ -26,7 +26,6 @@ | ||||
|  *   THE SOFTWARE. | ||||
|  * | ||||
|  */ | ||||
| 
 | ||||
| package com.owncloud.android.lib.resources.status | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.OwnCloudClient | ||||
| @ -36,11 +35,14 @@ import com.owncloud.android.lib.common.operations.RemoteOperation | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK | ||||
| import com.owncloud.android.lib.resources.response.CapabilityResponse | ||||
| import com.owncloud.android.lib.resources.response.CommonResponse | ||||
| import com.owncloud.android.lib.resources.status.RemoteCapability.CapabilityBooleanType.Companion.fromBooleanValue | ||||
| import com.squareup.moshi.JsonAdapter | ||||
| import com.squareup.moshi.Moshi | ||||
| import org.json.JSONObject | ||||
| import com.squareup.moshi.Types | ||||
| import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory | ||||
| import timber.log.Timber | ||||
| import java.lang.reflect.Type | ||||
| import java.net.URL | ||||
| 
 | ||||
| /** | ||||
| @ -50,9 +52,6 @@ import java.net.URL | ||||
|  * @author masensio | ||||
|  * @author David González Verdugo | ||||
|  */ | ||||
| /** | ||||
|  * Constructor | ||||
|  */ | ||||
| class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() { | ||||
| 
 | ||||
|     override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteCapability> { | ||||
| @ -72,7 +71,7 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() { | ||||
| 
 | ||||
|             val response = getMethod.responseBodyAsString | ||||
| 
 | ||||
|             if (!isSuccess(status)) { | ||||
|             if (status != HttpConstants.HTTP_OK) { | ||||
|                 result = RemoteOperationResult(getMethod) | ||||
|                 Timber.e("Failed response while getting capabilities from the server status code: $status; response message: $response") | ||||
|                 return result | ||||
| @ -81,31 +80,27 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() { | ||||
|             Timber.d("Successful response: $response") | ||||
| 
 | ||||
|             // Parse the response | ||||
|             val respJSON = JSONObject(response) | ||||
|             val respOCS = respJSON.getJSONObject(NODE_OCS) | ||||
|             val respMeta = respOCS.getJSONObject(NODE_META) | ||||
|             val respData = respOCS.getJSONObject(NODE_DATA) | ||||
|             val moshi: Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build() | ||||
|             val type: Type = Types.newParameterizedType(CommonResponse::class.java, CapabilityResponse::class.java) | ||||
|             val adapter: JsonAdapter<CommonResponse<CapabilityResponse>> = moshi.adapter(type) | ||||
|             val commonResponse: CommonResponse<CapabilityResponse>? = adapter.fromJson(response) | ||||
| 
 | ||||
|             // Read meta | ||||
|             val statusProp = respMeta.getString(PROPERTY_STATUS).equals(PROPERTY_STATUS_OK, ignoreCase = true) | ||||
|             val statuscode = respMeta.getInt(PROPERTY_STATUSCODE) | ||||
|             val message = respMeta.getString(PROPERTY_MESSAGE) | ||||
|             // Read MetaData | ||||
|             val statusMessage = commonResponse?.ocs?.meta?.status | ||||
|             val statusCode = commonResponse?.ocs?.meta?.statuscode | ||||
|             val message = commonResponse?.ocs?.meta?.message | ||||
| 
 | ||||
|             if (statusProp) { | ||||
|                 val moshi: Moshi = Moshi.Builder().build() | ||||
|                 val adapter: JsonAdapter<CapabilityResponse> = moshi.adapter(CapabilityResponse::class.java) | ||||
|                 val capabilityResponse: CapabilityResponse? = adapter.fromJson(respData.toString()) | ||||
| 
 | ||||
|                 val remoteCapability = capabilityResponse?.let { mapToModel(it) } ?: RemoteCapability() | ||||
|             if (statusMessage.equals(PROPERTY_STATUS_OK, ignoreCase = true)) { | ||||
|                 val remoteCapability = commonResponse?.ocs?.data?.let { mapToModel(it) } ?: RemoteCapability() | ||||
|                 // Result | ||||
|                 result = RemoteOperationResult(OK) | ||||
|                 result.data = remoteCapability | ||||
| 
 | ||||
|                 Timber.d("*** Get Capabilities completed $remoteCapability") | ||||
|                 Timber.d("*** Get Capabilities completed and parsed to: $remoteCapability") | ||||
|             } else { | ||||
|                 result = RemoteOperationResult(statuscode, message, null) | ||||
|                 result = RemoteOperationResult(statusCode!!, message, null) | ||||
|                 Timber.e("Failed response while getting capabilities from the server ") | ||||
|                 Timber.e("*** status: $statusProp; message: $message") | ||||
|                 Timber.e("*** statusCode: $status; status: $statusMessage; message: $message") | ||||
|             } | ||||
| 
 | ||||
|         } catch (e: Exception) { | ||||
| @ -116,19 +111,15 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() { | ||||
|         return result | ||||
|     } | ||||
| 
 | ||||
|     private fun isSuccess(status: Int): Boolean { | ||||
|         return status == HttpConstants.HTTP_OK | ||||
|     } | ||||
| 
 | ||||
|     private fun mapToModel(capabilityResponse: CapabilityResponse): RemoteCapability = | ||||
|         with(capabilityResponse) { | ||||
|             RemoteCapability( | ||||
|                 versionMayor = serverVersion.versionMayor, | ||||
|                 versionMinor = serverVersion.versionMinor, | ||||
|                 versionMicro = serverVersion.versionMicro, | ||||
|                 versionString = serverVersion.versionString, | ||||
|                 versionEdition = serverVersion.versionEdition, | ||||
|                 corePollinterval = capabilities.coreCapabilities.pollInterval, | ||||
|                 versionMayor = serverVersion.major, | ||||
|                 versionMinor = serverVersion.minor, | ||||
|                 versionMicro = serverVersion.micro, | ||||
|                 versionString = serverVersion.string, | ||||
|                 versionEdition = serverVersion.edition, | ||||
|                 corePollinterval = capabilities.coreCapabilities.pollinterval, | ||||
|                 filesSharingApiEnabled = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingApiEnabled), | ||||
|                 filesSharingResharing = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingReSharing), | ||||
|                 filesSharingPublicEnabled = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingPublic.enabled), | ||||
| @ -142,9 +133,9 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() { | ||||
|                 filesSharingPublicExpireDateEnabled = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingPublic.fileSharingPublicExpireDate.enabled), | ||||
|                 filesSharingPublicExpireDateDays = capabilities.fileSharingCapabilities.fileSharingPublic.fileSharingPublicExpireDate.days ?: 0, | ||||
|                 filesSharingPublicExpireDateEnforced = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingPublic.fileSharingPublicExpireDate.enforced?:false), | ||||
|                 filesBigFileChunking = fromBooleanValue(capabilities.filesCapabilities.filesBigFileChunking), | ||||
|                 filesUndelete = fromBooleanValue(capabilities.filesCapabilities.filesUnDelete), | ||||
|                 filesVersioning = fromBooleanValue(capabilities.filesCapabilities.filesVersioning), | ||||
|                 filesBigFileChunking = fromBooleanValue(capabilities.filesCapabilities.bigfilechunking), | ||||
|                 filesUndelete = fromBooleanValue(capabilities.filesCapabilities.undelete), | ||||
|                 filesVersioning = fromBooleanValue(capabilities.filesCapabilities.versioning), | ||||
|                 filesSharingFederationIncoming = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingFederation.incoming), | ||||
|                 filesSharingFederationOutgoing = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingFederation.outgoing) | ||||
|             ) | ||||
| @ -161,16 +152,6 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() { | ||||
|         // Arguments - constant values | ||||
|         private const val VALUE_FORMAT = "json" | ||||
| 
 | ||||
|         // JSON Node names | ||||
|         private const val NODE_OCS = "ocs" | ||||
| 
 | ||||
|         private const val NODE_META = "meta" | ||||
| 
 | ||||
|         private const val NODE_DATA = "data" | ||||
| 
 | ||||
|         private const val PROPERTY_STATUS = "status" | ||||
|         private const val PROPERTY_STATUS_OK = "ok" | ||||
|         private const val PROPERTY_STATUSCODE = "statuscode" | ||||
|         private const val PROPERTY_MESSAGE = "message" | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user