@@ -19,6 +19,7 @@ package com.google.firebase.sessions.settings
19
19
import android.net.Uri
20
20
import com.google.firebase.sessions.ApplicationInfo
21
21
import java.io.BufferedReader
22
+ import java.io.IOException
22
23
import java.io.InputStreamReader
23
24
import java.net.URL
24
25
import javax.net.ssl.HttpsURLConnection
@@ -27,43 +28,48 @@ import org.json.JSONObject
27
28
internal interface CrashlyticsSettingsFetcher {
28
29
suspend fun doConfigFetch (
29
30
headerOptions : Map <String , String >,
30
- onSuccess : suspend (( JSONObject ) ) -> Unit ,
31
- onFailure : suspend () -> Unit
31
+ onSuccess : suspend (JSONObject ) -> Unit ,
32
+ onFailure : suspend (msg: String ) -> Unit
32
33
)
33
34
}
34
35
35
- internal class RemoteSettingsFetcher (val appInfo : ApplicationInfo ) : CrashlyticsSettingsFetcher {
36
+ internal class RemoteSettingsFetcher (private val appInfo : ApplicationInfo ) :
37
+ CrashlyticsSettingsFetcher {
36
38
override suspend fun doConfigFetch (
37
39
headerOptions : Map <String , String >,
38
- onSuccess : suspend (( JSONObject ) ) -> Unit ,
39
- onFailure : suspend () -> Unit
40
+ onSuccess : suspend (JSONObject ) -> Unit ,
41
+ onFailure : suspend (String ) -> Unit
40
42
) {
41
43
val connection = settingsUrl().openConnection() as HttpsURLConnection
42
44
connection.requestMethod = " GET"
43
45
connection.setRequestProperty(" Accept" , " application/json" )
44
46
headerOptions.forEach { connection.setRequestProperty(it.key, it.value) }
45
47
46
- val responseCode = connection.responseCode
47
- if (responseCode == HttpsURLConnection .HTTP_OK ) {
48
- val inputStream = connection.inputStream
49
- val bufferedReader = BufferedReader (InputStreamReader (inputStream))
50
- val response = StringBuilder ()
51
- var inputLine: String?
52
- while (bufferedReader.readLine().also { inputLine = it } != null ) {
53
- response.append(inputLine)
54
- }
55
- bufferedReader.close()
56
- inputStream.close()
48
+ try {
49
+ val responseCode = connection.responseCode
50
+ if (responseCode == HttpsURLConnection .HTTP_OK ) {
51
+ val inputStream = connection.inputStream
52
+ val bufferedReader = BufferedReader (InputStreamReader (inputStream))
53
+ val response = StringBuilder ()
54
+ var inputLine: String?
55
+ while (bufferedReader.readLine().also { inputLine = it } != null ) {
56
+ response.append(inputLine)
57
+ }
58
+ bufferedReader.close()
59
+ inputStream.close()
57
60
58
- val responseJson = JSONObject (response.toString())
59
- onSuccess(responseJson)
60
- } else {
61
- onFailure()
61
+ val responseJson = JSONObject (response.toString())
62
+ onSuccess(responseJson)
63
+ } else {
64
+ onFailure(" Bad response code: $responseCode " )
65
+ }
66
+ } catch (ex: IOException ) {
67
+ onFailure(ex.message ? : ex.toString())
62
68
}
63
69
}
64
70
65
- fun settingsUrl (): URL {
66
- var uri =
71
+ private fun settingsUrl (): URL {
72
+ val uri =
67
73
Uri .Builder ()
68
74
.scheme(" https" )
69
75
.authority(FIREBASE_SESSIONS_BASE_URL_STRING )
0 commit comments