Skip to content

Commit 58b6a3f

Browse files
authored
Merge pull request #16 from haroldadmin/development
Add response code to NetworkResponse.Success
2 parents 9a4d382 + 09522b1 commit 58b6a3f

File tree

7 files changed

+19
-5
lines changed

7 files changed

+19
-5
lines changed

src/main/kotlin/com/haroldadmin/cnradapter/CoroutinesNetworkResponseAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ internal class CoroutinesNetworkResponseAdapter<T : Any, U : Any>(
9494
val responseCode = response.code()
9595
val body = response.body()
9696
body?.let {
97-
deferred.complete(NetworkResponse.Success(it, headers))
97+
deferred.complete(NetworkResponse.Success(it, headers, responseCode))
9898
} ?: deferred.complete(NetworkResponse.ServerError(null, responseCode, headers))
9999
}
100100
})

src/main/kotlin/com/haroldadmin/cnradapter/NetworkResponse.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ sealed class NetworkResponse<out T : Any, out U : Any> {
77
/**
88
* A request that resulted in a response with a 2xx status code that has a body.
99
*/
10-
data class Success<T : Any>(val body: T, val headers: Headers? = null) : NetworkResponse<T, Nothing>()
10+
data class Success<T : Any>(
11+
val body: T,
12+
val headers: Headers? = null,
13+
val code: Int
14+
) : NetworkResponse<T, Nothing>()
1115

1216
/**
1317
* A request that resulted in a response with a non-2xx status code.

src/main/kotlin/com/haroldadmin/cnradapter/ResponseHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ internal object ResponseHandler {
3636

3737
return if (response.isSuccessful) {
3838
if (body != null) {
39-
NetworkResponse.Success(body, headers)
39+
NetworkResponse.Success(body, headers, code)
4040
} else {
4141
// Special case: If the response is successful and the body is null, return a successful response
4242
// if the service method declares the success body type as Unit. Otherwise, return a server error
4343
if (successBodyType == Unit::class.java) {
4444
@Suppress("UNCHECKED_CAST")
45-
NetworkResponse.Success(Unit, headers) as NetworkResponse<S, E>
45+
NetworkResponse.Success(Unit, headers, code) as NetworkResponse<S, E>
4646
} else {
4747
NetworkResponse.ServerError(null, code, headers)
4848
}

src/test/kotlin/com/haroldadmin/cnradapter/DeferredTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ internal class DeferredTest : DescribeSpec() {
6060
body shouldBe responseBody
6161
headers shouldNotBe null
6262
headers!!.get("TEST") shouldBe "test"
63+
code shouldBe 200
6364
}
6465
}
6566
}

src/test/kotlin/com/haroldadmin/cnradapter/InvokeOperatorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import java.io.IOException
88

99
private class MessageRepo {
1010
fun getMessage(): Deferred<NetworkResponse<String, String>> {
11-
return CompletableDeferred(NetworkResponse.Success("Hello!"))
11+
return CompletableDeferred(NetworkResponse.Success("Hello!", null, 200))
1212
}
1313

1414
fun getMessageError(): Deferred<NetworkResponse<String, String>> {

src/test/kotlin/com/haroldadmin/cnradapter/MoshiApplicationTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ internal class MoshiApplicationTest: AnnotationSpec() {
119119
response.shouldBeInstanceOf<NetworkResponse.Success<Launch>>()
120120
response as NetworkResponse.Success
121121
response.body.name shouldContain "FalconSat"
122+
response.code shouldBe 200
122123
}
123124

124125
@Test
@@ -133,6 +134,7 @@ internal class MoshiApplicationTest: AnnotationSpec() {
133134
response.shouldBeInstanceOf<NetworkResponse.ServerError<GenericErrorResponse>>()
134135
response as NetworkResponse.ServerError
135136
response.body!!.error shouldContain "Not Found"
137+
response.code shouldBe 404
136138
}
137139

138140
@Test
@@ -174,6 +176,7 @@ internal class MoshiApplicationTest: AnnotationSpec() {
174176
response.shouldBeInstanceOf<NetworkResponse.Success<Launch>>()
175177
response as NetworkResponse.Success
176178
response.body.name shouldContain "FalconSat"
179+
response.code shouldBe 200
177180
}
178181

179182
@Test
@@ -188,6 +191,7 @@ internal class MoshiApplicationTest: AnnotationSpec() {
188191
response.shouldBeInstanceOf<NetworkResponse.ServerError<GenericErrorResponse>>()
189192
response as NetworkResponse.ServerError
190193
response.body!!.error shouldContain "Not Found"
194+
response.code shouldBe 404
191195
}
192196

193197
@Test
@@ -229,6 +233,7 @@ internal class MoshiApplicationTest: AnnotationSpec() {
229233
response.shouldBeInstanceOf<NetworkResponse.Success<Unit>>()
230234
response as NetworkResponse.Success
231235
response.body shouldBe Unit
236+
response.code shouldBe 204
232237
}
233238

234239
@Test
@@ -242,5 +247,6 @@ internal class MoshiApplicationTest: AnnotationSpec() {
242247
response.shouldBeInstanceOf<NetworkResponse.Success<Unit>>()
243248
response as NetworkResponse.Success
244249
response.body shouldBe Unit
250+
response.code shouldBe 204
245251
}
246252
}

src/test/kotlin/com/haroldadmin/cnradapter/SuspendTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ internal class SuspendTest: AnnotationSpec() {
5252
body shouldBe responseBody
5353
headers shouldNotBe null
5454
headers!!["TEST"] shouldBe "test"
55+
code shouldBe 200
5556
}
5657
}
5758

@@ -94,6 +95,7 @@ internal class SuspendTest: AnnotationSpec() {
9495
shouldBeTypeOf<NetworkResponse.Success<Unit>>()
9596
this as NetworkResponse.Success
9697
body shouldBe Unit
98+
code shouldBe 204
9799
}
98100
}
99101

@@ -110,6 +112,7 @@ internal class SuspendTest: AnnotationSpec() {
110112
shouldBeTypeOf<NetworkResponse.ServerError<String>>()
111113
this as NetworkResponse.ServerError
112114
body shouldBe ""
115+
code shouldBe 400
113116
}
114117
}
115118

0 commit comments

Comments
 (0)