@@ -6,6 +6,7 @@ import com.onesignal.common.JSONUtils
6
6
import com.onesignal.common.OneSignalUtils
7
7
import com.onesignal.common.OneSignalWrapper
8
8
import com.onesignal.core.internal.config.ConfigModelStore
9
+ import com.onesignal.core.internal.device.IInstallIdService
9
10
import com.onesignal.core.internal.http.HttpResponse
10
11
import com.onesignal.core.internal.http.IHttpClient
11
12
import com.onesignal.core.internal.preferences.IPreferencesService
@@ -23,6 +24,7 @@ import kotlinx.coroutines.withTimeout
23
24
import org.json.JSONObject
24
25
import java.net.ConnectException
25
26
import java.net.HttpURLConnection
27
+ import java.net.URL
26
28
import java.net.UnknownHostException
27
29
import java.util.Scanner
28
30
import javax.net.ssl.HttpsURLConnection
@@ -32,6 +34,7 @@ internal class HttpClient(
32
34
private val _prefs : IPreferencesService ,
33
35
private val _configModelStore : ConfigModelStore ,
34
36
private val _time : ITime ,
37
+ private val _installIdService : IInstallIdService ,
35
38
) : IHttpClient {
36
39
/* *
37
40
* Delay making network requests until we reach this time.
@@ -149,6 +152,8 @@ internal class HttpClient(
149
152
con.setRequestProperty(" OneSignal-Subscription-Id" , subscriptionId)
150
153
}
151
154
155
+ con.setRequestProperty(" OneSignal-Install-Id" , _installIdService .getId().toString())
156
+
152
157
if (jsonBody != null ) {
153
158
con.doInput = true
154
159
}
@@ -159,16 +164,14 @@ internal class HttpClient(
159
164
con.doOutput = true
160
165
}
161
166
167
+ logHTTPSent(con.requestMethod, con.url, jsonBody, con.requestProperties)
168
+
162
169
if (jsonBody != null ) {
163
170
val strJsonBody = JSONUtils .toUnescapedEUIDString(jsonBody)
164
- Logging .debug(" HttpClient: ${method ? : " GET" } $url - $strJsonBody " )
165
-
166
171
val sendBytes = strJsonBody.toByteArray(charset(" UTF-8" ))
167
172
con.setFixedLengthStreamingMode(sendBytes.size)
168
173
val outputStream = con.outputStream
169
174
outputStream.write(sendBytes)
170
- } else {
171
- Logging .debug(" HttpClient: ${method ? : " GET" } $url " )
172
175
}
173
176
174
177
if (cacheKey != null ) {
@@ -194,7 +197,7 @@ internal class HttpClient(
194
197
PreferenceStores .ONESIGNAL ,
195
198
PreferenceOneSignalKeys .PREFS_OS_HTTP_CACHE_PREFIX + cacheKey,
196
199
)
197
- Logging .debug(" HttpClient: ${method ? : " GET" } $url - Using Cached response due to 304: " + cachedResponse)
200
+ Logging .debug(" HttpClient: Got Response = ${method ? : " GET" } ${con. url} - Using Cached response due to 304: " + cachedResponse)
198
201
199
202
// TODO: SHOULD RETURN OK INSTEAD OF NOT_MODIFIED TO MAKE TRANSPARENT?
200
203
retVal = HttpResponse (httpResponse, cachedResponse, retryAfterSeconds = retryAfter)
@@ -204,12 +207,12 @@ internal class HttpClient(
204
207
val scanner = Scanner (inputStream, " UTF-8" )
205
208
val json = if (scanner.useDelimiter(" \\ A" ).hasNext()) scanner.next() else " "
206
209
scanner.close()
207
- Logging .debug(" HttpClient: ${method ? : " GET" } $url - STATUS: $httpResponse JSON : " + json)
210
+ Logging .debug(" HttpClient: Got Response = ${method ? : " GET" } ${con. url} - STATUS: $httpResponse - Body : " + json)
208
211
209
212
if (cacheKey != null ) {
210
213
val eTag = con.getHeaderField(" etag" )
211
214
if (eTag != null ) {
212
- Logging .debug(" HttpClient: Response has etag of $eTag so caching the response." )
215
+ Logging .debug(" HttpClient: Got Response = Response has etag of $eTag so caching the response." )
213
216
214
217
_prefs .saveString(
215
218
PreferenceStores .ONESIGNAL ,
@@ -227,7 +230,7 @@ internal class HttpClient(
227
230
retVal = HttpResponse (httpResponse, json, retryAfterSeconds = retryAfter)
228
231
}
229
232
else -> {
230
- Logging .debug(" HttpClient: ${method ? : " GET" } $url - FAILED STATUS: $httpResponse " )
233
+ Logging .debug(" HttpClient: Got Response = ${method ? : " GET" } ${con. url} - FAILED STATUS: $httpResponse " )
231
234
232
235
var inputStream = con.errorStream
233
236
if (inputStream == null ) {
@@ -240,9 +243,9 @@ internal class HttpClient(
240
243
jsonResponse =
241
244
if (scanner.useDelimiter(" \\ A" ).hasNext()) scanner.next() else " "
242
245
scanner.close()
243
- Logging .warn(" HttpClient: $method RECEIVED JSON : $jsonResponse " )
246
+ Logging .warn(" HttpClient: Got Response = $method - STATUS: $httpResponse - Body : $jsonResponse " )
244
247
} else {
245
- Logging .warn(" HttpClient: $method HTTP Code : $httpResponse No response body!" )
248
+ Logging .warn(" HttpClient: Got Response = $method - STATUS : $httpResponse - No response body!" )
246
249
}
247
250
248
251
retVal = HttpResponse (httpResponse, jsonResponse, retryAfterSeconds = retryAfter)
@@ -285,6 +288,18 @@ internal class HttpClient(
285
288
}
286
289
}
287
290
291
+ private fun logHTTPSent (
292
+ method : String? ,
293
+ url : URL ,
294
+ jsonBody : JSONObject ? ,
295
+ headers : Map <String , List <String >>,
296
+ ) {
297
+ val headersStr = headers.entries.joinToString()
298
+ val methodStr = method ? : " GET"
299
+ val bodyStr = if (jsonBody != null ) JSONUtils .toUnescapedEUIDString(jsonBody) else null
300
+ Logging .debug(" HttpClient: Request Sent = $methodStr $url - Body: $bodyStr - Headers: $headersStr " )
301
+ }
302
+
288
303
companion object {
289
304
private const val OS_API_VERSION = " 1"
290
305
private const val OS_ACCEPT_HEADER = " application/vnd.onesignal.v$OS_API_VERSION +json"
0 commit comments