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

Commit b1a3402

Browse files
abelgardepJuancaG05
authored andcommitted
Create spaces fetch operation and spaces service
1 parent f0dda9e commit b1a3402

File tree

4 files changed

+272
-0
lines changed

4 files changed

+272
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2022 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.owncloud.android.lib.resources.spaces
24+
25+
import android.net.Uri
26+
import com.owncloud.android.lib.common.OwnCloudClient
27+
import com.owncloud.android.lib.common.http.HttpConstants
28+
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
29+
import com.owncloud.android.lib.common.operations.RemoteOperation
30+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
31+
import com.owncloud.android.lib.resources.CommonOcsResponse
32+
import com.owncloud.android.lib.resources.shares.responses.ShareeOcsResponse
33+
import com.owncloud.android.lib.resources.spaces.responses.SpaceResponse
34+
import com.owncloud.android.lib.resources.spaces.responses.SpacesResponseWrapper
35+
import com.squareup.moshi.JsonAdapter
36+
import com.squareup.moshi.Moshi
37+
import com.squareup.moshi.Types
38+
import timber.log.Timber
39+
import java.lang.reflect.Type
40+
import java.net.URL
41+
42+
class GetRemoteSpacesOperation : RemoteOperation<List<SpaceResponse>>() {
43+
override fun run(client: OwnCloudClient): RemoteOperationResult<List<SpaceResponse>> {
44+
val requestUri = buildRequestUri(client.baseUri)
45+
46+
val getMethod = GetMethod(URL(requestUri.toString()))
47+
48+
return try {
49+
val status = client.executeHttpMethod(getMethod)
50+
val response = getMethod.getResponseBodyAsString()
51+
52+
if (isSuccess(status)) {
53+
onRequestSuccessful(response)
54+
} else {
55+
onResultUnsuccessful(getMethod, response, status)
56+
}
57+
} catch (e: Exception) {
58+
Timber.e(e, "Exception while getting remote shares")
59+
RemoteOperationResult(e)
60+
}
61+
}
62+
63+
private fun buildRequestUri(baseUri: Uri) =
64+
baseUri.buildUpon()
65+
.appendEncodedPath(GRAPH_API_PATH)
66+
.appendEncodedPath(ENDPOINT_SPACES_LIST)
67+
.build()
68+
69+
private fun parseResponse(response: String): List<SpaceResponse> {
70+
val moshi = Moshi.Builder().build()
71+
val adapter: JsonAdapter<SpacesResponseWrapper> = moshi.adapter(SpacesResponseWrapper::class.java)
72+
return adapter.fromJson(response)?.value ?: listOf()
73+
}
74+
75+
private fun onResultUnsuccessful(
76+
method: GetMethod,
77+
response: String?,
78+
status: Int
79+
): RemoteOperationResult<List<SpaceResponse>> {
80+
Timber.e("Failed response while getting spaces for user")
81+
if (response != null) {
82+
Timber.e("*** status code: $status; response message: $response")
83+
} else {
84+
Timber.e("*** status code: $status")
85+
}
86+
return RemoteOperationResult(method)
87+
}
88+
89+
private fun onRequestSuccessful(response: String?): RemoteOperationResult<List<SpaceResponse>> {
90+
val result = RemoteOperationResult<List<SpaceResponse>>(RemoteOperationResult.ResultCode.OK)
91+
Timber.d("Successful response: $response")
92+
result.data = response?.let { parseResponse(it) } ?: listOf()
93+
Timber.d("*** Fetch of spaces completed and parsed to ${result.data}")
94+
return result
95+
}
96+
97+
private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK
98+
99+
companion object {
100+
private const val GRAPH_API_PATH = "graph/v1.0"
101+
private const val ENDPOINT_SPACES_LIST = "me/drives"
102+
}
103+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2022 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.owncloud.android.lib.resources.spaces.responses
24+
25+
import com.squareup.moshi.JsonClass
26+
27+
@JsonClass(generateAdapter = true)
28+
data class SpacesResponseWrapper(
29+
val value: List<SpaceResponse>
30+
)
31+
32+
@JsonClass(generateAdapter = true)
33+
data class SpaceResponse(
34+
val description: String?,
35+
val driveAlias: String,
36+
val driveType: String,
37+
val id: String,
38+
val lastModifiedDateTime: String,
39+
val name: String,
40+
val owner: OwnerResponse,
41+
val quota: QuotaResponse,
42+
val root: RootResponse,
43+
val special: List<SpecialResponse>?,
44+
val webUrl: String
45+
)
46+
47+
@JsonClass(generateAdapter = true)
48+
data class OwnerResponse(
49+
val user: UserResponse
50+
)
51+
52+
@JsonClass(generateAdapter = true)
53+
data class QuotaResponse(
54+
val remaining: Long,
55+
val state: String,
56+
val total: Int,
57+
val used: Int
58+
)
59+
60+
@JsonClass(generateAdapter = true)
61+
data class RootResponse(
62+
val eTag: String,
63+
val id: String,
64+
val permissions: List<PermissionResponse>?,
65+
val webDavUrl: String
66+
)
67+
68+
@JsonClass(generateAdapter = true)
69+
data class SpecialResponse(
70+
val eTag: String,
71+
val file: FileResponse,
72+
val id: String,
73+
val lastModifiedDateTime: String,
74+
val name: String,
75+
val size: Int,
76+
val specialFolder: SpecialFolderResponse,
77+
val webDavUrl: String
78+
)
79+
80+
@JsonClass(generateAdapter = true)
81+
data class UserResponse(
82+
val id: String
83+
)
84+
85+
@JsonClass(generateAdapter = true)
86+
data class FileResponse(
87+
val mimeType: String
88+
)
89+
90+
@JsonClass(generateAdapter = true)
91+
data class GrantedToResponse(
92+
val user: UserResponse
93+
)
94+
95+
@JsonClass(generateAdapter = true)
96+
data class PermissionResponse(
97+
val grantedTo: List<GrantedToResponse>,
98+
val roles: List<String>
99+
)
100+
101+
@JsonClass(generateAdapter = true)
102+
data class SpecialFolderResponse(
103+
val name: String
104+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2022 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.owncloud.android.lib.resources.spaces.services
24+
25+
import com.owncloud.android.lib.common.OwnCloudClient
26+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
27+
import com.owncloud.android.lib.resources.spaces.GetRemoteSpacesOperation
28+
import com.owncloud.android.lib.resources.spaces.responses.SpaceResponse
29+
30+
class OCSpacesService(override val client: OwnCloudClient) : SpacesService {
31+
override fun getSpaces(): RemoteOperationResult<List<SpaceResponse>> {
32+
return GetRemoteSpacesOperation().execute(client)
33+
}
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2022 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.owncloud.android.lib.resources.spaces.services
24+
25+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
26+
import com.owncloud.android.lib.resources.Service
27+
import com.owncloud.android.lib.resources.spaces.responses.SpaceResponse
28+
29+
interface SpacesService : Service {
30+
fun getSpaces(): RemoteOperationResult<List<SpaceResponse>>
31+
}

0 commit comments

Comments
 (0)