1
1
package com.uid2.network
2
2
3
+ import java.io.IOException
3
4
import java.net.HttpURLConnection
4
5
import java.net.URL
5
6
@@ -16,35 +17,46 @@ public open class DefaultNetworkSession : NetworkSession {
16
17
* Loads the given [URL] and [NetworkRequest] using [HttpURLConnection].
17
18
*/
18
19
override fun loadData (url : URL , request : NetworkRequest ): NetworkResponse {
19
- val connection = openConnection(url).apply {
20
- requestMethod = request.type.toRequestMethod()
20
+ try {
21
+ val connection = openConnection(url).apply {
22
+ requestMethod = request.type.toRequestMethod()
21
23
22
- setRequestProperty(" Accept" , " application/json" )
24
+ setRequestProperty(" Accept" , " application/json" )
23
25
24
- // If we've been given any request headers, add them to the request.
25
- request.headers.forEach {
26
- addRequestProperty(it.key, it.value)
27
- }
26
+ // If we've been given any request headers, add them to the request.
27
+ request.headers.forEach {
28
+ addRequestProperty(it.key, it.value)
29
+ }
28
30
29
- // If data was provided, this will require us to write it to the output stream.
30
- request.data?.let { data ->
31
- doOutput = true
31
+ // If data was provided, this will require us to write it to the output stream.
32
+ request.data?.let { data ->
33
+ doOutput = true
32
34
33
- outputStream.use { outputStream ->
34
- outputStream.write(data.toByteArray(Charsets .UTF_8 ))
35
+ outputStream.use { outputStream ->
36
+ outputStream.write(data.toByteArray(Charsets .UTF_8 ))
37
+ }
35
38
}
36
39
}
37
- }
38
40
39
- // A successful response code should be in the [200-299] range.
40
- val responseCode = connection.responseCode
41
- if (responseCode !in SUCCESS_CODE_RANGE_MIN .. SUCCESS_CODE_RANGE_MAX ) {
42
- return NetworkResponse (responseCode)
43
- }
41
+ // A successful response code should be in the [200-299] range. If we receive something outside that range, then
42
+ // we should be reading from the errorStream rather than the standard inputStream.
43
+ val responseCode = connection.responseCode
44
+ val responseStream = if (NetworkSession .isSuccess(responseCode)) {
45
+ connection.inputStream
46
+ } else {
47
+ connection.errorStream
48
+ }
44
49
45
- // We expect the response to be a String.
46
- val responseText = connection.inputStream.bufferedReader().use { it.readText() }
47
- return NetworkResponse (responseCode, responseText)
50
+ // We expect the response to be a String.
51
+ val responseText = runCatching {
52
+ responseStream.bufferedReader().use { it.readText() }
53
+ }.getOrDefault(" " )
54
+ return NetworkResponse (responseCode, responseText)
55
+ } catch (ex: IOException ) {
56
+ // If we're unable to make a request, e.g. due to lack of connection, we will simply report an internal
57
+ // error.
58
+ return NetworkResponse (HttpURLConnection .HTTP_INTERNAL_ERROR )
59
+ }
48
60
}
49
61
50
62
/* *
@@ -62,9 +74,4 @@ public open class DefaultNetworkSession : NetworkSession {
62
74
NetworkRequestType .GET -> " GET"
63
75
NetworkRequestType .POST -> " POST"
64
76
}
65
-
66
- private companion object {
67
- const val SUCCESS_CODE_RANGE_MIN = HttpURLConnection .HTTP_OK
68
- const val SUCCESS_CODE_RANGE_MAX = 299
69
- }
70
77
}
0 commit comments