Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NSDK-215] Update Virtusize WebView URL - General Use #115

Merged
merged 18 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .githooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@ status=$?

if [ "$status" = 0 ] ; then
echo "ktlint check ran successfully."
exit 0
else
echo "ktlint check failed. Formatting with ktlint..."
./gradlew ktlintFormat
exit 1
fi

echo "Running unit tests..."

./gradlew testDebugUnitTest --daemon

status=$? # Update the status after running unit tests

if [ "$status" = 0 ] ; then
echo "Unit tests ran successfully."
exit 0
else
echo "Unit tests failed."
exit 1
ourognao marked this conversation as resolved.
Show resolved Hide resolved
fi
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## [@next](https://github.com/virtusize/integration_android/releases/tag/@next) (2024-8-\_\_)
### Next Release
- Update the Virtusize WebView URL - General Use

### 2.6.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ data class ApiRequest(
* @param userId the user ID that is unique from the client system
*/
object VirtusizeApi {
const val DEFAULT_AOYAMA_VERSION = "3.3.1"

private var environment = VirtusizeEnvironment.GLOBAL
private lateinit var apiKey: String
private lateinit var userId: String
Expand Down Expand Up @@ -84,7 +86,7 @@ object VirtusizeApi {
*/
fun productCheck(product: VirtusizeProduct): ApiRequest {
val urlBuilder =
Uri.parse(environment.servicesApiUrl() + VirtusizeEndpoint.ProductCheck.getPath())
Uri.parse(environment.servicesApiUrl() + VirtusizeEndpoint.ProductCheck.path)
.buildUpon()
.appendQueryParameter("apiKey", apiKey)
.appendQueryParameter("externalId", product.externalId)
Expand All @@ -93,16 +95,26 @@ object VirtusizeApi {
return ApiRequest(url, HttpMethod.GET)
}

fun fetchLatestAoyamaVersion(): ApiRequest {
val url =
Uri.parse(environment.virtusizeUrl() + VirtusizeEndpoint.LatestAoyamaVersion.path)
.buildUpon()
.build()
.toString()
return ApiRequest(url, HttpMethod.GET)
}

/**
* Gets the Virtusize web view URL for a VirtusizeProduct
*
* @param version the version of the Virtusize web view
* @return the Virtusize web view URL as String
*/
fun virtusizeWebViewURL(): String {
fun virtusizeWebViewURL(version: String = DEFAULT_AOYAMA_VERSION): String {
val urlBuilder =
Uri.parse(
environment.virtusizeUrl() + VirtusizeEndpoint.VirtusizeWebView.getPath(environment),
)
.buildUpon()
environment.virtusizeUrl() + VirtusizeEndpoint.VirtusizeWebView(version = version).path,
).buildUpon()
return urlBuilder.build().toString()
}

Expand All @@ -115,7 +127,7 @@ object VirtusizeApi {
val url =
Uri.parse(
environment.defaultApiUrl() +
VirtusizeEndpoint.ProductMetaDataHints.getPath(),
VirtusizeEndpoint.ProductMetaDataHints.path,
)
.buildUpon()
.build()
Expand Down Expand Up @@ -228,7 +240,7 @@ object VirtusizeApi {
*/
fun sendOrder(order: VirtusizeOrder): ApiRequest {
val url =
Uri.parse(environment.defaultApiUrl() + VirtusizeEndpoint.Orders.getPath())
Uri.parse(environment.defaultApiUrl() + VirtusizeEndpoint.Orders.path)
.buildUpon()
.build()
.toString()
Expand All @@ -244,7 +256,7 @@ object VirtusizeApi {
val url =
Uri.parse(
environment.defaultApiUrl() +
VirtusizeEndpoint.StoreViewApiKey.getPath() +
VirtusizeEndpoint.StoreViewApiKey.path +
apiKey,
)
.buildUpon()
Expand All @@ -263,7 +275,7 @@ object VirtusizeApi {
val url =
Uri.parse(
environment.defaultApiUrl() +
VirtusizeEndpoint.StoreProducts.getPath() +
VirtusizeEndpoint.StoreProducts.path +
productId,
)
.buildUpon()
Expand All @@ -279,7 +291,7 @@ object VirtusizeApi {
*/
fun getProductTypes(): ApiRequest {
val url =
Uri.parse(environment.defaultApiUrl() + VirtusizeEndpoint.ProductType.getPath())
Uri.parse(environment.defaultApiUrl() + VirtusizeEndpoint.ProductType.path)
.buildUpon()
.build()
.toString()
Expand All @@ -292,7 +304,7 @@ object VirtusizeApi {
*/
fun getI18n(language: VirtusizeLanguage): ApiRequest {
val url =
Uri.parse(I18N_URL + VirtusizeEndpoint.I18N.getPath() + language.value)
Uri.parse(I18N_URL + VirtusizeEndpoint.I18N.path + language.value)
.buildUpon()
.build()
.toString()
Expand All @@ -301,7 +313,7 @@ object VirtusizeApi {

fun getSessions(): ApiRequest {
val url =
Uri.parse(environment.defaultApiUrl() + VirtusizeEndpoint.Sessions.getPath())
Uri.parse(environment.defaultApiUrl() + VirtusizeEndpoint.Sessions.path)
.buildUpon()
.build()
.toString()
Expand All @@ -314,7 +326,7 @@ object VirtusizeApi {
*/
fun deleteUser(): ApiRequest {
val url =
Uri.parse(environment.defaultApiUrl() + VirtusizeEndpoint.User.getPath())
Uri.parse(environment.defaultApiUrl() + VirtusizeEndpoint.User.path)
.buildUpon()
.build()
.toString()
Expand All @@ -328,7 +340,7 @@ object VirtusizeApi {
fun getUserProducts(): ApiRequest {
val url =
Uri.parse(
environment.defaultApiUrl() + VirtusizeEndpoint.UserProducts.getPath(),
environment.defaultApiUrl() + VirtusizeEndpoint.UserProducts.path,
)
.buildUpon()
.build()
Expand All @@ -343,7 +355,7 @@ object VirtusizeApi {
fun getUserBodyProfile(): ApiRequest {
val url =
Uri.parse(
environment.defaultApiUrl() + VirtusizeEndpoint.UserBodyMeasurements.getPath(),
environment.defaultApiUrl() + VirtusizeEndpoint.UserBodyMeasurements.path,
)
.buildUpon()
.build()
Expand All @@ -366,7 +378,7 @@ object VirtusizeApi {
val bodyProfileRecommendedSizeParams =
BodyProfileRecommendedSizeParams(productTypes, storeProduct, userBodyProfile)
val url =
Uri.parse("${environment.sizeRecommendationApiBaseUrl()}${VirtusizeEndpoint.GetSize.getPath()}")
Uri.parse("${environment.sizeRecommendationApiBaseUrl()}${VirtusizeEndpoint.GetSize.path}")
.buildUpon()
.build()
.toString()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.virtusize.android.network

enum class VirtusizeApiResponseFormat {
JSON,
STRING,
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import java.net.URL
import java.util.Scanner
import java.util.concurrent.TimeUnit
import javax.net.ssl.HttpsURLConnection
import kotlin.jvm.Throws

/**
* The asynchronous task to make an API request in the background thread
Expand Down Expand Up @@ -47,6 +48,9 @@ class VirtusizeApiTask(
// The Json parser interface for converting the JSON response to a given type of Java object
private var jsonParser: VirtusizeJsonParser<Any>? = null

// The response format of the API request
private var responseFormat: VirtusizeApiResponseFormat = VirtusizeApiResponseFormat.JSON

/**
* Sets up the JSON parser for converting the JSON response to a given type of Java object
* @return the [VirtusizeApiTask] with the JSON parser set up
Expand All @@ -56,6 +60,15 @@ class VirtusizeApiTask(
return this
}

/**
* Sets up the response format of the API request
* @return the [VirtusizeApiTask] with the response format set up
*/
fun setResponseFormat(responseFormat: VirtusizeApiResponseFormat): VirtusizeApiTask {
this.responseFormat = responseFormat
return this
}

/**
* Executes the API request and returns the response
* @param apiRequest [ApiRequest]
Expand Down Expand Up @@ -93,7 +106,7 @@ class VirtusizeApiTask(
setRequestProperty(HEADER_CONTENT_TYPE, "application/json")

// Set up the request header for the sessions API
if (apiRequest.url.contains(VirtusizeEndpoint.Sessions.getPath())) {
if (apiRequest.url.contains(VirtusizeEndpoint.Sessions.path)) {
sharedPreferencesHelper.getAuthToken()?.let {
setRequestProperty(HEADER_AUTH, it)
setRequestProperty(HEADER_COOKIE, "")
Expand All @@ -120,8 +133,8 @@ class VirtusizeApiTask(
val inputStreamString = readInputStreamAsString(inputStream)
val response =
parseInputStreamStringToObject(
apiRequest.url,
inputStreamString,
apiRequestUrl = apiRequest.url,
inputStreamString = inputStreamString,
)
VirtusizeApiResponse.Success(response) as VirtusizeApiResponse<T>
} catch (e: JSONException) {
Expand Down Expand Up @@ -222,35 +235,37 @@ class VirtusizeApiTask(

/**
* Parses the string of the input stream to a data object
*
* @param apiRequestUrl the API request URL
* @param inputStreamString the string of the input stream
* @return either the object that contains the content of the string of the input stream or null
*/
private fun parseInputStreamStringToObject(
apiRequestUrl: String? = null,
internal fun parseInputStreamStringToObject(
apiRequestUrl: String = "",
inputStreamString: String? = null,
): Any? {
var result: Any? = null
): Any? =
if (inputStreamString != null) {
try {
result = parseStringToObject(apiRequestUrl, inputStreamString)
parseStringToObject(apiRequestUrl = apiRequestUrl, streamString = inputStreamString)
} catch (e: JSONException) {
messageHandler?.onError(
VirtusizeErrorType.JsonParsingError.virtusizeError(
extraMessage = e.localizedMessage,
),
)
null
}
} else {
null
}
return result
}

/**
* Parses the string of the error stream to a data object
*
* @param errorStreamString the string of the error stream
* @return either an object that contains the content of the string of the input stream, the string of the error stream, or null
*/
private fun parseErrorStreamStringToObject(errorStreamString: String? = null): Any? {
internal fun parseErrorStreamStringToObject(errorStreamString: String? = null): Any? {
var result: Any? = null
if (errorStreamString != null) {
result =
Expand All @@ -265,44 +280,48 @@ class VirtusizeApiTask(

/**
* Parses the string of an input stream to an object
*
* @param apiRequestUrl the API request URL
* @param streamString the string of the input stream
* @return either the data object that is converted from streamString or null
*/
private fun parseStringToObject(
apiRequestUrl: String? = null,
@Throws(JSONException::class)
internal fun parseStringToObject(
apiRequestUrl: String = "",
streamString: String,
): Any? {
var result: Any? = null
jsonParser?.let { jsonParser ->
result =
if (apiRequestUrl != null && responseIsJsonArray(apiRequestUrl)) {
val jsonArray = JSONArray(streamString)
(0 until jsonArray.length())
.map { idx -> jsonArray.getJSONObject(idx) }
.mapNotNull { jsonParser.parse(it) }
} else {
val jsonObject = JSONObject(streamString)
jsonParser.parse(jsonObject)
}
): Any? =
when {
responseFormat == VirtusizeApiResponseFormat.STRING -> streamString.trimIndent()

responseIsJsonArray(apiRequestUrl) -> {
val jsonArray = JSONArray(streamString)
(0 until jsonArray.length())
.map { idx -> jsonArray.getJSONObject(idx) }
.mapNotNull { jsonParser?.parse(it) }
}

else -> {
val jsonObject = JSONObject(streamString)
jsonParser?.parse(jsonObject)
}
}
return result
}

/**
* Check if the response of the API request is a JSON array
*
* @param apiRequestUrl The input stream of bytes
* @return the boolean value to tell whether the response of the apiRequestUrl is a JSON array.
*/
private fun responseIsJsonArray(apiRequestUrl: String): Boolean {
return apiRequestUrl.contains(VirtusizeEndpoint.ProductType.getPath()) ||
return apiRequestUrl.contains(VirtusizeEndpoint.ProductType.path) ||
apiRequestUrl.contains(
VirtusizeEndpoint.UserProducts.getPath(),
) || apiRequestUrl.contains(VirtusizeEndpoint.GetSize.getPath())
VirtusizeEndpoint.UserProducts.path,
) || apiRequestUrl.contains(VirtusizeEndpoint.GetSize.path)
}

/**
* Returns the contents of an [InputStream] as a String.
*
* @param inputStream The input stream of bytes
* @return the string from scanning through the inputStream
*/
Expand All @@ -313,6 +332,7 @@ class VirtusizeApiTask(

/**
* Gets the API error message based on the path part of the request url
*
* @param requestPath the path part of the request URL
* @param response the response from an API request
* @return the message with the info of the request's path and response
Expand Down
Loading