Skip to content

Commit bef6794

Browse files
refactor: Apply suggestion from code review
1 parent f239be4 commit bef6794

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

Network/Models/src/main/java/com/infomaniak/core/network/models/ApiResponse.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ open class ApiResponse<T>(
4949
val itemsPerPage: Int = 0,
5050
) {
5151
@Transient
52-
var headers: InfomaniakHeaders? = null
52+
var headers: ResponseHeaders? = null
5353

5454
fun isSuccess() = result == ApiResponseStatus.SUCCESS
5555
}

Network/Models/src/main/java/com/infomaniak/core/network/models/InfomaniakHeaders.kt

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Infomaniak Core - Android
3+
* Copyright (C) 2025 Infomaniak Network SA
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see http://www.gnu.org/licenses/.
17+
*/
18+
package com.infomaniak.core.network.models
19+
20+
import okhttp3.Headers
21+
import kotlin.reflect.KProperty
22+
23+
class ResponseHeaders(headers: Headers) {
24+
private val headersBacking = HeadersBacking(headers)
25+
26+
val lastModified: String? by headersBacking.header("last-modified")
27+
}
28+
29+
private class HeadersBacking(val remoteHeaders: Headers) {
30+
fun header(key: String): HeaderData {
31+
return object : HeaderData {
32+
override fun getValue(thisRef: Any?, property: KProperty<*>): String? {
33+
return remoteHeaders[key]
34+
}
35+
}
36+
}
37+
}
38+
39+
private interface HeaderData {
40+
operator fun getValue(thisRef: Any?, property: KProperty<*>): String?
41+
}

Network/src/main/kotlin/com/infomaniak/core/network/api/ApiController.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import com.infomaniak.core.network.isSerializationException
2626
import com.infomaniak.core.network.models.ApiError
2727
import com.infomaniak.core.network.models.ApiResponse
2828
import com.infomaniak.core.network.models.ApiResponseStatus
29-
import com.infomaniak.core.network.models.InfomaniakHeaders
29+
import com.infomaniak.core.network.models.ResponseHeaders
3030
import com.infomaniak.core.network.models.exceptions.NetworkException
3131
import com.infomaniak.core.network.models.exceptions.ServerErrorException
3232
import com.infomaniak.core.network.networking.HttpClient
@@ -187,7 +187,7 @@ object ApiController {
187187
@Suppress("DEPRECATION")
188188
apiResponse.translatedError = InternalTranslatedErrorCode.UnknownError.translateRes
189189
}
190-
if (apiResponse is ApiResponse<*>) apiResponse.headers = InfomaniakHeaders(headers)
190+
if (apiResponse is ApiResponse<*>) apiResponse.headers = ResponseHeaders(headers)
191191

192192
return apiResponse
193193
}

0 commit comments

Comments
 (0)