@@ -4,6 +4,7 @@ import com.harmony.kotlin.data.datasource.DeleteDataSource
4
4
import com.harmony.kotlin.data.datasource.GetDataSource
5
5
import com.harmony.kotlin.data.datasource.PutDataSource
6
6
import com.harmony.kotlin.data.error.QueryNotSupportedException
7
+ import com.harmony.kotlin.data.mapper.IdentityMapper
7
8
import com.harmony.kotlin.data.mapper.Mapper
8
9
import com.harmony.kotlin.data.query.Query
9
10
import io.ktor.client.HttpClient
@@ -12,23 +13,21 @@ import kotlinx.serialization.builtins.ListSerializer
12
13
import kotlinx.serialization.builtins.serializer
13
14
import kotlinx.serialization.json.Json
14
15
15
- open class GetNetworkDataSource <T >(
16
+ class GetNetworkDataSource <T >(
16
17
private val url : String ,
17
18
private val httpClient : HttpClient ,
18
19
private val serializer : KSerializer <T >,
19
20
private val json : Json ,
20
21
private val globalHeaders : List <Pair <String , String >> = emptyList(),
21
- private val exceptionMapper : Mapper <Exception , Exception > = GenericNetworkExceptionMapper ()
22
+ private val exceptionMapper : Mapper <Exception , Exception > = IdentityMapper ()
22
23
) : GetDataSource<T> {
23
24
24
25
/* *
25
26
* GET request returning an object
26
27
*/
27
28
override suspend fun get (query : Query ): T {
28
- val response: String = try {
29
+ val response: String = tryOrThrow(exceptionMapper) {
29
30
executeGetRequest(query)
30
- } catch (e: Exception ) {
31
- throw exceptionMapper.map(e)
32
31
}
33
32
return json.decodeFromString(serializer, response)
34
33
@@ -38,11 +37,10 @@ open class GetNetworkDataSource<T>(
38
37
* GET request returning a list of objects
39
38
*/
40
39
override suspend fun getAll (query : Query ): List <T > {
41
- val response: String = try {
40
+ val response: String = tryOrThrow(exceptionMapper) {
42
41
executeGetRequest(query)
43
- } catch (e: Exception ) {
44
- throw exceptionMapper.map(e)
45
42
}
43
+
46
44
return json.decodeFromString(ListSerializer (serializer), response)
47
45
48
46
}
@@ -63,26 +61,25 @@ open class GetNetworkDataSource<T>(
63
61
}
64
62
}
65
63
66
- open class PutNetworkDataSource <T >(
64
+ class PutNetworkDataSource <T >(
67
65
private val url : String ,
68
66
private val httpClient : HttpClient ,
69
67
private val serializer : KSerializer <T >,
70
68
private val json : Json ,
71
69
private val globalHeaders : List <Pair <String , String >> = emptyList(),
72
- private val exceptionMapper : Mapper <Exception , Exception > = GenericNetworkExceptionMapper ()
70
+ private val exceptionMapper : Mapper <Exception , Exception > = IdentityMapper ()
71
+
73
72
) : PutDataSource<T> {
74
73
75
74
/* *
76
75
* POST or PUT request returning an object
77
76
* @throws IllegalArgumentException if both value and content-type of the query method are defined
78
77
*/
79
78
override suspend fun put (query : Query , value : T ? ): T {
80
- val response: String = try {
79
+ val response: String = tryOrThrow(exceptionMapper) {
81
80
validateQuery(query)
82
81
.sanitizeContentType(value)
83
82
.executeKtorRequest(httpClient = httpClient, baseUrl = url, globalHeaders = globalHeaders)
84
- } catch (e: Exception ) {
85
- throw exceptionMapper.map(e)
86
83
}
87
84
88
85
return if (serializer.descriptor != Unit .serializer().descriptor) {
@@ -97,14 +94,11 @@ open class PutNetworkDataSource<T>(
97
94
* @throws IllegalArgumentException if both value and content-type of the query method are defined
98
95
*/
99
96
override suspend fun putAll (query : Query , value : List <T >? ): List <T > {
100
- val response: String = try {
97
+ val response: String = tryOrThrow(exceptionMapper) {
101
98
validateQuery(query)
102
99
.sanitizeContentType(value)
103
100
.executeKtorRequest(httpClient = httpClient, baseUrl = url, globalHeaders = globalHeaders)
104
- } catch (e: Exception ) {
105
- throw exceptionMapper.map(e)
106
101
}
107
-
108
102
return if (serializer.descriptor != Unit .serializer().descriptor) {
109
103
json.decodeFromString(ListSerializer (serializer), response)
110
104
} else { // If Unit.serializer() is used is because we want to ignore the response and just return an empty list of Unit
@@ -153,17 +147,15 @@ class DeleteNetworkDataSource(
153
147
private val url : String ,
154
148
private val httpClient : HttpClient ,
155
149
private val globalHeaders : List <Pair <String , String >> = emptyList(),
156
- private val exceptionMapper : Mapper <Exception , Exception > = GenericNetworkExceptionMapper ()
150
+ private val exceptionMapper : Mapper <Exception , Exception > = IdentityMapper ()
157
151
) : DeleteDataSource {
158
152
159
153
/* *
160
154
* DELETE request
161
155
*/
162
156
override suspend fun delete (query : Query ) {
163
- try {
157
+ tryOrThrow(exceptionMapper) {
164
158
validateQuery(query).executeKtorRequest(httpClient = httpClient, baseUrl = url, globalHeaders = globalHeaders)
165
- } catch (e: Exception ) {
166
- throw exceptionMapper.map(e)
167
159
}
168
160
}
169
161
@@ -179,3 +171,11 @@ class DeleteNetworkDataSource(
179
171
return query
180
172
}
181
173
}
174
+
175
+ private suspend fun <T > tryOrThrow (exceptionMapper : Mapper <Exception , Exception >, block : suspend () -> T ): T {
176
+ return try {
177
+ block()
178
+ } catch (e: Exception ) {
179
+ throw exceptionMapper.map(e)
180
+ }
181
+ }
0 commit comments