mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
Refactor GetRemoteUserInfoOperation
Start using Moshi to parse json responses. Create RemoteUserInfo and UserInfoResponse. Create UserService. Migrate GetRemoteUserInfoOperation to kotlin.
This commit is contained in:
parent
c5e64bd7ac
commit
99ced8bf41
@ -1,6 +1,7 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
ext {
|
ext {
|
||||||
kotlinVersion = '1.3.71'
|
kotlinVersion = '1.3.71'
|
||||||
|
moshiVersion = "1.9.2"
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
@ -9,6 +9,12 @@ dependencies {
|
|||||||
api 'com.gitlab.ownclouders:dav4android:oc_support_1.0.1'
|
api 'com.gitlab.ownclouders:dav4android:oc_support_1.0.1'
|
||||||
api 'com.github.hannesa2:Logcat:1.6.0'
|
api 'com.github.hannesa2:Logcat:1.6.0'
|
||||||
api 'net.openid:appauth:0.7.1'
|
api 'net.openid:appauth:0.7.1'
|
||||||
|
|
||||||
|
// Moshi
|
||||||
|
implementation ("com.squareup.moshi:moshi-kotlin:$moshiVersion") {
|
||||||
|
exclude module: "kotlin-reflect"
|
||||||
|
}
|
||||||
|
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion"
|
||||||
}
|
}
|
||||||
|
|
||||||
allOpen {
|
allOpen {
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class CommonOcsResponse<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?
|
||||||
|
)
|
@ -1,111 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* 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.users;
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants;
|
|
||||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod;
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets information (id, display name, and e-mail address) about the user logged in.
|
|
||||||
*
|
|
||||||
* @author masensio
|
|
||||||
* @author David A. Velasco
|
|
||||||
* @author David González Verdugo
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class GetRemoteUserInfoOperation extends RemoteOperation<GetRemoteUserInfoOperation.UserInfo> {
|
|
||||||
|
|
||||||
// OCS Route
|
|
||||||
private static final String OCS_ROUTE = "/ocs/v2.php/cloud/user?format=json";
|
|
||||||
|
|
||||||
// JSON Node names
|
|
||||||
private static final String NODE_OCS = "ocs";
|
|
||||||
private static final String NODE_DATA = "data";
|
|
||||||
private static final String NODE_ID = "id";
|
|
||||||
private static final String NODE_DISPLAY_NAME = "display-name";
|
|
||||||
private static final String NODE_EMAIL = "email";
|
|
||||||
|
|
||||||
public GetRemoteUserInfoOperation() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected RemoteOperationResult<UserInfo> run(OwnCloudClient client) {
|
|
||||||
RemoteOperationResult<UserInfo> result;
|
|
||||||
|
|
||||||
//Get the user
|
|
||||||
try {
|
|
||||||
GetMethod getMethod = new GetMethod(new URL(client.getBaseUri() + OCS_ROUTE));
|
|
||||||
|
|
||||||
int status = client.executeHttpMethod(getMethod);
|
|
||||||
|
|
||||||
if (isSuccess(status)) {
|
|
||||||
Timber.d("Successful response");
|
|
||||||
|
|
||||||
JSONObject respJSON = new JSONObject(getMethod.getResponseBodyAsString());
|
|
||||||
JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
|
|
||||||
JSONObject respData = respOCS.getJSONObject(NODE_DATA);
|
|
||||||
|
|
||||||
UserInfo userInfo = new UserInfo();
|
|
||||||
userInfo.mId = respData.getString(NODE_ID);
|
|
||||||
userInfo.mDisplayName = respData.getString(NODE_DISPLAY_NAME);
|
|
||||||
userInfo.mEmail = respData.getString(NODE_EMAIL);
|
|
||||||
|
|
||||||
result = new RemoteOperationResult<>(OK);
|
|
||||||
|
|
||||||
result.setData(userInfo);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
result = new RemoteOperationResult<>(getMethod);
|
|
||||||
String response = getMethod.getResponseBodyAsString();
|
|
||||||
Timber.e("Failed response while getting user information ");
|
|
||||||
Timber.e("*** status code: " + status + " ; response message: " + response);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
result = new RemoteOperationResult<>(e);
|
|
||||||
Timber.e(e, "Exception while getting OC user information");
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isSuccess(int status) {
|
|
||||||
return (status == HttpConstants.HTTP_OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class UserInfo {
|
|
||||||
public String mId = "";
|
|
||||||
public String mDisplayName = "";
|
|
||||||
public String mEmail = "";
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,81 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* 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.users
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient
|
||||||
|
import com.owncloud.android.lib.common.http.HttpConstants
|
||||||
|
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
|
||||||
|
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
|
||||||
|
import com.owncloud.android.lib.resources.CommonOcsResponse
|
||||||
|
import com.squareup.moshi.JsonAdapter
|
||||||
|
import com.squareup.moshi.Moshi
|
||||||
|
import com.squareup.moshi.Types
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.lang.reflect.Type
|
||||||
|
import java.net.URL
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets information (id, display name, and e-mail address) about the user logged in.
|
||||||
|
*
|
||||||
|
* @author masensio
|
||||||
|
* @author David A. Velasco
|
||||||
|
* @author David González Verdugo
|
||||||
|
* @author Abel García de Prada
|
||||||
|
*/
|
||||||
|
class GetRemoteUserInfoOperation : RemoteOperation<RemoteUserInfo>() {
|
||||||
|
override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteUserInfo> {
|
||||||
|
var result: RemoteOperationResult<RemoteUserInfo>
|
||||||
|
//Get the user
|
||||||
|
try {
|
||||||
|
val getMethod = GetMethod(URL(client.baseUri.toString() + OCS_ROUTE))
|
||||||
|
val status = client.executeHttpMethod(getMethod)
|
||||||
|
val response = getMethod.responseBodyAsString
|
||||||
|
if (status == HttpConstants.HTTP_OK) {
|
||||||
|
Timber.d("Successful response $response")
|
||||||
|
|
||||||
|
val moshi: Moshi = Moshi.Builder().build()
|
||||||
|
val type: Type = Types.newParameterizedType(CommonOcsResponse::class.java, UserInfoResponse::class.java)
|
||||||
|
val adapter: JsonAdapter<CommonOcsResponse<UserInfoResponse>> = moshi.adapter(type)
|
||||||
|
val commonResponse: CommonOcsResponse<UserInfoResponse>? = adapter.fromJson(response)
|
||||||
|
|
||||||
|
result = RemoteOperationResult(ResultCode.OK)
|
||||||
|
result.setData(commonResponse?.ocs?.data?.toRemoteUserInfo())
|
||||||
|
} else {
|
||||||
|
result = RemoteOperationResult(getMethod)
|
||||||
|
Timber.e("Failed response while getting user information status code: $status, response: $response")
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
result = RemoteOperationResult(e)
|
||||||
|
Timber.e(e, "Exception while getting OC user information")
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
// OCS Route
|
||||||
|
private const val OCS_ROUTE = "/ocs/v2.php/cloud/user?format=json"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
*
|
||||||
|
* 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.users
|
||||||
|
|
||||||
|
data class RemoteUserInfo(
|
||||||
|
val id: String,
|
||||||
|
val displayName: String,
|
||||||
|
val email: String?
|
||||||
|
)
|
@ -0,0 +1,42 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
*
|
||||||
|
* 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.users
|
||||||
|
|
||||||
|
import com.squareup.moshi.Json
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class UserInfoResponse(
|
||||||
|
val id: String,
|
||||||
|
@Json(name = "display-name")
|
||||||
|
val displayName: String,
|
||||||
|
val email: String?
|
||||||
|
)
|
||||||
|
|
||||||
|
fun UserInfoResponse.toRemoteUserInfo() =
|
||||||
|
RemoteUserInfo(
|
||||||
|
id = id,
|
||||||
|
displayName = displayName,
|
||||||
|
email = email
|
||||||
|
)
|
@ -0,0 +1,32 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* 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.users
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
||||||
|
import com.owncloud.android.lib.resources.Service
|
||||||
|
|
||||||
|
interface UserService: Service {
|
||||||
|
fun getUserInfo() : RemoteOperationResult<RemoteUserInfo>
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user