Skip to content

Commit e8c2fa1

Browse files
author
Rodrigo Gomez Palacio
committed
Update HTTP Client to take OptionalHeaders object
Motivation: we want to avoid adding lots of new method arguments. Better to encapsulate in a single object
1 parent af1c1bd commit e8c2fa1

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/http/IHttpClient.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.onesignal.core.internal.http
22

3+
import com.onesignal.core.internal.http.impl.OptionalHeaderValues
34
import org.json.JSONObject
45

56
/**
@@ -18,6 +19,7 @@ interface IHttpClient {
1819
suspend fun post(
1920
url: String,
2021
body: JSONObject,
22+
headerValues: OptionalHeaderValues? = null,
2123
): HttpResponse
2224

2325
/**
@@ -33,7 +35,7 @@ interface IHttpClient {
3335
*/
3436
suspend fun get(
3537
url: String,
36-
cacheKey: String? = null,
38+
headerValues: OptionalHeaderValues? = null,
3739
): HttpResponse
3840

3941
/**
@@ -47,6 +49,7 @@ interface IHttpClient {
4749
suspend fun put(
4850
url: String,
4951
body: JSONObject,
52+
headerValues: OptionalHeaderValues? = null,
5053
): HttpResponse
5154

5255
/**
@@ -60,6 +63,7 @@ interface IHttpClient {
6063
suspend fun patch(
6164
url: String,
6265
body: JSONObject,
66+
headerValues: OptionalHeaderValues? = null,
6367
): HttpResponse
6468

6569
/**
@@ -69,7 +73,10 @@ interface IHttpClient {
6973
*
7074
* @return The response returned.
7175
*/
72-
suspend fun delete(url: String): HttpResponse
76+
suspend fun delete(
77+
url: String,
78+
headerValues: OptionalHeaderValues? = null,
79+
): HttpResponse
7380
}
7481

7582
/**

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/http/impl/HttpClient.kt

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,41 +45,37 @@ internal class HttpClient(
4545
override suspend fun post(
4646
url: String,
4747
body: JSONObject,
48-
): HttpResponse {
49-
return makeRequest(url, "POST", body, _configModelStore.model.httpTimeout, null)
50-
}
48+
headerValues: OptionalHeaderValues?,
49+
): HttpResponse = makeRequest(url, "POST", body, _configModelStore.model.httpTimeout, headerValues)
5150

5251
override suspend fun get(
5352
url: String,
54-
cacheKey: String?,
55-
): HttpResponse {
56-
return makeRequest(url, null, null, _configModelStore.model.httpGetTimeout, cacheKey)
57-
}
53+
headerValues: OptionalHeaderValues?,
54+
): HttpResponse = makeRequest(url, null, null, _configModelStore.model.httpGetTimeout, headerValues)
5855

5956
override suspend fun put(
6057
url: String,
6158
body: JSONObject,
62-
): HttpResponse {
63-
return makeRequest(url, "PUT", body, _configModelStore.model.httpTimeout, null)
64-
}
59+
headerValues: OptionalHeaderValues?,
60+
): HttpResponse = makeRequest(url, "PUT", body, _configModelStore.model.httpTimeout, headerValues)
6561

6662
override suspend fun patch(
6763
url: String,
6864
body: JSONObject,
69-
): HttpResponse {
70-
return makeRequest(url, "PATCH", body, _configModelStore.model.httpTimeout, null)
71-
}
65+
headerValues: OptionalHeaderValues?,
66+
): HttpResponse = makeRequest(url, "PATCH", body, _configModelStore.model.httpTimeout, headerValues)
7267

73-
override suspend fun delete(url: String): HttpResponse {
74-
return makeRequest(url, "DELETE", null, _configModelStore.model.httpTimeout, null)
75-
}
68+
override suspend fun delete(
69+
url: String,
70+
headerValues: OptionalHeaderValues?,
71+
): HttpResponse = makeRequest(url, "DELETE", null, _configModelStore.model.httpTimeout, headerValues)
7672

7773
private suspend fun makeRequest(
7874
url: String,
7975
method: String?,
8076
jsonBody: JSONObject?,
8177
timeout: Int,
82-
cacheKey: String?,
78+
headerValues: OptionalHeaderValues?,
8379
): HttpResponse {
8480
// If privacy consent is required but not yet given, any non-GET request should be blocked.
8581
if (method != null && _configModelStore.model.consentRequired == true && _configModelStore.model.consentGiven != true) {
@@ -94,7 +90,7 @@ internal class HttpClient(
9490

9591
try {
9692
return withTimeout(getThreadTimeout(timeout).toLong()) {
97-
return@withTimeout makeRequestIODispatcher(url, method, jsonBody, timeout, cacheKey)
93+
return@withTimeout makeRequestIODispatcher(url, method, jsonBody, timeout, headerValues)
9894
}
9995
} catch (e: TimeoutCancellationException) {
10096
Logging.error("HttpClient: Request timed out: $url", e)
@@ -110,7 +106,7 @@ internal class HttpClient(
110106
method: String?,
111107
jsonBody: JSONObject?,
112108
timeout: Int,
113-
cacheKey: String?,
109+
headerValues: OptionalHeaderValues?,
114110
): HttpResponse {
115111
var retVal: HttpResponse? = null
116112

@@ -174,11 +170,16 @@ internal class HttpClient(
174170
outputStream.write(sendBytes)
175171
}
176172

177-
if (cacheKey != null) {
178-
val eTag = _prefs.getString(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.PREFS_OS_ETAG_PREFIX + cacheKey)
173+
// H E A D E R S
179174

175+
if (headerValues?.cacheKey != null) {
176+
val eTag =
177+
_prefs.getString(
178+
PreferenceStores.ONESIGNAL,
179+
PreferenceOneSignalKeys.PREFS_OS_ETAG_PREFIX + headerValues.cacheKey,
180+
)
180181
if (eTag != null) {
181-
con.setRequestProperty("if-none-match", eTag)
182+
con.setRequestProperty("If-None-Match", eTag)
182183
Logging.debug("HttpClient: Adding header if-none-match: $eTag")
183184
}
184185
}
@@ -195,9 +196,12 @@ internal class HttpClient(
195196
val cachedResponse =
196197
_prefs.getString(
197198
PreferenceStores.ONESIGNAL,
198-
PreferenceOneSignalKeys.PREFS_OS_HTTP_CACHE_PREFIX + cacheKey,
199+
PreferenceOneSignalKeys.PREFS_OS_HTTP_CACHE_PREFIX + headerValues?.cacheKey,
199200
)
200-
Logging.debug("HttpClient: Got Response = ${method ?: "GET"} ${con.url} - Using Cached response due to 304: " + cachedResponse)
201+
Logging.debug(
202+
"HttpClient: Got Response = ${method ?: "GET"} ${con.url} - Using Cached response due to 304: " +
203+
cachedResponse,
204+
)
201205

202206
// TODO: SHOULD RETURN OK INSTEAD OF NOT_MODIFIED TO MAKE TRANSPARENT?
203207
retVal = HttpResponse(httpResponse, cachedResponse, retryAfterSeconds = retryAfter)
@@ -207,21 +211,23 @@ internal class HttpClient(
207211
val scanner = Scanner(inputStream, "UTF-8")
208212
val json = if (scanner.useDelimiter("\\A").hasNext()) scanner.next() else ""
209213
scanner.close()
210-
Logging.debug("HttpClient: Got Response = ${method ?: "GET"} ${con.url} - STATUS: $httpResponse - Body: " + json)
214+
Logging.debug(
215+
"HttpClient: Got Response = ${method ?: "GET"} ${con.url} - STATUS: $httpResponse - Body: " + json,
216+
)
211217

212-
if (cacheKey != null) {
218+
if (headerValues?.cacheKey != null) {
213219
val eTag = con.getHeaderField("etag")
214220
if (eTag != null) {
215221
Logging.debug("HttpClient: Got Response = Response has etag of $eTag so caching the response.")
216222

217223
_prefs.saveString(
218224
PreferenceStores.ONESIGNAL,
219-
PreferenceOneSignalKeys.PREFS_OS_ETAG_PREFIX + cacheKey,
225+
PreferenceOneSignalKeys.PREFS_OS_ETAG_PREFIX + headerValues.cacheKey,
220226
eTag,
221227
)
222228
_prefs.saveString(
223229
PreferenceStores.ONESIGNAL,
224-
PreferenceOneSignalKeys.PREFS_OS_HTTP_CACHE_PREFIX + cacheKey,
230+
PreferenceOneSignalKeys.PREFS_OS_HTTP_CACHE_PREFIX + headerValues.cacheKey,
225231
json,
226232
)
227233
}

0 commit comments

Comments
 (0)