Skip to content

Commit

Permalink
Merge pull request #634 from Shopify/release-4.0.0
Browse files Browse the repository at this point in the history
Prepare for 4.0.0
  • Loading branch information
jmignac authored Jan 23, 2020
2 parents ed87718 + c942067 commit 31af98f
Show file tree
Hide file tree
Showing 12 changed files with 3,385 additions and 392 deletions.
2 changes: 1 addition & 1 deletion MobileBuy/buy3/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=3.6.0
VERSION_NAME=4.0.0

POM_ARTIFACT_ID=buy3
POM_GROUP_ID=com.shopify.mobilebuysdk
Expand Down
40 changes: 29 additions & 11 deletions MobileBuy/buy3/src/main/java/com/shopify/buy3/GraphClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import java.util.concurrent.TimeUnit

private val DEFAULT_HTTP_CONNECTION_TIME_OUT_MS = TimeUnit.SECONDS.toMillis(10)
private val DEFAULT_HTTP_READ_WRITE_TIME_OUT_MS = TimeUnit.SECONDS.toMillis(20)
private val STOREFRONT_API_VERSION = "2019-10"
private val STOREFRONT_API_VERSION = "2020-01"

@DslMarker
annotation class GraphClientBuilder
Expand Down Expand Up @@ -119,16 +119,30 @@ class GraphClient private constructor(
* @return [GraphClient.Config] client builder
*/
fun build(
context: Context,
shopDomain: String,
accessToken: String,
configure: Config.() -> Unit = {}
context: Context,
shopDomain: String,
accessToken: String,
configure: Config.() -> Unit = {},
locale: String? = null
): GraphClient = Config.create(
context = context,
shopDomain = shopDomain,
accessToken = accessToken,
configure = configure
).build()
).build(locale)

fun build(
context: Context,
shopDomain: String,
accessToken: String,
configure: Config.() -> Unit = {}
): GraphClient = build(
context = context,
shopDomain = shopDomain,
accessToken = accessToken,
configure = configure,
locale = null
)
}

/**
Expand Down Expand Up @@ -178,12 +192,12 @@ class GraphClient private constructor(
*
* @return configured [GraphClient]
*/
fun build(): GraphClient {
fun build(locale: String?): GraphClient {
val httpCache = httpCacheConfig.let { config ->
when (config) {
is HttpCacheConfig.DiskLru -> {
val version = BuildConfig.VERSION_NAME
val tmp = (endpointUrl.toString() + "/" + version + "/" + accessToken).toByteArray(Charset.forName("UTF-8"))
val tmp = (endpointUrl.toString() + "/" + version + "/" + accessToken + "/" + locale).toByteArray(Charset.forName("UTF-8"))
val httpCacheFolder = File(config.cacheFolder, ByteString.of(*tmp).md5().hex())
HttpCache(
cacheStore = DiskLruCacheStore(
Expand All @@ -199,8 +213,9 @@ class GraphClient private constructor(
}

val okHttpClient = httpClient.withSdkHeaderInterceptor(
applicationName = applicationName,
accessToken = accessToken
applicationName = applicationName,
accessToken = accessToken,
locale = locale
).withHttpCacheInterceptor(httpCache)

return GraphClient(
Expand Down Expand Up @@ -257,14 +272,17 @@ private fun OkHttpClient.withHttpCacheInterceptor(httpCache: HttpCache?): OkHttp
}
}

private fun OkHttpClient.withSdkHeaderInterceptor(applicationName: String, accessToken: String): OkHttpClient {
private fun OkHttpClient.withSdkHeaderInterceptor(applicationName: String, accessToken: String, locale: String?): OkHttpClient {
return newBuilder().addInterceptor { chain ->
val original = chain.request()
val builder = original.newBuilder().method(original.method(), original.body())
builder.header("User-Agent", "Mobile Buy SDK Android/" + BuildConfig.VERSION_NAME + "/" + applicationName)
builder.header("X-SDK-Version", BuildConfig.VERSION_NAME)
builder.header("X-SDK-Variant", "android")
builder.header("X-Shopify-Storefront-Access-Token", accessToken)
if (locale!= null) {
builder.header("Accept-Language", locale.toString())
}
chain.proceed(builder.build())
}.build()
}
Expand Down
Loading

0 comments on commit 31af98f

Please sign in to comment.