-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[17415] Add config to pass custom base url when initializing TripKit …
…to be used on api calls instead of tripgo/region base url [17415] Add config to pass custom headers when initializing TripKit - Fix TripKitSample
- Loading branch information
1 parent
cd44a6f
commit b4f476d
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
TripKitAndroid/src/main/java/com/skedgo/tripkit/data/HttpClientCustomDataStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.skedgo.tripkit.data | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import com.google.gson.Gson | ||
|
||
@SuppressLint("StaticFieldLeak") | ||
object HttpClientCustomDataStore { | ||
|
||
private val gson = Gson() | ||
private var context: Context? = null | ||
private var sharedPreferences: SharedPreferences? = null | ||
private const val KEY_CUSTOM_HEADERS = "_custom_headers" | ||
private const val KEY_CUSTOM_BASE_URL = "_custom_base_url" | ||
|
||
fun init(context: Context) { | ||
this.context = context | ||
sharedPreferences = context.getSharedPreferences("HttpClientSharedPref", 0) | ||
} | ||
|
||
fun setCustomHeaders(headers: Map<String, String>) { | ||
sharedPreferences?.apply { | ||
edit().putString(KEY_CUSTOM_HEADERS, gson.toJson(headers)).apply() | ||
} | ||
} | ||
|
||
fun getCustomHeadersString(): String? = sharedPreferences?.getString(KEY_CUSTOM_HEADERS, null) | ||
|
||
fun setCustomBaseUrl(url: String) { | ||
sharedPreferences?.apply { | ||
edit().putString(KEY_CUSTOM_BASE_URL, url).apply() | ||
} | ||
} | ||
|
||
fun getCustomBaseUrl(): String? = sharedPreferences?.getString(KEY_CUSTOM_BASE_URL, null) | ||
} |