-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommonRemoteDataSource.kt
100 lines (88 loc) · 3.42 KB
/
CommonRemoteDataSource.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.wordline.helios.rewarding.sdk.data.datasource.remote
import com.wordline.helios.rewarding.sdk.data.datasource.local.LocalDataSource
import com.wordline.helios.rewarding.sdk.domain.model.Activity
import com.wordline.helios.rewarding.sdk.domain.model.Either
import com.wordline.helios.rewarding.sdk.domain.model.Error
import com.wordline.helios.rewarding.sdk.domain.model.Success
import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Android
import io.ktor.client.features.ClientRequestException
import io.ktor.client.features.json.GsonSerializer
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.features.logging.LogLevel
import io.ktor.client.features.logging.Logger
import io.ktor.client.features.logging.Logging
import io.ktor.client.features.logging.SIMPLE
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.post
import io.ktor.http.HttpStatusCode
import io.ktor.http.takeFrom
class CommonRemoteDataSource(localDataSource: LocalDataSource) : RemoteDataSource {
companion object {
const val END_POINT_HELIOS = "https://devel3.tempos21.com"
private const val TOKEN_HEADER = "Authorization"
}
private val client = HttpClient(Android) {
// install(Auth) {
// basic {
// username = AUTH_USER
// password = AUTH_PASS
// }
// }
install(Logging) {
logger = Logger.SIMPLE
level = LogLevel.ALL
}
install(JsonFeature) {
serializer = GsonSerializer()
}
install(TokenFeature) {
tokenHeaderName = TOKEN_HEADER
tokenProvider = localDataSource
}
}
override suspend fun registerUser(
userID: String,
context: String
): Either<Error, RegisterDataResponse> = execute {
client.post<RegisterDataResponseDto> {
call("/hrm-api/auth/networkUser/register")
val json = io.ktor.client.features.json.defaultSerializer()
body = json.write(RegisterData(userID = userID, context = context))
}.toModel()
}
override suspend fun registerActivity(action: String, date: String): Either<Error, Success> =
execute {
client.post<String> {
call("/hrm-api/activities/record")
val json = io.ktor.client.features.json.defaultSerializer()
body = json.write(listOf(Activity(action = action, date = date)))
}.toSuccess()
}
private suspend fun <R> execute(f: suspend () -> R): Either<Error, R> =
try {
Either.Right(f())
} catch (e: Throwable) {
print(e.toString())
Either.Left(
when (e) {
is ClientRequestException -> when (e.response.status) {
HttpStatusCode.Unauthorized -> Error.InvalidCredentials
HttpStatusCode.NotFound -> Error.NotFound
HttpStatusCode.BadRequest -> Error.NoInternet
else -> Error.Default(e.message ?: "")
}
else -> Error.Default(e.message ?: "")
}
)
}
private fun HttpRequestBuilder.call(path: String) {
url {
takeFrom(END_POINT_HELIOS)
encodedPath = "$path"
}
}
private fun String.toSuccess(): Success {
return Success
}
}