From 4bfad05cd5fae0aa58a186987b5ca87659093158 Mon Sep 17 00:00:00 2001 From: Maria Anchugova Date: Wed, 3 Jul 2024 17:13:42 +0500 Subject: [PATCH 01/11] fix: fixed build gradle --- personalizatio-sdk/build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/personalizatio-sdk/build.gradle b/personalizatio-sdk/build.gradle index c20f289e..5c14c117 100644 --- a/personalizatio-sdk/build.gradle +++ b/personalizatio-sdk/build.gradle @@ -88,7 +88,6 @@ dependencies { } group = 'com.personalizatio' -apply from: '../publish.gradle' tasks.register('sourcesJar', Jar) { archiveClassifier.set('sources') From 7a0c01306fffd008d52355ee779f65109221c646 Mon Sep 17 00:00:00 2001 From: Maria Anchugova Date: Wed, 3 Jul 2024 17:20:45 +0500 Subject: [PATCH 02/11] feat: implemented recommendation methods by entity --- personalizatio-sdk/build.gradle | 3 +- .../src/main/kotlin/com/personalizatio/SDK.kt | 22 +++- .../api/entities/product/CategoryEntity.kt | 20 +++ .../entities/product/ImageUrlResizedEntity.kt | 22 ++++ .../api/entities/product/ProductEntity.kt | 46 +++++++ .../ExtendedRecommendationEntity.kt | 15 +++ .../recommendation/RecommendationEntity.kt | 14 +++ .../api/managers/RecommendationManager.kt | 118 ++++++++++++++++++ .../GetRecommendationParameter.kt | 7 ++ .../RecommendationManagerImpl.kt | 102 +++++++++++++++ 10 files changed, 366 insertions(+), 3 deletions(-) create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/CategoryEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ImageUrlResizedEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ProductEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/ExtendedRecommendationEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/RecommendationEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/GetRecommendationParameter.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt diff --git a/personalizatio-sdk/build.gradle b/personalizatio-sdk/build.gradle index 5c14c117..d8f80587 100644 --- a/personalizatio-sdk/build.gradle +++ b/personalizatio-sdk/build.gradle @@ -83,8 +83,9 @@ dependencies { implementation 'com.github.bumptech.glide:glide:4.16.0' implementation 'androidx.media3:media3-exoplayer:1.2.1' implementation 'androidx.media3:media3-ui:1.2.1' - implementation 'androidx.core:core-ktx:1.13.1' + implementation 'androidx.core:core-ktx:1.13.1' implementation 'androidx.work:work-runtime-ktx:2.9.0' + implementation 'com.google.code.gson:gson:2.11.0' } group = 'com.personalizatio' diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt index 5463fac2..53e285c4 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt @@ -12,6 +12,8 @@ import com.personalizatio.Params.TrackEvent import com.personalizatio.api.Api import com.personalizatio.api.ApiMethod import com.personalizatio.api.OnApiCallbackListener +import com.personalizatio.api.managers.RecommendationManager +import com.personalizatio.features.recommendation.RecommendationManagerImpl import com.personalizatio.notification.NotificationHandler import com.personalizatio.notification.NotificationHelper import com.personalizatio.notifications.Source @@ -52,6 +54,10 @@ open class SDK { StoriesManager(this) } + val recommendationManager: RecommendationManager by lazy { + RecommendationManagerImpl(this) + } + /** * @param shopId Shop key */ @@ -302,8 +308,14 @@ open class SDK { * @param recommender_code Recommendation block code * @param listener Callback */ + @Deprecated( + "This method will be removed in future versions. Use recommendationManager.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "recommendationManager.getRecommendation(recommender_code, listener)" + ) + ) fun recommend(recommender_code: String, listener: OnApiCallbackListener) { - recommend(recommender_code, Params(), listener) + recommendationManager.getRecommendation(recommender_code, listener) } /** @@ -313,8 +325,14 @@ open class SDK { * @param params Parameters for the request * @param listener Callback */ + @Deprecated( + "This method will be removed in future versions. Use recommendationManager.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "recommendationManager.getRecommendation(code, params, listener)" + ) + ) fun recommend(code: String, params: Params, listener: OnApiCallbackListener) { - getAsync("recommend/$code", params.build(), listener) + recommendationManager.getRecommendation(code, params, listener) } /** diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/CategoryEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/CategoryEntity.kt new file mode 100644 index 00000000..36390dab --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/CategoryEntity.kt @@ -0,0 +1,20 @@ +package com.personalizatio.api.entities.product + +import com.google.gson.annotations.SerializedName + +data class CategoryEntity( + @SerializedName("id") + val id: String, + @SerializedName("level") + val level: String, + @SerializedName("name") + val name: String, + @SerializedName("name_with_parent") + val nameWithParent: String, + @SerializedName("parent_id") + val parentId: String, + @SerializedName("url") + val url: String, + @SerializedName("url_handle") + val urlHandle: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ImageUrlResizedEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ImageUrlResizedEntity.kt new file mode 100644 index 00000000..6590e646 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ImageUrlResizedEntity.kt @@ -0,0 +1,22 @@ +package com.personalizatio.api.entities.product + +import com.google.gson.annotations.SerializedName + +data class ImageUrlResizedEntity( + @SerializedName("120") + val x120: String, + @SerializedName("140") + val x140: String, + @SerializedName("160") + val x160: String, + @SerializedName("180") + val x180: String, + @SerializedName("200") + val x200: String, + @SerializedName("220") + val x220: String, + @SerializedName("310") + val x310: String, + @SerializedName("520") + val x520: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ProductEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ProductEntity.kt new file mode 100644 index 00000000..a51022dc --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ProductEntity.kt @@ -0,0 +1,46 @@ +package com.personalizatio.api.entities.product + +import com.google.gson.annotations.SerializedName + +data class ProductEntity( + @SerializedName("brand") + val brand: String, + @SerializedName("categories") + val categories: List, + @SerializedName("category_ids") + val categoryIds: List, + @SerializedName("currency") + val currency: String, + @SerializedName("description") + val description: String, + @SerializedName("_id") + val _id: String, + @SerializedName("id") + val id: String, + @SerializedName("image_url") + val imageUrl: String, + @SerializedName("image_url_handle") + val imageUrlHandle: String, + @SerializedName("image_url_resized") + val imageUrlResized: ImageUrlResizedEntity, + @SerializedName("name") + val name: String, + @SerializedName("picture") + val picture: String, + @SerializedName("price") + val price: Double, + @SerializedName("price_formatted") + val priceFormatted: String, + @SerializedName("price_full") + val priceFull: Double, + @SerializedName("price_full_formatted") + val priceFullFormatted: String, + @SerializedName("relative_sales_rate") + val relativeSalesRate: Double, + @SerializedName("sales_rate") + val salesRate: Int, + @SerializedName("url") + val url: String, + @SerializedName("url_handle") + val urlHandle: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/ExtendedRecommendationEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/ExtendedRecommendationEntity.kt new file mode 100644 index 00000000..a9cf6be5 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/ExtendedRecommendationEntity.kt @@ -0,0 +1,15 @@ +package com.personalizatio.api.entities.recommendation + +import com.google.gson.annotations.SerializedName +import com.personalizatio.api.entities.product.ProductEntity + +data class ExtendedRecommendationEntity( + @SerializedName("html") + val html: String, + @SerializedName("id") + val id: Int, + @SerializedName("recommends") + val products: List, + @SerializedName("title") + val title: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/RecommendationEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/RecommendationEntity.kt new file mode 100644 index 00000000..e656069c --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/RecommendationEntity.kt @@ -0,0 +1,14 @@ +package com.personalizatio.api.entities.recommendation + +import com.google.gson.annotations.SerializedName + +data class RecommendationEntity( + @SerializedName("html") + val html: String, + @SerializedName("id") + val id: Int, + @SerializedName("recommends") + val productIds: List, + @SerializedName("title") + val title: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt new file mode 100644 index 00000000..de09e157 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt @@ -0,0 +1,118 @@ +package com.personalizatio.api.managers + +import com.personalizatio.Params +import com.personalizatio.api.OnApiCallbackListener +import com.personalizatio.api.entities.recommendation.ExtendedRecommendationEntity +import com.personalizatio.api.entities.recommendation.RecommendationEntity + +interface RecommendationManager { + + /** + * Request a dynamic block of recommendations + * + * @param recommenderCode Recommendation block code + * @param listener Callback + * + */ + fun getRecommendation( + recommenderCode: String, + listener: OnApiCallbackListener + ) + + /** + * Request a dynamic block of recommendations + * + * @param recommenderCode Code of the dynamic block of recommendations + * @param params Parameters for the request + * @param listener Callback + */ + fun getRecommendation( + recommenderCode: String, + params: Params, + listener: OnApiCallbackListener + ) + + /** + * Request a dynamic block of recommendations + * + * @param recommenderCode Recommendation block code + * @param onGetRecommendation Callback for get recommendation + * @param onError Callback for error + */ + fun getRecommendation( + recommenderCode: String, + onGetRecommendation: (RecommendationEntity) -> Unit, + onError: (Int, String?) -> Unit = { _: Int, _: String? -> } + ) + + /** + * Request a dynamic block of recommendations + * + * @param recommenderCode Recommendation block code + * @param onGetRecommendation Callback for get recommendation + * @param onError Callback for error + */ + fun getRecommendation( + recommenderCode: String, + params: Params, + onGetRecommendation: (RecommendationEntity) -> Unit, + onError: (Int, String?) -> Unit = { _: Int, _: String? -> } + ) + + /** + * Request a dynamic block of recommendations with all information about recommended products + * + * + * @param recommenderCode Recommendation block code + * @param listener Callback + * + */ + fun getExtendedRecommendation( + recommenderCode: String, + listener: OnApiCallbackListener + ) + + /** + * Request a dynamic block of recommendations with all information about recommended products + * + * + * @param recommenderCode Recommendation block code + * @param params Params + * @param listener Callback + * + */ + fun getExtendedRecommendation( + recommenderCode: String, + params: Params, + listener: OnApiCallbackListener + ) + + /** + * Request a dynamic block of recommendations with all information about recommended products + * + * + * @param recommenderCode Recommendation block code + * @param onGetExtendedRecommendation Callback for get extended recommendation + * @param onError Callback for error + */ + fun getExtendedRecommendation( + recommenderCode: String, + onGetExtendedRecommendation: (ExtendedRecommendationEntity) -> Unit, + onError: (Int, String?) -> Unit = { _: Int, _: String? -> } + ) + + /** + * Request a dynamic block of recommendations with all information about recommended products + * + * + * @param recommenderCode Recommendation block code + * @param onGetExtendedRecommendation Callback for get extended recommendation + * @param onError Callback for error + */ + fun getExtendedRecommendation( + recommenderCode: String, + params: Params, + onGetExtendedRecommendation: (ExtendedRecommendationEntity) -> Unit, + onError: (Int, String?) -> Unit = { _: Int, _: String? -> } + ) +} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/GetRecommendationParameter.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/GetRecommendationParameter.kt new file mode 100644 index 00000000..3242c12e --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/GetRecommendationParameter.kt @@ -0,0 +1,7 @@ +package com.personalizatio.features.recommendation + +import com.personalizatio.AbstractParams.ParamInterface + +internal enum class GetRecommendationParameter(override val value: String) : ParamInterface { + EXTENDED("extended") +} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt new file mode 100644 index 00000000..51cfdd40 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt @@ -0,0 +1,102 @@ +package com.personalizatio.features.recommendation + +import com.google.gson.Gson +import com.personalizatio.Params +import com.personalizatio.SDK +import com.personalizatio.api.OnApiCallbackListener +import com.personalizatio.api.managers.RecommendationManager +import com.personalizatio.api.entities.recommendation.RecommendationEntity +import com.personalizatio.api.entities.recommendation.ExtendedRecommendationEntity +import org.json.JSONObject + +internal class RecommendationManagerImpl(private val sdk: SDK) : RecommendationManager { + + override fun getRecommendation( + recommenderCode: String, + onGetRecommendation: (RecommendationEntity) -> Unit, + onError: (Int, String?) -> Unit + ) { + val params = getRecommendationParams() + getRecommendation(recommenderCode, params, onGetRecommendation, onError) + } + + override fun getRecommendation(recommenderCode: String, listener: OnApiCallbackListener) { + val params = getRecommendationParams() + getRecommendation(recommenderCode, params, listener) + } + + override fun getRecommendation( + recommenderCode: String, + params: Params, + onGetRecommendation: (RecommendationEntity) -> Unit, + onError: (Int, String?) -> Unit + ) { + getRecommendation(recommenderCode, params, object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) { + response?.let { + val recommendationEntity = Gson().fromJson(it.toString(), RecommendationEntity::class.java) + onGetRecommendation(recommendationEntity) + } + } + + override fun onError(code: Int, msg: String?) { + onError(code, msg) + } + }) + } + + override fun getExtendedRecommendation( + recommenderCode: String, + onGetExtendedRecommendation: (ExtendedRecommendationEntity) -> Unit, + onError: (Int, String?) -> Unit + ) { + val params = getRecommendationParams(extended = true) + getExtendedRecommendation(recommenderCode, params, onGetExtendedRecommendation, onError) + } + + override fun getExtendedRecommendation( + recommenderCode: String, + params: Params, + onGetExtendedRecommendation: (ExtendedRecommendationEntity) -> Unit, + onError: (Int, String?) -> Unit + ) { + getExtendedRecommendation(recommenderCode, params, object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) { + response?.let { + val extendedRecommendationEntity = Gson().fromJson(it.toString(), ExtendedRecommendationEntity::class.java) + onGetExtendedRecommendation.invoke(extendedRecommendationEntity) + } + } + + override fun onError(code: Int, msg: String?) { + onError(code, msg) + } + }) + } + + override fun getExtendedRecommendation(recommenderCode: String, listener: OnApiCallbackListener) { + val params = getRecommendationParams(extended = true) + getRecommendation(recommenderCode, params, listener) + } + + override fun getExtendedRecommendation(recommenderCode: String, params: Params, listener: OnApiCallbackListener) { + params.put(GetRecommendationParameter.EXTENDED, true) + getRecommendation(recommenderCode, params, listener) + } + + override fun getRecommendation(recommenderCode: String, params: Params, listener: OnApiCallbackListener) { + sdk.getAsync("$GET_RECOMMENDATION_REQUEST/$recommenderCode", params.build(), listener) + } + + private fun getRecommendationParams(extended: Boolean? = null) : Params { + val params = Params() + + if(extended != null) params.put(GetRecommendationParameter.EXTENDED, extended) + + return params + } + + companion object { + const val GET_RECOMMENDATION_REQUEST = "recommend" + } +} From f225f9647a87f9a6e8b8876b87e248c1458af827 Mon Sep 17 00:00:00 2001 From: Maria Anchugova Date: Wed, 3 Jul 2024 18:18:29 +0500 Subject: [PATCH 03/11] feat: implemented cart methods by entity --- .../main/kotlin/com/personalizatio/Params.kt | 46 +++++- .../src/main/kotlin/com/personalizatio/SDK.kt | 6 + .../api/entities/cart/CartEntity.kt | 10 ++ .../api/entities/cart/DataEntity.kt | 8 + .../api/entities/cart/ItemEntity.kt | 10 ++ .../api/managers/CartManager.kt | 143 ++++++++++++++++++ .../api/params/ProductItemParams.kt | 33 ++++ .../features/cart/CartManagerImpl.kt | 142 +++++++++++++++++ 8 files changed, 395 insertions(+), 3 deletions(-) create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/CartEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/DataEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/ItemEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/CartManager.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/ProductItemParams.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/features/cart/CartManagerImpl.kt diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/Params.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/Params.kt index b4e4fb36..14a73bff 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/Params.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/Params.kt @@ -1,6 +1,7 @@ package com.personalizatio import android.util.Log +import com.personalizatio.api.params.ProductItemParams import org.json.JSONArray import org.json.JSONException import org.json.JSONObject @@ -66,8 +67,15 @@ class Params : AbstractParams() { } /** - * Товар + * Product */ + @Deprecated( + "This class will be removed in future versions.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "ProductItemParams", + "com.personalizatio.api.params.ProductItemParams" + ) + ) class Item(id: String) { enum class COLUMN(val value: String) { ID("id"), @@ -124,7 +132,21 @@ class Params : AbstractParams() { enum class TrackEvent(@JvmField var value: String) { VIEW("view"), CATEGORY("category"), + @Deprecated( + "This method will be removed in future versions.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "sdk.cartManager.addToCart()", + "com.personalizatio.api.managers.CartManager" + ) + ) CART("cart"), + @Deprecated( + "This method will be removed in future versions.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "sdk.cartManager.removeFromCart()", + "com.personalizatio.api.managers.CartManager" + ) + ) REMOVE_FROM_CART("remove_from_cart"), PURCHASE("purchase"), SEARCH("search"), @@ -148,9 +170,27 @@ class Params : AbstractParams() { } /** - * Вставка товара + * Add item */ + @Deprecated( + "This method will be removed in future versions.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "put(productItemParams)", + "com.personalizatio.api.params.ProductItemParams" + ) + ) fun put(item: Item): Params { + return putProductParameters(item.columns) + } + + /** + * Add product + */ + fun put(productItemParams: ProductItemParams): Params { + return putProductParameters(productItemParams.parameters) + } + + private fun putProductParameters(parameters: HashMap): Params { try { val array: JSONArray if (params.has("items")) { @@ -160,7 +200,7 @@ class Params : AbstractParams() { params.put("items", array) } val jsonObject = JSONObject() - for ((key, value) in item.columns) { + for ((key, value) in parameters) { jsonObject.put(key, value) } array.put(jsonObject) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt index 53e285c4..7d44836b 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt @@ -12,7 +12,9 @@ import com.personalizatio.Params.TrackEvent import com.personalizatio.api.Api import com.personalizatio.api.ApiMethod import com.personalizatio.api.OnApiCallbackListener +import com.personalizatio.api.managers.CartManager import com.personalizatio.api.managers.RecommendationManager +import com.personalizatio.features.cart.CartManagerImpl import com.personalizatio.features.recommendation.RecommendationManagerImpl import com.personalizatio.notification.NotificationHandler import com.personalizatio.notification.NotificationHelper @@ -58,6 +60,10 @@ open class SDK { RecommendationManagerImpl(this) } + val cartManager: CartManager by lazy { + CartManagerImpl(this) + } + /** * @param shopId Shop key */ diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/CartEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/CartEntity.kt new file mode 100644 index 00000000..6e24f466 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/CartEntity.kt @@ -0,0 +1,10 @@ +package com.personalizatio.api.entities.cart + +import com.google.gson.annotations.SerializedName + +data class CartEntity( + @SerializedName("data") + val data: DataEntity, + @SerializedName("status") + val status: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/DataEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/DataEntity.kt new file mode 100644 index 00000000..6d10e3b2 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/DataEntity.kt @@ -0,0 +1,8 @@ +package com.personalizatio.api.entities.cart + +import com.google.gson.annotations.SerializedName + +data class DataEntity( + @SerializedName("items") + val items: List +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/ItemEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/ItemEntity.kt new file mode 100644 index 00000000..c317d4aa --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/ItemEntity.kt @@ -0,0 +1,10 @@ +package com.personalizatio.api.entities.cart + +import com.google.gson.annotations.SerializedName + +data class ItemEntity( + @SerializedName("quantity") + val quantity: Int, + @SerializedName("uniqid") + val uniqid: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/CartManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/CartManager.kt new file mode 100644 index 00000000..d668df91 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/CartManager.kt @@ -0,0 +1,143 @@ +package com.personalizatio.api.managers + +import com.personalizatio.Params +import com.personalizatio.api.OnApiCallbackListener +import com.personalizatio.api.entities.cart.CartEntity + +interface CartManager { + + /** + * Request a cart + * + * @param onGetCart Callback for get cart + * @param onError Callback for error + */ + fun getCart( + onGetCart: (CartEntity) -> Unit, + onError: (Int, String?) -> Unit = { _: Int, _: String? -> } + ) + + /** + * Request a cart + * + * @param listener Callback + */ + fun getCart(listener: OnApiCallbackListener) + + /** + * Add product to cart + * + * @param productId Product ID + * @param amount Amount + * @param listener Callback + */ + fun addToCart( + productId: String, + amount: Int, + listener: OnApiCallbackListener? = null + ) + + /** + * Add product to cart + * + * @param productId Product ID + * @param amount Amount + * @param price Price + * @param listener Callback + */ + fun addToCart( + productId: String, + amount: Int, + price: Double, + listener: OnApiCallbackListener? = null + ) + + /** + * Add product to cart + * + * @param productId Product ID + * @param amount Amount + * @param fashionSize Fashion size + * @param listener Callback + */ + fun addToCart( + productId: String, + amount: Int, + fashionSize: String, + listener: OnApiCallbackListener? = null + ) + + /** + * Add product to cart + * + * @param productId Product ID + * @param amount Amount + * @param price Price + * @param fashionSize Fashion size + * @param listener Callback + */ + fun addToCart( + productId: String, + amount: Int, + price: Double, + fashionSize: String, + listener: OnApiCallbackListener? = null + ) + + /** + * Add products to cart + * + * @param products Product ID and Amount pair map + * @param listener Callback + */ + fun addToCart( + products: Map, + listener: OnApiCallbackListener? = null + ) + + /** + * Add products to cart + * + * @param params Params + * @param listener Callback + */ + fun addToCart( + params: Params, + listener: OnApiCallbackListener? = null + ) + + /** + * Remove product from cart + * + * @param productId Product ID + * @param amount Amount + * @param listener Callback + */ + fun removeFromCart( + productId: String, + amount: Int, + listener: OnApiCallbackListener? = null + ) + + /** + * Remove products from cart + * + * @param products Product ID and Quantity pair map + * @param listener Callback + */ + fun removeFromCart( + products: Map, + listener: OnApiCallbackListener? = null + ) + + /** + * Remove products from cart + * + * @param params Params + * @param listener Callback + */ + fun removeFromCart( + params: Params, + listener: OnApiCallbackListener? = null + ) +} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/ProductItemParams.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/ProductItemParams.kt new file mode 100644 index 00000000..62396fe9 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/ProductItemParams.kt @@ -0,0 +1,33 @@ +package com.personalizatio.api.params + +class ProductItemParams(id: String) { + enum class PARAMETER(val value: String) { + ID("id"), + AMOUNT("amount"), + PRICE("price"), + FASHION_SIZE("fashion_size") + } + + val parameters: HashMap = HashMap() + + init { + parameters[PARAMETER.ID.value] = id + } + + fun set(column: PARAMETER, value: String): ProductItemParams { + parameters[column.value] = value + return this + } + + fun set(column: PARAMETER, value: Int): ProductItemParams { + return set(column, value.toString()) + } + + fun set(column: PARAMETER, value: Double): ProductItemParams { + return set(column, value.toString()) + } + + fun set(column: PARAMETER, value: Boolean): ProductItemParams { + return set(column, if (value) "1" else "0") + } +} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/cart/CartManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/cart/CartManagerImpl.kt new file mode 100644 index 00000000..c8a391c7 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/cart/CartManagerImpl.kt @@ -0,0 +1,142 @@ +package com.personalizatio.features.cart + +import com.google.gson.Gson +import com.personalizatio.Params +import com.personalizatio.SDK +import com.personalizatio.api.OnApiCallbackListener +import com.personalizatio.api.entities.cart.CartEntity +import com.personalizatio.api.managers.CartManager +import com.personalizatio.api.params.ProductItemParams +import org.json.JSONObject + +internal class CartManagerImpl(val sdk: SDK) : CartManager { + + override fun getCart( + onGetCart: (CartEntity) -> Unit, + onError: (Int, String?) -> Unit + ) { + getCart(object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) { + response?.let { + val cartEntity = Gson().fromJson(it.toString(), CartEntity::class.java) + onGetCart(cartEntity) + } + } + + override fun onError(code: Int, msg: String?) { + onError(code, msg) + } + }) + } + + override fun getCart( + listener: OnApiCallbackListener + ) { + sdk.getAsync(GET_CART_REQUEST, Params().build(), listener) + } + + override fun addToCart( + productId: String, + amount: Int, + listener: OnApiCallbackListener? + ) { + val params = createProductParams(productId, amount) + addToCart(params, listener) + } + + override fun addToCart( + productId: String, + amount: Int, + price: Double, + listener: OnApiCallbackListener? + ) { + val params = createProductParams(productId, amount, price = price) + addToCart(params, listener) + } + + override fun addToCart( + productId: String, + amount: Int, + fashionSize: String, + listener: OnApiCallbackListener? + ) { + val params = createProductParams(productId, amount, fashionSize = fashionSize) + addToCart(params, listener) + } + + override fun addToCart( + productId: String, + amount: Int, + price: Double, + fashionSize: String, + listener: OnApiCallbackListener? + ) { + val params = createProductParams(productId, amount, price = price, fashionSize = fashionSize) + addToCart(params, listener) + } + + override fun addToCart( + products: Map, + listener: OnApiCallbackListener? + ) { + val params = createProductsParams(products) + addToCart(params, listener) + } + + override fun addToCart( + params: Params, + listener: OnApiCallbackListener? + ) { + sdk.track(Params.TrackEvent.CART, params, listener) + } + + override fun removeFromCart( + productId: String, + amount: Int, + listener: OnApiCallbackListener? + ) { + val params = createProductParams(productId, amount) + removeFromCart(params, listener) + } + + override fun removeFromCart( + products: Map, + listener: OnApiCallbackListener? + ) { + val params = createProductsParams(products) + removeFromCart(params, listener) + } + + override fun removeFromCart( + params: Params, + listener: OnApiCallbackListener? + ) { + sdk.track(Params.TrackEvent.REMOVE_FROM_CART, params, listener) + } + + private fun createProductParams(productId: String, amount: Int, price: Double? = null, fashionSize: String? = null) : Params { + return Params().put(createProductItemParams(productId, amount, price, fashionSize)) + } + + private fun createProductsParams(products: Map) : Params { + val params = Params() + for (product in products) { + params.put(createProductItemParams(product.key, product.value)) + } + return params + } + + private fun createProductItemParams(productId: String, amount: Int, price: Double? = null, fashionSize: String? = null) : ProductItemParams { + val productItemParams = ProductItemParams(productId) + .set(ProductItemParams.PARAMETER.AMOUNT, amount) + + if(price != null) productItemParams.set(ProductItemParams.PARAMETER.PRICE, price) + if(fashionSize != null) productItemParams.set(ProductItemParams.PARAMETER.FASHION_SIZE, fashionSize) + + return productItemParams + } + + companion object { + const val GET_CART_REQUEST = "products/cart" + } +} From f5f84d82a683c240c8a9cc2a3d7d065f6868b6ba Mon Sep 17 00:00:00 2001 From: Maria Anchugova Date: Wed, 3 Jul 2024 19:11:04 +0500 Subject: [PATCH 04/11] feat: implemented search methods by entity --- .../src/main/kotlin/com/personalizatio/SDK.kt | 24 +++ .../kotlin/com/personalizatio/SearchParams.kt | 7 + .../api/entities/search/BrandEntity.kt | 10 ++ .../api/entities/search/CategoryEntity.kt | 18 +++ .../api/entities/search/PriceRangeEntity.kt | 10 ++ .../api/entities/search/PriceRangesEntity.kt | 10 ++ .../api/entities/search/SearchBlankEntity.kt | 13 ++ .../api/entities/search/SearchFullEntity.kt | 31 ++++ .../entities/search/SearchInstantEntity.kt | 27 ++++ .../api/entities/search/SuggestEntity.kt | 10 ++ .../api/managers/SearchManager.kt | 134 +++++++++++++++++ .../personalizatio/api/params/SearchParams.kt | 58 ++++++++ .../features/search/SearchManagerImpl.kt | 138 ++++++++++++++++++ .../features/search/SearchParameter.kt | 8 + 14 files changed, 498 insertions(+) create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/BrandEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/CategoryEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangeEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangesEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchBlankEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchFullEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchInstantEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SuggestEntity.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/SearchParams.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchParameter.kt diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt index 7d44836b..69fa0339 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt @@ -14,8 +14,10 @@ import com.personalizatio.api.ApiMethod import com.personalizatio.api.OnApiCallbackListener import com.personalizatio.api.managers.CartManager import com.personalizatio.api.managers.RecommendationManager +import com.personalizatio.api.managers.SearchManager import com.personalizatio.features.cart.CartManagerImpl import com.personalizatio.features.recommendation.RecommendationManagerImpl +import com.personalizatio.features.search.SearchManagerImpl import com.personalizatio.notification.NotificationHandler import com.personalizatio.notification.NotificationHelper import com.personalizatio.notifications.Source @@ -64,6 +66,10 @@ open class SDK { CartManagerImpl(this) } + val searchManager: SearchManager by lazy { + SearchManagerImpl(this) + } + /** * @param shopId Shop key */ @@ -258,6 +264,12 @@ open class SDK { * @param type Search type * @param listener Callback */ + @Deprecated( + "This class will be removed in future versions.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "searchManager.searchInstant(query, listener) or searchManager.searchFull(query, listener)" + ) + ) fun search(query: String, type: SearchParams.TYPE, listener: OnApiCallbackListener) { search(query, type, SearchParams(), listener) } @@ -270,6 +282,12 @@ open class SDK { * @param params Additional parameters for the request * @param listener v */ + @Deprecated( + "This class will be removed in future versions.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "searchManager.searchInstant(query, params, listener) or searchManager.searchFull(query, params, listener)" + ) + ) fun search( query: String, type: SearchParams.TYPE, @@ -285,6 +303,12 @@ open class SDK { } } + @Deprecated( + "This class will be removed in future versions.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "searchManager.searchBlank(listener)" + ) + ) fun searchBlank(listener: OnApiCallbackListener) { if (search != null) { if (search?.blank == null) { diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SearchParams.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SearchParams.kt index 48418be7..8a0b2ab1 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SearchParams.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SearchParams.kt @@ -4,6 +4,13 @@ import org.json.JSONArray import org.json.JSONException import org.json.JSONObject +@Deprecated( + "This class will be removed in future versions.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "SearchParams", + "com.personalizatio.api.params.SearchParams" + ) +) class SearchParams : AbstractParams() { enum class Parameter(override var value: String) : ParamInterface { diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/BrandEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/BrandEntity.kt new file mode 100644 index 00000000..947c4069 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/BrandEntity.kt @@ -0,0 +1,10 @@ +package com.personalizatio.api.entities.search + +import com.google.gson.annotations.SerializedName + +data class BrandEntity( + @SerializedName("count") + val count: Int, + @SerializedName("name") + val name: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/CategoryEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/CategoryEntity.kt new file mode 100644 index 00000000..31cf24db --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/CategoryEntity.kt @@ -0,0 +1,18 @@ +package com.personalizatio.api.entities.search + +import com.google.gson.annotations.SerializedName + +data class CategoryEntity( + @SerializedName("count") + val count: Int, + @SerializedName("id") + val id: String, + @SerializedName("name") + val name: String, + @SerializedName("parent") + val parent: String, + @SerializedName("url") + val url: String, + @SerializedName("url_handle") + val urlHandle: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangeEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangeEntity.kt new file mode 100644 index 00000000..1d8164a2 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangeEntity.kt @@ -0,0 +1,10 @@ +package com.personalizatio.api.entities.search + +import com.google.gson.annotations.SerializedName + +data class PriceRangeEntity( + @SerializedName("max") + val max: Double, + @SerializedName("min") + val min: Double +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangesEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangesEntity.kt new file mode 100644 index 00000000..50eab3b3 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangesEntity.kt @@ -0,0 +1,10 @@ +package com.personalizatio.api.entities.search + +import com.google.gson.annotations.SerializedName + +data class PriceRangesEntity( + @SerializedName("count") + val count: Int, + @SerializedName("from") + val from: Double +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchBlankEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchBlankEntity.kt new file mode 100644 index 00000000..40830c88 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchBlankEntity.kt @@ -0,0 +1,13 @@ +package com.personalizatio.api.entities.search + +import com.google.gson.annotations.SerializedName +import com.personalizatio.api.entities.product.ProductEntity + +data class SearchBlankEntity( + @SerializedName("html") + val html: String, + @SerializedName("products") + val products: List, + @SerializedName("suggests") + val suggests: List +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchFullEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchFullEntity.kt new file mode 100644 index 00000000..956f1090 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchFullEntity.kt @@ -0,0 +1,31 @@ +package com.personalizatio.api.entities.search + +import com.google.gson.annotations.SerializedName +import com.personalizatio.api.entities.product.ProductEntity + +data class SearchFullEntity( + @SerializedName("brands") + val brands: List, + @SerializedName("categories") + val categories: List, + @SerializedName("clarification") + val clarification: Boolean, + @SerializedName("collections") + val collections: List, + @SerializedName("html") + val html: String, + @SerializedName("price_median") + val priceMedian: Double, + @SerializedName("price_range") + val priceRange: PriceRangeEntity, + @SerializedName("price_ranges") + val priceRanges: List, + @SerializedName("products") + val products: List, + @SerializedName("products_total") + val productsTotal: Int, + @SerializedName("requests_count") + val requestsCount: Int, + @SerializedName("search_query") + val searchQuery: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchInstantEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchInstantEntity.kt new file mode 100644 index 00000000..eee22c58 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchInstantEntity.kt @@ -0,0 +1,27 @@ +package com.personalizatio.api.entities.search + +import com.google.gson.annotations.SerializedName +import com.personalizatio.api.entities.product.ProductEntity + +data class SearchInstantEntity( + @SerializedName("book_author") + val bookAuthor: List, + @SerializedName("categories") + val categories: List, + @SerializedName("clarification") + val clarification: Boolean, + @SerializedName("collections") + val collections: List, + @SerializedName("html") + val html: String, + @SerializedName("products") + val products: List, + @SerializedName("products_total") + val productsTotal: Int, + @SerializedName("queries") + val queries: List, + @SerializedName("requests_count") + val requestsCount: Int, + @SerializedName("search_query") + val searchQuery: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SuggestEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SuggestEntity.kt new file mode 100644 index 00000000..1b676805 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SuggestEntity.kt @@ -0,0 +1,10 @@ +package com.personalizatio.api.entities.search + +import com.google.gson.annotations.SerializedName + +data class SuggestEntity( + @SerializedName("name") + val name: String, + @SerializedName("url") + val url: String +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt new file mode 100644 index 00000000..0f45904f --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt @@ -0,0 +1,134 @@ +package com.personalizatio.api.managers + +import com.personalizatio.api.OnApiCallbackListener +import com.personalizatio.api.entities.search.SearchBlankEntity +import com.personalizatio.api.entities.search.SearchFullEntity +import com.personalizatio.api.entities.search.SearchInstantEntity +import com.personalizatio.api.params.SearchParams + +interface SearchManager { + + /** + * Search full + * + * @param query Search phrase + * @param onGetSearchFull Callback for search full + * @param onError Callback for error + */ + fun searchFull( + query: String, + onGetSearchFull: (SearchFullEntity) -> Unit, + onError: (Int, String?) -> Unit = { _: Int, _: String? -> } + ) + + /** + * Search full + * + * @param query Search phrase + * @param listener Callback + */ + fun searchFull( + query: String, + listener: OnApiCallbackListener + ) + + /** + * Search full + * + * @param query Search phrase + * @param searchParams SearchParams + * @param onGetSearchFull Callback for search full + * @param onError Callback for error + */ + fun searchFull( + query: String, + searchParams: SearchParams, + onGetSearchFull: (SearchFullEntity) -> Unit, + onError: (Int, String?) -> Unit = { _: Int, _: String? -> } + ) + + /** + * Search full + * + * @param query Search phrase + * @param searchParams SearchParams + * @param listener Callback + */ + fun searchFull( + query: String, + searchParams: SearchParams, + listener: OnApiCallbackListener + ) + + /** + * Search instant + * + * @param query Search phrase + * @param onGetSearchInstant Callback for search instant + * @param onError Callback for error + */ + fun searchInstant( + query: String, + onGetSearchInstant: (SearchInstantEntity) -> Unit, + onError: (Int, String?) -> Unit = { _: Int, _: String? -> } + ) + + /** + * Search instant + * + * @param query Search phrase + * @param listener Callback + */ + fun searchInstant( + query: String, + listener: OnApiCallbackListener + ) + + /** + * Search instant + * + * @param query Search phrase + * @param searchParams SearchParams + * @param onGetSearchInstant Callback for search instant + * @param onError Callback for error + */ + fun searchInstant( + query: String, + searchParams: SearchParams, + onGetSearchInstant: (SearchInstantEntity) -> Unit, + onError: (Int, String?) -> Unit = { _: Int, _: String? -> } + ) + + /** + * Search instant + * + * @param query Search phrase + * @param searchParams SearchParams + * @param listener Callback + */ + fun searchInstant( + query: String, + searchParams: SearchParams, + listener: OnApiCallbackListener + ) + + /** + * Search blank + * + * @param listener Callback + */ + fun searchBlank( + listener: OnApiCallbackListener + ) + + /** + * Search blank + * + * @param onGetSearchBlank Callback for search blank + * @param onError Callback for error + */ + fun searchBlank( + onGetSearchBlank: (SearchBlankEntity) -> Unit, + onError: (Int, String?) -> Unit = { _: Int, _: String? -> } + ) +} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/SearchParams.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/SearchParams.kt new file mode 100644 index 00000000..05b3e186 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/SearchParams.kt @@ -0,0 +1,58 @@ +package com.personalizatio.api.params + +import com.personalizatio.AbstractParams +import com.personalizatio.SDK +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +class SearchParams : AbstractParams() { + + enum class Parameter(override var value: String) : ParamInterface { + PAGE("page"), + LIMIT("limit"), + CATEGORY_LIMIT("category_limit"), + CATEGORIES("categories"), + EXTENDED("extended"), + SORT_BY("sort_by"), + SORT_DIR("sort_dir"), + LOCATIONS("locations"), + BRANDS("brands"), + FILTERS("filters"), + PRICE_MIN("price_min"), + PRICE_MAX("price_max"), + COLORS("colors"), + FASHION_SIZES("fashion_sizes"), + EXCLUDE("exclude"), + NO_CLARIFICATION("no_clarification") + } + + enum class TYPE(var value: String) { + INSTANT("instant_search"), + FULL("full_search") + } + + class SearchFilters { + private val filters = HashMap>() + + fun put(key: String, values: Array) { + filters[key] = values + } + + override fun toString(): String { + val json = JSONObject() + for ((key, value) in filters) { + try { + json.put(key, JSONArray(value)) + } catch (e: JSONException) { + SDK.warn(e.message) + } + } + return json.toString() + } + } + + fun put(param: Parameter, value: SearchFilters): SearchParams { + return put(param, value.toString()) + } +} \ No newline at end of file diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt new file mode 100644 index 00000000..73bb5d49 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt @@ -0,0 +1,138 @@ +package com.personalizatio.features.search + +import com.google.gson.Gson +import com.personalizatio.Params +import com.personalizatio.SDK +import com.personalizatio.api.OnApiCallbackListener +import com.personalizatio.api.params.SearchParams +import com.personalizatio.api.managers.SearchManager +import com.personalizatio.api.entities.search.SearchBlankEntity +import com.personalizatio.api.entities.search.SearchFullEntity +import com.personalizatio.api.entities.search.SearchInstantEntity +import org.json.JSONObject + +internal class SearchManagerImpl(private val sdk: SDK) : SearchManager { + + override fun searchFull( + query: String, + onGetSearchFull: (SearchFullEntity) -> Unit, + onError: (Int, String?) -> Unit + ) { + searchFull(query, SearchParams(), onGetSearchFull, onError) + } + + override fun searchFull( + query: String, + searchParams: SearchParams, + onGetSearchFull: (SearchFullEntity) -> Unit, + onError: (Int, String?) -> Unit + ) { + searchFull(query, searchParams, object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) { + response?.let { + val searchFullEntity = Gson().fromJson(it.toString(), SearchFullEntity::class.java) + onGetSearchFull(searchFullEntity) + } + } + + override fun onError(code: Int, msg: String?) { + onError(code, msg) + } + }) + } + + override fun searchFull(query: String, listener: OnApiCallbackListener) { + searchFull(query, SearchParams(), listener) + } + + override fun searchFull( + query: String, + searchParams: SearchParams, + listener: OnApiCallbackListener + ) { + search(query, SearchParams.TYPE.FULL, searchParams, listener) + } + + override fun searchInstant( + query: String, + onGetSearchInstant: (SearchInstantEntity) -> Unit, + onError: (Int, String?) -> Unit + ) { + searchInstant(query, SearchParams(), onGetSearchInstant, onError) + } + + override fun searchInstant( + query: String, + searchParams: SearchParams, + onGetSearchInstant: (SearchInstantEntity) -> Unit, + onError: (Int, String?) -> Unit + ) { + searchInstant(query, searchParams, object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) { + response?.let { + val searchInstantEntity = Gson().fromJson(it.toString(), SearchInstantEntity::class.java) + onGetSearchInstant(searchInstantEntity) + } + } + + override fun onError(code: Int, msg: String?) { + onError(code, msg) + } + }) + } + + override fun searchInstant( + query: String, + listener: OnApiCallbackListener + ) { + searchInstant(query, SearchParams(), listener) + } + + + override fun searchInstant( + query: String, + searchParams: SearchParams, + listener: OnApiCallbackListener + ) { + search(query, SearchParams.TYPE.INSTANT, searchParams, listener) + } + + override fun searchBlank( + onGetSearchBlank: (SearchBlankEntity) -> Unit, + onError: (Int, String?) -> Unit + ) { + searchBlank(object : OnApiCallbackListener() { + override fun onSuccess(response: JSONObject?) { + response?.let { + val searchBlankEntity = Gson().fromJson(it.toString(), SearchBlankEntity::class.java) + onGetSearchBlank(searchBlankEntity) + } + } + + override fun onError(code: Int, msg: String?) { + onError(code, msg) + } + }) + } + + override fun searchBlank(listener: OnApiCallbackListener) { + sdk.getAsync(BLANK_SEARCH_REQUEST, Params().build(), listener) + } + + private fun search( + query: String, + type: SearchParams.TYPE, + params: SearchParams, + listener: OnApiCallbackListener + ) { + params + .put(SearchParameter.SEARCH_TYPE, type.value) + .put(SearchParameter.SEARCH_QUERY, query) + sdk.getAsync(SEARCH_REQUEST, params.build(), listener) + } + + companion object { + private const val SEARCH_REQUEST = "search" + private const val BLANK_SEARCH_REQUEST = "search/blank" + } +} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchParameter.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchParameter.kt new file mode 100644 index 00000000..bc792680 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchParameter.kt @@ -0,0 +1,8 @@ +package com.personalizatio.features.search + +import com.personalizatio.AbstractParams.ParamInterface + +internal enum class SearchParameter(override val value: String) : ParamInterface { + SEARCH_TYPE("type"), + SEARCH_QUERY("search_query") +} \ No newline at end of file From 27e4dad9ce0c1f3fe5a8d6233e1a8e5744918300 Mon Sep 17 00:00:00 2001 From: Maria Anchugova Date: Wed, 3 Jul 2024 21:14:52 +0500 Subject: [PATCH 05/11] feat: separated track event methods --- .../main/kotlin/com/personalizatio/Params.kt | 14 -- .../src/main/kotlin/com/personalizatio/SDK.kt | 74 ++++----- .../api/entities/cart/CartEntity.kt | 10 -- .../api/entities/cart/DataEntity.kt | 8 - .../api/entities/cart/ItemEntity.kt | 10 -- .../api/managers/CartManager.kt | 143 ------------------ .../api/managers/TrackEventManager.kt | 46 ++++++ .../features/cart/CartManagerImpl.kt | 142 ----------------- .../track_event/TrackEventManagerImpl.kt | 55 +++++++ 9 files changed, 128 insertions(+), 374 deletions(-) delete mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/CartEntity.kt delete mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/DataEntity.kt delete mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/ItemEntity.kt delete mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/CartManager.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/TrackEventManager.kt delete mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/features/cart/CartManagerImpl.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/features/track_event/TrackEventManagerImpl.kt diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/Params.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/Params.kt index 14a73bff..e0bbb9ab 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/Params.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/Params.kt @@ -132,21 +132,7 @@ class Params : AbstractParams() { enum class TrackEvent(@JvmField var value: String) { VIEW("view"), CATEGORY("category"), - @Deprecated( - "This method will be removed in future versions.", - level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( - "sdk.cartManager.addToCart()", - "com.personalizatio.api.managers.CartManager" - ) - ) CART("cart"), - @Deprecated( - "This method will be removed in future versions.", - level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( - "sdk.cartManager.removeFromCart()", - "com.personalizatio.api.managers.CartManager" - ) - ) REMOVE_FROM_CART("remove_from_cart"), PURCHASE("purchase"), SEARCH("search"), diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt index 69fa0339..e13a6cab 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt @@ -12,10 +12,10 @@ import com.personalizatio.Params.TrackEvent import com.personalizatio.api.Api import com.personalizatio.api.ApiMethod import com.personalizatio.api.OnApiCallbackListener -import com.personalizatio.api.managers.CartManager +import com.personalizatio.api.managers.TrackEventManager import com.personalizatio.api.managers.RecommendationManager import com.personalizatio.api.managers.SearchManager -import com.personalizatio.features.cart.CartManagerImpl +import com.personalizatio.features.track_event.TrackEventManagerImpl import com.personalizatio.features.recommendation.RecommendationManagerImpl import com.personalizatio.features.search.SearchManagerImpl import com.personalizatio.notification.NotificationHandler @@ -58,12 +58,12 @@ open class SDK { StoriesManager(this) } - val recommendationManager: RecommendationManager by lazy { - RecommendationManagerImpl(this) + val trackEventManager: TrackEventManager by lazy { + TrackEventManagerImpl(this) } - val cartManager: CartManager by lazy { - CartManagerImpl(this) + val recommendationManager: RecommendationManager by lazy { + RecommendationManagerImpl(this) } val searchManager: SearchManager by lazy { @@ -371,8 +371,14 @@ open class SDK { * @param event Event type * @param itemId Product ID */ + @Deprecated( + "This method will be removed in future versions.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "trackEventManager.track(event, itemId)" + ) + ) fun track(event: TrackEvent, itemId: String) { - track(event, Params().put(Params.Item(itemId)), null) + trackEventManager.track(event, itemId) } /** @@ -382,19 +388,14 @@ open class SDK { * @param params Parameters for the request * @param listener Callback */ - /** - * Event tracking - * - * @param event Event type - * @param params Parameters - */ + @Deprecated( + "This method will be removed in future versions.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "trackEventManager.track(event, params, listener)" + ) + ) fun track(event: TrackEvent, params: Params, listener: OnApiCallbackListener? = null) { - params.put(InternalParameter.EVENT, event.value) - if (lastRecommendedBy != null) { - params.put(lastRecommendedBy!!) - lastRecommendedBy = null - } - sendAsync(PUSH_FIELD, params.build(), listener) + trackEventManager.track(event, params, listener) } /** @@ -406,20 +407,12 @@ open class SDK { * @param value Event value * @param listener Callback */ - /** - * Tracking custom events - * - * @param event Event key - */ - /** - * Tracking custom events - * - * @param event Event key - * @param category Event category - * @param label Event label - * @param value Event value - */ - @JvmOverloads + @Deprecated( + "This method will be removed in future versions.", + level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( + "trackEventManager.track(event, category, label, value, listener)" + ) + ) fun track( event: String, category: String? = null, @@ -427,18 +420,7 @@ open class SDK { value: Int? = null, listener: OnApiCallbackListener? = null ) { - val params = Params() - params.put(InternalParameter.EVENT, event) - if (category != null) { - params.put(InternalParameter.CATEGORY, category) - } - if (label != null) { - params.put(InternalParameter.LABEL, label) - } - if (value != null) { - params.put(InternalParameter.VALUE, value) - } - sendAsync(CUSTOM_PUSH_FIELD, params.build(), listener) + trackEventManager.track(event, category, label, value, listener) } /** @@ -804,7 +786,6 @@ open class SDK { private const val TRACK_STORY_ID_FIELD = "story_id" private const val TRACK_SLIDE_ID_FIELD = "slide_id" private const val TRACK_RECEIVED = "track/received" - private const val CUSTOM_PUSH_FIELD = "push/custom" private const val SET_PROFILE_FIELD = "profile/set" private const val SEGMENT_ID_FIELD = "segment_id" private const val SEGMENT_EMAIL_FIELD = "email" @@ -823,7 +804,6 @@ open class SDK { private const val CODE_FIELD = "code" private const val INIT_FIELD = "init" private const val TYPE_FIELD = "type" - private const val PUSH_FIELD = "push" private const val ADD_FIELD = "add" private const val DID_FIELD = "did" private const val SID_FIELD = "sid" diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/CartEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/CartEntity.kt deleted file mode 100644 index 6e24f466..00000000 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/CartEntity.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.personalizatio.api.entities.cart - -import com.google.gson.annotations.SerializedName - -data class CartEntity( - @SerializedName("data") - val data: DataEntity, - @SerializedName("status") - val status: String -) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/DataEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/DataEntity.kt deleted file mode 100644 index 6d10e3b2..00000000 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/DataEntity.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.personalizatio.api.entities.cart - -import com.google.gson.annotations.SerializedName - -data class DataEntity( - @SerializedName("items") - val items: List -) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/ItemEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/ItemEntity.kt deleted file mode 100644 index c317d4aa..00000000 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/cart/ItemEntity.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.personalizatio.api.entities.cart - -import com.google.gson.annotations.SerializedName - -data class ItemEntity( - @SerializedName("quantity") - val quantity: Int, - @SerializedName("uniqid") - val uniqid: String -) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/CartManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/CartManager.kt deleted file mode 100644 index d668df91..00000000 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/CartManager.kt +++ /dev/null @@ -1,143 +0,0 @@ -package com.personalizatio.api.managers - -import com.personalizatio.Params -import com.personalizatio.api.OnApiCallbackListener -import com.personalizatio.api.entities.cart.CartEntity - -interface CartManager { - - /** - * Request a cart - * - * @param onGetCart Callback for get cart - * @param onError Callback for error - */ - fun getCart( - onGetCart: (CartEntity) -> Unit, - onError: (Int, String?) -> Unit = { _: Int, _: String? -> } - ) - - /** - * Request a cart - * - * @param listener Callback - */ - fun getCart(listener: OnApiCallbackListener) - - /** - * Add product to cart - * - * @param productId Product ID - * @param amount Amount - * @param listener Callback - */ - fun addToCart( - productId: String, - amount: Int, - listener: OnApiCallbackListener? = null - ) - - /** - * Add product to cart - * - * @param productId Product ID - * @param amount Amount - * @param price Price - * @param listener Callback - */ - fun addToCart( - productId: String, - amount: Int, - price: Double, - listener: OnApiCallbackListener? = null - ) - - /** - * Add product to cart - * - * @param productId Product ID - * @param amount Amount - * @param fashionSize Fashion size - * @param listener Callback - */ - fun addToCart( - productId: String, - amount: Int, - fashionSize: String, - listener: OnApiCallbackListener? = null - ) - - /** - * Add product to cart - * - * @param productId Product ID - * @param amount Amount - * @param price Price - * @param fashionSize Fashion size - * @param listener Callback - */ - fun addToCart( - productId: String, - amount: Int, - price: Double, - fashionSize: String, - listener: OnApiCallbackListener? = null - ) - - /** - * Add products to cart - * - * @param products Product ID and Amount pair map - * @param listener Callback - */ - fun addToCart( - products: Map, - listener: OnApiCallbackListener? = null - ) - - /** - * Add products to cart - * - * @param params Params - * @param listener Callback - */ - fun addToCart( - params: Params, - listener: OnApiCallbackListener? = null - ) - - /** - * Remove product from cart - * - * @param productId Product ID - * @param amount Amount - * @param listener Callback - */ - fun removeFromCart( - productId: String, - amount: Int, - listener: OnApiCallbackListener? = null - ) - - /** - * Remove products from cart - * - * @param products Product ID and Quantity pair map - * @param listener Callback - */ - fun removeFromCart( - products: Map, - listener: OnApiCallbackListener? = null - ) - - /** - * Remove products from cart - * - * @param params Params - * @param listener Callback - */ - fun removeFromCart( - params: Params, - listener: OnApiCallbackListener? = null - ) -} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/TrackEventManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/TrackEventManager.kt new file mode 100644 index 00000000..161d9ca4 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/TrackEventManager.kt @@ -0,0 +1,46 @@ +package com.personalizatio.api.managers + +import com.personalizatio.Params +import com.personalizatio.Params.TrackEvent +import com.personalizatio.api.OnApiCallbackListener + +interface TrackEventManager { + + /** + * Event tracking + * + * @param event Event type + * @param productId Product ID + */ + fun track(event: TrackEvent, productId: String) + + /** + * Event tracking + * + * @param event Event type + * @param params Parameters for the request + * @param listener Callback + */ + fun track( + event: TrackEvent, + params: Params, + listener: OnApiCallbackListener? = null + ) + + /** + * Tracking custom events + * + * @param event Event key + * @param category Event category + * @param label Event label + * @param value Event value + * @param listener Callback + */ + fun track( + event: String, + category: String? = null, + label: String? = null, + value: Int? = null, + listener: OnApiCallbackListener? = null + ) +} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/cart/CartManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/cart/CartManagerImpl.kt deleted file mode 100644 index c8a391c7..00000000 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/cart/CartManagerImpl.kt +++ /dev/null @@ -1,142 +0,0 @@ -package com.personalizatio.features.cart - -import com.google.gson.Gson -import com.personalizatio.Params -import com.personalizatio.SDK -import com.personalizatio.api.OnApiCallbackListener -import com.personalizatio.api.entities.cart.CartEntity -import com.personalizatio.api.managers.CartManager -import com.personalizatio.api.params.ProductItemParams -import org.json.JSONObject - -internal class CartManagerImpl(val sdk: SDK) : CartManager { - - override fun getCart( - onGetCart: (CartEntity) -> Unit, - onError: (Int, String?) -> Unit - ) { - getCart(object : OnApiCallbackListener() { - override fun onSuccess(response: JSONObject?) { - response?.let { - val cartEntity = Gson().fromJson(it.toString(), CartEntity::class.java) - onGetCart(cartEntity) - } - } - - override fun onError(code: Int, msg: String?) { - onError(code, msg) - } - }) - } - - override fun getCart( - listener: OnApiCallbackListener - ) { - sdk.getAsync(GET_CART_REQUEST, Params().build(), listener) - } - - override fun addToCart( - productId: String, - amount: Int, - listener: OnApiCallbackListener? - ) { - val params = createProductParams(productId, amount) - addToCart(params, listener) - } - - override fun addToCart( - productId: String, - amount: Int, - price: Double, - listener: OnApiCallbackListener? - ) { - val params = createProductParams(productId, amount, price = price) - addToCart(params, listener) - } - - override fun addToCart( - productId: String, - amount: Int, - fashionSize: String, - listener: OnApiCallbackListener? - ) { - val params = createProductParams(productId, amount, fashionSize = fashionSize) - addToCart(params, listener) - } - - override fun addToCart( - productId: String, - amount: Int, - price: Double, - fashionSize: String, - listener: OnApiCallbackListener? - ) { - val params = createProductParams(productId, amount, price = price, fashionSize = fashionSize) - addToCart(params, listener) - } - - override fun addToCart( - products: Map, - listener: OnApiCallbackListener? - ) { - val params = createProductsParams(products) - addToCart(params, listener) - } - - override fun addToCart( - params: Params, - listener: OnApiCallbackListener? - ) { - sdk.track(Params.TrackEvent.CART, params, listener) - } - - override fun removeFromCart( - productId: String, - amount: Int, - listener: OnApiCallbackListener? - ) { - val params = createProductParams(productId, amount) - removeFromCart(params, listener) - } - - override fun removeFromCart( - products: Map, - listener: OnApiCallbackListener? - ) { - val params = createProductsParams(products) - removeFromCart(params, listener) - } - - override fun removeFromCart( - params: Params, - listener: OnApiCallbackListener? - ) { - sdk.track(Params.TrackEvent.REMOVE_FROM_CART, params, listener) - } - - private fun createProductParams(productId: String, amount: Int, price: Double? = null, fashionSize: String? = null) : Params { - return Params().put(createProductItemParams(productId, amount, price, fashionSize)) - } - - private fun createProductsParams(products: Map) : Params { - val params = Params() - for (product in products) { - params.put(createProductItemParams(product.key, product.value)) - } - return params - } - - private fun createProductItemParams(productId: String, amount: Int, price: Double? = null, fashionSize: String? = null) : ProductItemParams { - val productItemParams = ProductItemParams(productId) - .set(ProductItemParams.PARAMETER.AMOUNT, amount) - - if(price != null) productItemParams.set(ProductItemParams.PARAMETER.PRICE, price) - if(fashionSize != null) productItemParams.set(ProductItemParams.PARAMETER.FASHION_SIZE, fashionSize) - - return productItemParams - } - - companion object { - const val GET_CART_REQUEST = "products/cart" - } -} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/track_event/TrackEventManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/track_event/TrackEventManagerImpl.kt new file mode 100644 index 00000000..10a724e8 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/track_event/TrackEventManagerImpl.kt @@ -0,0 +1,55 @@ +package com.personalizatio.features.track_event + +import com.personalizatio.Params +import com.personalizatio.Params.InternalParameter +import com.personalizatio.Params.TrackEvent +import com.personalizatio.SDK +import com.personalizatio.api.OnApiCallbackListener +import com.personalizatio.api.managers.TrackEventManager +import com.personalizatio.api.params.ProductItemParams + +internal class TrackEventManagerImpl(val sdk: SDK) : TrackEventManager { + + override fun track(event: TrackEvent, productId: String) { + track(event, Params().put(ProductItemParams(productId)), null) + } + + override fun track( + event: TrackEvent, + params: Params, + listener: OnApiCallbackListener? + ) { + params.put(InternalParameter.EVENT, event.value) + if (sdk.lastRecommendedBy != null) { + params.put(sdk.lastRecommendedBy!!) + sdk.lastRecommendedBy = null + } + sdk.sendAsync(PUSH_REQUEST, params.build(), listener) + } + + override fun track( + event: String, + category: String?, + label: String?, + value: Int?, + listener: OnApiCallbackListener? + ) { + val params = Params() + params.put(InternalParameter.EVENT, event) + if (category != null) { + params.put(InternalParameter.CATEGORY, category) + } + if (label != null) { + params.put(InternalParameter.LABEL, label) + } + if (value != null) { + params.put(InternalParameter.VALUE, value) + } + sdk.sendAsync(CUSTOM_PUSH_REQUEST, params.build(), listener) + } + + companion object { + private const val CUSTOM_PUSH_REQUEST = "push/custom" + private const val PUSH_REQUEST = "push" + } +} From 16f2647e8e96babf12d9cf526f19059716c0ac1b Mon Sep 17 00:00:00 2001 From: Maria Anchugova Date: Tue, 9 Jul 2024 17:14:31 +0500 Subject: [PATCH 06/11] refactor: renamed entities to responses --- .../api/entities/search/SearchBlankEntity.kt | 13 -------- .../api/managers/RecommendationManager.kt | 12 ++++---- .../api/managers/SearchManager.kt | 24 +++++++-------- .../product/Category.kt} | 4 +-- .../product/ImageUrlResized.kt} | 4 +-- .../product/Product.kt} | 8 ++--- .../GetExtendedRecommendationResponse.kt} | 8 ++--- .../GetRecommendationResponse.kt} | 4 +-- .../search/Brand.kt} | 4 +-- .../search/Category.kt} | 4 +-- .../search/PriceRange.kt} | 4 +-- .../search/PriceRanges.kt} | 4 +-- .../responses/search/SearchBlankResponse.kt | 13 ++++++++ .../search/SearchFullResponse.kt} | 16 +++++----- .../search/SearchInstantResponse.kt} | 10 +++---- .../search/Suggest.kt} | 4 +-- .../RecommendationManagerImpl.kt | 20 ++++++------- .../features/search/SearchManagerImpl.kt | 30 +++++++++---------- 18 files changed, 93 insertions(+), 93 deletions(-) delete mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchBlankEntity.kt rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/product/CategoryEntity.kt => responses/product/Category.kt} (85%) rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/product/ImageUrlResizedEntity.kt => responses/product/ImageUrlResized.kt} (84%) rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/product/ProductEntity.kt => responses/product/Product.kt} (88%) rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/recommendation/ExtendedRecommendationEntity.kt => responses/recommendation/GetExtendedRecommendationResponse.kt} (54%) rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/recommendation/RecommendationEntity.kt => responses/recommendation/GetRecommendationResponse.kt} (73%) rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/search/BrandEntity.kt => responses/search/Brand.kt} (68%) rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/search/CategoryEntity.kt => responses/search/Category.kt} (82%) rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/search/PriceRangeEntity.kt => responses/search/PriceRange.kt} (66%) rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/search/PriceRangesEntity.kt => responses/search/PriceRanges.kt} (66%) create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/SearchBlankResponse.kt rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/search/SearchFullEntity.kt => responses/search/SearchFullResponse.kt} (66%) rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/search/SearchInstantEntity.kt => responses/search/SearchInstantResponse.kt} (73%) rename personalizatio-sdk/src/main/kotlin/com/personalizatio/api/{entities/search/SuggestEntity.kt => responses/search/Suggest.kt} (67%) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchBlankEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchBlankEntity.kt deleted file mode 100644 index 40830c88..00000000 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchBlankEntity.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.personalizatio.api.entities.search - -import com.google.gson.annotations.SerializedName -import com.personalizatio.api.entities.product.ProductEntity - -data class SearchBlankEntity( - @SerializedName("html") - val html: String, - @SerializedName("products") - val products: List, - @SerializedName("suggests") - val suggests: List -) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt index de09e157..bad7e466 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt @@ -2,8 +2,8 @@ package com.personalizatio.api.managers import com.personalizatio.Params import com.personalizatio.api.OnApiCallbackListener -import com.personalizatio.api.entities.recommendation.ExtendedRecommendationEntity -import com.personalizatio.api.entities.recommendation.RecommendationEntity +import com.personalizatio.api.responses.recommendation.GetExtendedRecommendationResponse +import com.personalizatio.api.responses.recommendation.GetRecommendationResponse interface RecommendationManager { @@ -41,7 +41,7 @@ interface RecommendationManager { */ fun getRecommendation( recommenderCode: String, - onGetRecommendation: (RecommendationEntity) -> Unit, + onGetRecommendation: (GetRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) @@ -55,7 +55,7 @@ interface RecommendationManager { fun getRecommendation( recommenderCode: String, params: Params, - onGetRecommendation: (RecommendationEntity) -> Unit, + onGetRecommendation: (GetRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) @@ -97,7 +97,7 @@ interface RecommendationManager { */ fun getExtendedRecommendation( recommenderCode: String, - onGetExtendedRecommendation: (ExtendedRecommendationEntity) -> Unit, + onGetExtendedRecommendation: (GetExtendedRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) @@ -112,7 +112,7 @@ interface RecommendationManager { fun getExtendedRecommendation( recommenderCode: String, params: Params, - onGetExtendedRecommendation: (ExtendedRecommendationEntity) -> Unit, + onGetExtendedRecommendation: (GetExtendedRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) } diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt index 0f45904f..b85932d5 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt @@ -1,9 +1,9 @@ package com.personalizatio.api.managers import com.personalizatio.api.OnApiCallbackListener -import com.personalizatio.api.entities.search.SearchBlankEntity -import com.personalizatio.api.entities.search.SearchFullEntity -import com.personalizatio.api.entities.search.SearchInstantEntity +import com.personalizatio.api.responses.search.SearchBlankResponse +import com.personalizatio.api.responses.search.SearchFullResponse +import com.personalizatio.api.responses.search.SearchInstantResponse import com.personalizatio.api.params.SearchParams interface SearchManager { @@ -17,7 +17,7 @@ interface SearchManager { */ fun searchFull( query: String, - onGetSearchFull: (SearchFullEntity) -> Unit, + onGetSearchFull: (SearchFullResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) @@ -37,13 +37,13 @@ interface SearchManager { * * @param query Search phrase * @param searchParams SearchParams - * @param onGetSearchFull Callback for search full + * @param onSearchFull Callback for search full * @param onError Callback for error */ fun searchFull( query: String, searchParams: SearchParams, - onGetSearchFull: (SearchFullEntity) -> Unit, + onSearchFull: (SearchFullResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) @@ -64,12 +64,12 @@ interface SearchManager { * Search instant * * @param query Search phrase - * @param onGetSearchInstant Callback for search instant + * @param onSearchInstant Callback for search instant * @param onError Callback for error */ fun searchInstant( query: String, - onGetSearchInstant: (SearchInstantEntity) -> Unit, + onSearchInstant: (SearchInstantResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) @@ -89,13 +89,13 @@ interface SearchManager { * * @param query Search phrase * @param searchParams SearchParams - * @param onGetSearchInstant Callback for search instant + * @param onSearchInstant Callback for search instant * @param onError Callback for error */ fun searchInstant( query: String, searchParams: SearchParams, - onGetSearchInstant: (SearchInstantEntity) -> Unit, + onSearchInstant: (SearchInstantResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) @@ -124,11 +124,11 @@ interface SearchManager { /** * Search blank * - * @param onGetSearchBlank Callback for search blank + * @param onSearchBlank Callback for search blank * @param onError Callback for error */ fun searchBlank( - onGetSearchBlank: (SearchBlankEntity) -> Unit, + onSearchBlank: (SearchBlankResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) } diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/CategoryEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/product/Category.kt similarity index 85% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/CategoryEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/product/Category.kt index 36390dab..ab535e2e 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/CategoryEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/product/Category.kt @@ -1,8 +1,8 @@ -package com.personalizatio.api.entities.product +package com.personalizatio.api.responses.product import com.google.gson.annotations.SerializedName -data class CategoryEntity( +data class Category( @SerializedName("id") val id: String, @SerializedName("level") diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ImageUrlResizedEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/product/ImageUrlResized.kt similarity index 84% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ImageUrlResizedEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/product/ImageUrlResized.kt index 6590e646..b2997dc3 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ImageUrlResizedEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/product/ImageUrlResized.kt @@ -1,8 +1,8 @@ -package com.personalizatio.api.entities.product +package com.personalizatio.api.responses.product import com.google.gson.annotations.SerializedName -data class ImageUrlResizedEntity( +data class ImageUrlResized( @SerializedName("120") val x120: String, @SerializedName("140") diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ProductEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/product/Product.kt similarity index 88% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ProductEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/product/Product.kt index a51022dc..300acc04 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/product/ProductEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/product/Product.kt @@ -1,12 +1,12 @@ -package com.personalizatio.api.entities.product +package com.personalizatio.api.responses.product import com.google.gson.annotations.SerializedName -data class ProductEntity( +data class Product( @SerializedName("brand") val brand: String, @SerializedName("categories") - val categories: List, + val categories: List, @SerializedName("category_ids") val categoryIds: List, @SerializedName("currency") @@ -22,7 +22,7 @@ data class ProductEntity( @SerializedName("image_url_handle") val imageUrlHandle: String, @SerializedName("image_url_resized") - val imageUrlResized: ImageUrlResizedEntity, + val imageUrlResized: ImageUrlResized, @SerializedName("name") val name: String, @SerializedName("picture") diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/ExtendedRecommendationEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/recommendation/GetExtendedRecommendationResponse.kt similarity index 54% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/ExtendedRecommendationEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/recommendation/GetExtendedRecommendationResponse.kt index a9cf6be5..f4d14786 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/ExtendedRecommendationEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/recommendation/GetExtendedRecommendationResponse.kt @@ -1,15 +1,15 @@ -package com.personalizatio.api.entities.recommendation +package com.personalizatio.api.responses.recommendation import com.google.gson.annotations.SerializedName -import com.personalizatio.api.entities.product.ProductEntity +import com.personalizatio.api.responses.product.Product -data class ExtendedRecommendationEntity( +data class GetExtendedRecommendationResponse( @SerializedName("html") val html: String, @SerializedName("id") val id: Int, @SerializedName("recommends") - val products: List, + val products: List, @SerializedName("title") val title: String ) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/RecommendationEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/recommendation/GetRecommendationResponse.kt similarity index 73% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/RecommendationEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/recommendation/GetRecommendationResponse.kt index e656069c..db6b8d17 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/recommendation/RecommendationEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/recommendation/GetRecommendationResponse.kt @@ -1,8 +1,8 @@ -package com.personalizatio.api.entities.recommendation +package com.personalizatio.api.responses.recommendation import com.google.gson.annotations.SerializedName -data class RecommendationEntity( +data class GetRecommendationResponse( @SerializedName("html") val html: String, @SerializedName("id") diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/BrandEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/Brand.kt similarity index 68% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/BrandEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/Brand.kt index 947c4069..decc4152 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/BrandEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/Brand.kt @@ -1,8 +1,8 @@ -package com.personalizatio.api.entities.search +package com.personalizatio.api.responses.search import com.google.gson.annotations.SerializedName -data class BrandEntity( +data class Brand( @SerializedName("count") val count: Int, @SerializedName("name") diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/CategoryEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/Category.kt similarity index 82% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/CategoryEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/Category.kt index 31cf24db..fadbb61c 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/CategoryEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/Category.kt @@ -1,8 +1,8 @@ -package com.personalizatio.api.entities.search +package com.personalizatio.api.responses.search import com.google.gson.annotations.SerializedName -data class CategoryEntity( +data class Category( @SerializedName("count") val count: Int, @SerializedName("id") diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangeEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/PriceRange.kt similarity index 66% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangeEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/PriceRange.kt index 1d8164a2..177a84c1 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangeEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/PriceRange.kt @@ -1,8 +1,8 @@ -package com.personalizatio.api.entities.search +package com.personalizatio.api.responses.search import com.google.gson.annotations.SerializedName -data class PriceRangeEntity( +data class PriceRange( @SerializedName("max") val max: Double, @SerializedName("min") diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangesEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/PriceRanges.kt similarity index 66% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangesEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/PriceRanges.kt index 50eab3b3..1a2d911b 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/PriceRangesEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/PriceRanges.kt @@ -1,8 +1,8 @@ -package com.personalizatio.api.entities.search +package com.personalizatio.api.responses.search import com.google.gson.annotations.SerializedName -data class PriceRangesEntity( +data class PriceRanges( @SerializedName("count") val count: Int, @SerializedName("from") diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/SearchBlankResponse.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/SearchBlankResponse.kt new file mode 100644 index 00000000..b53a5231 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/SearchBlankResponse.kt @@ -0,0 +1,13 @@ +package com.personalizatio.api.responses.search + +import com.google.gson.annotations.SerializedName +import com.personalizatio.api.responses.product.Product + +data class SearchBlankResponse( + @SerializedName("html") + val html: String, + @SerializedName("products") + val products: List, + @SerializedName("suggests") + val suggests: List +) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchFullEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/SearchFullResponse.kt similarity index 66% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchFullEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/SearchFullResponse.kt index 956f1090..585071b1 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchFullEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/SearchFullResponse.kt @@ -1,13 +1,13 @@ -package com.personalizatio.api.entities.search +package com.personalizatio.api.responses.search import com.google.gson.annotations.SerializedName -import com.personalizatio.api.entities.product.ProductEntity +import com.personalizatio.api.responses.product.Product -data class SearchFullEntity( +data class SearchFullResponse( @SerializedName("brands") - val brands: List, + val brands: List, @SerializedName("categories") - val categories: List, + val categories: List, @SerializedName("clarification") val clarification: Boolean, @SerializedName("collections") @@ -17,11 +17,11 @@ data class SearchFullEntity( @SerializedName("price_median") val priceMedian: Double, @SerializedName("price_range") - val priceRange: PriceRangeEntity, + val priceRange: PriceRange, @SerializedName("price_ranges") - val priceRanges: List, + val priceRanges: List, @SerializedName("products") - val products: List, + val products: List, @SerializedName("products_total") val productsTotal: Int, @SerializedName("requests_count") diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchInstantEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/SearchInstantResponse.kt similarity index 73% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchInstantEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/SearchInstantResponse.kt index eee22c58..140b6a99 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SearchInstantEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/SearchInstantResponse.kt @@ -1,13 +1,13 @@ -package com.personalizatio.api.entities.search +package com.personalizatio.api.responses.search import com.google.gson.annotations.SerializedName -import com.personalizatio.api.entities.product.ProductEntity +import com.personalizatio.api.responses.product.Product -data class SearchInstantEntity( +data class SearchInstantResponse( @SerializedName("book_author") val bookAuthor: List, @SerializedName("categories") - val categories: List, + val categories: List, @SerializedName("clarification") val clarification: Boolean, @SerializedName("collections") @@ -15,7 +15,7 @@ data class SearchInstantEntity( @SerializedName("html") val html: String, @SerializedName("products") - val products: List, + val products: List, @SerializedName("products_total") val productsTotal: Int, @SerializedName("queries") diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SuggestEntity.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/Suggest.kt similarity index 67% rename from personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SuggestEntity.kt rename to personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/Suggest.kt index 1b676805..4fd2a02a 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/entities/search/SuggestEntity.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/responses/search/Suggest.kt @@ -1,8 +1,8 @@ -package com.personalizatio.api.entities.search +package com.personalizatio.api.responses.search import com.google.gson.annotations.SerializedName -data class SuggestEntity( +data class Suggest( @SerializedName("name") val name: String, @SerializedName("url") diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt index 51cfdd40..e2678877 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt @@ -5,15 +5,15 @@ import com.personalizatio.Params import com.personalizatio.SDK import com.personalizatio.api.OnApiCallbackListener import com.personalizatio.api.managers.RecommendationManager -import com.personalizatio.api.entities.recommendation.RecommendationEntity -import com.personalizatio.api.entities.recommendation.ExtendedRecommendationEntity +import com.personalizatio.api.responses.recommendation.GetRecommendationResponse +import com.personalizatio.api.responses.recommendation.GetExtendedRecommendationResponse import org.json.JSONObject internal class RecommendationManagerImpl(private val sdk: SDK) : RecommendationManager { override fun getRecommendation( recommenderCode: String, - onGetRecommendation: (RecommendationEntity) -> Unit, + onGetRecommendation: (GetRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit ) { val params = getRecommendationParams() @@ -28,14 +28,14 @@ internal class RecommendationManagerImpl(private val sdk: SDK) : RecommendationM override fun getRecommendation( recommenderCode: String, params: Params, - onGetRecommendation: (RecommendationEntity) -> Unit, + onGetRecommendation: (GetRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit ) { getRecommendation(recommenderCode, params, object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { response?.let { - val recommendationEntity = Gson().fromJson(it.toString(), RecommendationEntity::class.java) - onGetRecommendation(recommendationEntity) + val getRecommendationResponse = Gson().fromJson(it.toString(), GetRecommendationResponse::class.java) + onGetRecommendation(getRecommendationResponse) } } @@ -47,7 +47,7 @@ internal class RecommendationManagerImpl(private val sdk: SDK) : RecommendationM override fun getExtendedRecommendation( recommenderCode: String, - onGetExtendedRecommendation: (ExtendedRecommendationEntity) -> Unit, + onGetExtendedRecommendation: (GetExtendedRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit ) { val params = getRecommendationParams(extended = true) @@ -57,14 +57,14 @@ internal class RecommendationManagerImpl(private val sdk: SDK) : RecommendationM override fun getExtendedRecommendation( recommenderCode: String, params: Params, - onGetExtendedRecommendation: (ExtendedRecommendationEntity) -> Unit, + onGetExtendedRecommendation: (GetExtendedRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit ) { getExtendedRecommendation(recommenderCode, params, object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { response?.let { - val extendedRecommendationEntity = Gson().fromJson(it.toString(), ExtendedRecommendationEntity::class.java) - onGetExtendedRecommendation.invoke(extendedRecommendationEntity) + val getExtendedRecommendationResponse = Gson().fromJson(it.toString(), GetExtendedRecommendationResponse::class.java) + onGetExtendedRecommendation.invoke(getExtendedRecommendationResponse) } } diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt index 73bb5d49..ac5e32ed 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt @@ -6,16 +6,16 @@ import com.personalizatio.SDK import com.personalizatio.api.OnApiCallbackListener import com.personalizatio.api.params.SearchParams import com.personalizatio.api.managers.SearchManager -import com.personalizatio.api.entities.search.SearchBlankEntity -import com.personalizatio.api.entities.search.SearchFullEntity -import com.personalizatio.api.entities.search.SearchInstantEntity +import com.personalizatio.api.responses.search.SearchBlankResponse +import com.personalizatio.api.responses.search.SearchFullResponse +import com.personalizatio.api.responses.search.SearchInstantResponse import org.json.JSONObject internal class SearchManagerImpl(private val sdk: SDK) : SearchManager { override fun searchFull( query: String, - onGetSearchFull: (SearchFullEntity) -> Unit, + onGetSearchFull: (SearchFullResponse) -> Unit, onError: (Int, String?) -> Unit ) { searchFull(query, SearchParams(), onGetSearchFull, onError) @@ -24,14 +24,14 @@ internal class SearchManagerImpl(private val sdk: SDK) : SearchManager { override fun searchFull( query: String, searchParams: SearchParams, - onGetSearchFull: (SearchFullEntity) -> Unit, + onSearchFull: (SearchFullResponse) -> Unit, onError: (Int, String?) -> Unit ) { searchFull(query, searchParams, object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { response?.let { - val searchFullEntity = Gson().fromJson(it.toString(), SearchFullEntity::class.java) - onGetSearchFull(searchFullEntity) + val searchFullResponse = Gson().fromJson(it.toString(), SearchFullResponse::class.java) + onSearchFull(searchFullResponse) } } @@ -55,23 +55,23 @@ internal class SearchManagerImpl(private val sdk: SDK) : SearchManager { override fun searchInstant( query: String, - onGetSearchInstant: (SearchInstantEntity) -> Unit, + onSearchInstant: (SearchInstantResponse) -> Unit, onError: (Int, String?) -> Unit ) { - searchInstant(query, SearchParams(), onGetSearchInstant, onError) + searchInstant(query, SearchParams(), onSearchInstant, onError) } override fun searchInstant( query: String, searchParams: SearchParams, - onGetSearchInstant: (SearchInstantEntity) -> Unit, + onSearchInstant: (SearchInstantResponse) -> Unit, onError: (Int, String?) -> Unit ) { searchInstant(query, searchParams, object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { response?.let { - val searchInstantEntity = Gson().fromJson(it.toString(), SearchInstantEntity::class.java) - onGetSearchInstant(searchInstantEntity) + val searchInstantResponse = Gson().fromJson(it.toString(), SearchInstantResponse::class.java) + onSearchInstant(searchInstantResponse) } } @@ -98,14 +98,14 @@ internal class SearchManagerImpl(private val sdk: SDK) : SearchManager { } override fun searchBlank( - onGetSearchBlank: (SearchBlankEntity) -> Unit, + onSearchBlank: (SearchBlankResponse) -> Unit, onError: (Int, String?) -> Unit ) { searchBlank(object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { response?.let { - val searchBlankEntity = Gson().fromJson(it.toString(), SearchBlankEntity::class.java) - onGetSearchBlank(searchBlankEntity) + val searchBlankResponse = Gson().fromJson(it.toString(), SearchBlankResponse::class.java) + onSearchBlank(searchBlankResponse) } } From 279d61807838804f0cd00903903d5db52251bdcc Mon Sep 17 00:00:00 2001 From: Maria Anchugova Date: Wed, 10 Jul 2024 15:30:33 +0500 Subject: [PATCH 07/11] refactor: made recommendation methods universal --- .../com/personalizatio/AbstractParams.kt | 15 +++ .../src/main/kotlin/com/personalizatio/SDK.kt | 6 +- .../api/managers/RecommendationManager.kt | 95 ++++--------------- .../GetRecommendationParameter.kt | 7 -- .../RecommendationManagerImpl.kt | 54 ++++------- 5 files changed, 55 insertions(+), 122 deletions(-) delete mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/GetRecommendationParameter.kt diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/AbstractParams.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/AbstractParams.kt index 259d156c..bd4dc394 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/AbstractParams.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/AbstractParams.kt @@ -59,6 +59,21 @@ abstract class AbstractParams

> { return this as P } + internal fun put(param: String, value: String): P { + params.put(param, value) + return this as P + } + + internal fun put(param: String, value: Int): P { + params.put(param, value) + return this as P + } + + internal fun put(param: String, value: Boolean): P { + params.put(param, value) + return this as P + } + fun build(): JSONObject { return params } diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt index e13a6cab..37144eab 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt @@ -341,11 +341,11 @@ open class SDK { @Deprecated( "This method will be removed in future versions. Use recommendationManager.", level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( - "recommendationManager.getRecommendation(recommender_code, listener)" + "recommendationManager.getRecommendation(recommender_code, ...)" ) ) fun recommend(recommender_code: String, listener: OnApiCallbackListener) { - recommendationManager.getRecommendation(recommender_code, listener) + recommendationManager.getRecommendation(recommender_code, Params(), listener) } /** @@ -358,7 +358,7 @@ open class SDK { @Deprecated( "This method will be removed in future versions. Use recommendationManager.", level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( - "recommendationManager.getRecommendation(code, params, listener)" + "recommendationManager.getRecommendation(code, ...)" ) ) fun recommend(code: String, params: Params, listener: OnApiCallbackListener) { diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt index bad7e466..5de081ce 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt @@ -11,50 +11,15 @@ interface RecommendationManager { * Request a dynamic block of recommendations * * @param recommenderCode Recommendation block code - * @param listener Callback - * - */ - fun getRecommendation( - recommenderCode: String, - listener: OnApiCallbackListener - ) - - /** - * Request a dynamic block of recommendations - * - * @param recommenderCode Code of the dynamic block of recommendations - * @param params Parameters for the request - * @param listener Callback - */ - fun getRecommendation( - recommenderCode: String, - params: Params, - listener: OnApiCallbackListener - ) - - /** - * Request a dynamic block of recommendations - * - * @param recommenderCode Recommendation block code - * @param onGetRecommendation Callback for get recommendation - * @param onError Callback for error - */ - fun getRecommendation( - recommenderCode: String, - onGetRecommendation: (GetRecommendationResponse) -> Unit, - onError: (Int, String?) -> Unit = { _: Int, _: String? -> } - ) - - /** - * Request a dynamic block of recommendations - * - * @param recommenderCode Recommendation block code + * @param imageSize Image size (px) + * @param withLocations With locations * @param onGetRecommendation Callback for get recommendation * @param onError Callback for error */ fun getRecommendation( recommenderCode: String, - params: Params, + imageSize: Int? = null, + withLocations: Boolean? = null, onGetRecommendation: (GetRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) @@ -62,57 +27,35 @@ interface RecommendationManager { /** * Request a dynamic block of recommendations with all information about recommended products * - * - * @param recommenderCode Recommendation block code - * @param listener Callback - * - */ - fun getExtendedRecommendation( - recommenderCode: String, - listener: OnApiCallbackListener - ) - - /** - * Request a dynamic block of recommendations with all information about recommended products - * - * - * @param recommenderCode Recommendation block code - * @param params Params - * @param listener Callback - * - */ - fun getExtendedRecommendation( - recommenderCode: String, - params: Params, - listener: OnApiCallbackListener - ) - - /** - * Request a dynamic block of recommendations with all information about recommended products - * - * * @param recommenderCode Recommendation block code * @param onGetExtendedRecommendation Callback for get extended recommendation + * @param imageSize Image size (px) + * @param withLocations With locations * @param onError Callback for error */ fun getExtendedRecommendation( recommenderCode: String, + imageSize: Int? = null, + withLocations: Boolean? = null, onGetExtendedRecommendation: (GetExtendedRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) /** - * Request a dynamic block of recommendations with all information about recommended products - * + * Request a dynamic block of recommendations * - * @param recommenderCode Recommendation block code - * @param onGetExtendedRecommendation Callback for get extended recommendation - * @param onError Callback for error + * @param recommenderCode Code of the dynamic block of recommendations + * @param params Parameters for the request + * @param listener Callback */ - fun getExtendedRecommendation( + @Deprecated( + "This method will be removed in future versions.", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith("getRecommendation(...) or getExtendedRecommendation(...)") + ) + fun getRecommendation( recommenderCode: String, params: Params, - onGetExtendedRecommendation: (GetExtendedRecommendationResponse) -> Unit, - onError: (Int, String?) -> Unit = { _: Int, _: String? -> } + listener: OnApiCallbackListener ) } diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/GetRecommendationParameter.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/GetRecommendationParameter.kt deleted file mode 100644 index 3242c12e..00000000 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/GetRecommendationParameter.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.personalizatio.features.recommendation - -import com.personalizatio.AbstractParams.ParamInterface - -internal enum class GetRecommendationParameter(override val value: String) : ParamInterface { - EXTENDED("extended") -} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt index e2678877..0ef3103b 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt @@ -13,24 +13,13 @@ internal class RecommendationManagerImpl(private val sdk: SDK) : RecommendationM override fun getRecommendation( recommenderCode: String, + imageSize: Int?, + withLocations: Boolean?, onGetRecommendation: (GetRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit ) { - val params = getRecommendationParams() - getRecommendation(recommenderCode, params, onGetRecommendation, onError) - } - - override fun getRecommendation(recommenderCode: String, listener: OnApiCallbackListener) { - val params = getRecommendationParams() - getRecommendation(recommenderCode, params, listener) - } + val params = getRecommendationParams(false, imageSize, withLocations) - override fun getRecommendation( - recommenderCode: String, - params: Params, - onGetRecommendation: (GetRecommendationResponse) -> Unit, - onError: (Int, String?) -> Unit - ) { getRecommendation(recommenderCode, params, object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { response?.let { @@ -47,20 +36,14 @@ internal class RecommendationManagerImpl(private val sdk: SDK) : RecommendationM override fun getExtendedRecommendation( recommenderCode: String, + imageSize: Int?, + withLocations: Boolean?, onGetExtendedRecommendation: (GetExtendedRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit ) { - val params = getRecommendationParams(extended = true) - getExtendedRecommendation(recommenderCode, params, onGetExtendedRecommendation, onError) - } + val params = getRecommendationParams(true, imageSize, withLocations) - override fun getExtendedRecommendation( - recommenderCode: String, - params: Params, - onGetExtendedRecommendation: (GetExtendedRecommendationResponse) -> Unit, - onError: (Int, String?) -> Unit - ) { - getExtendedRecommendation(recommenderCode, params, object : OnApiCallbackListener() { + getRecommendation(recommenderCode, params, object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { response?.let { val getExtendedRecommendationResponse = Gson().fromJson(it.toString(), GetExtendedRecommendationResponse::class.java) @@ -74,29 +57,28 @@ internal class RecommendationManagerImpl(private val sdk: SDK) : RecommendationM }) } - override fun getExtendedRecommendation(recommenderCode: String, listener: OnApiCallbackListener) { - val params = getRecommendationParams(extended = true) - getRecommendation(recommenderCode, params, listener) - } - - override fun getExtendedRecommendation(recommenderCode: String, params: Params, listener: OnApiCallbackListener) { - params.put(GetRecommendationParameter.EXTENDED, true) - getRecommendation(recommenderCode, params, listener) - } - override fun getRecommendation(recommenderCode: String, params: Params, listener: OnApiCallbackListener) { sdk.getAsync("$GET_RECOMMENDATION_REQUEST/$recommenderCode", params.build(), listener) } - private fun getRecommendationParams(extended: Boolean? = null) : Params { + private fun getRecommendationParams( + extended: Boolean?, + imageSize: Int?, + withLocations: Boolean?) : Params { val params = Params() - if(extended != null) params.put(GetRecommendationParameter.EXTENDED, extended) + if(extended != null) params.put(EXTENDED_PARAMETER, extended) + if(imageSize != null) params.put(IMAGE_SIZE_PARAMETER, imageSize) + if(withLocations != null) params.put(WITH_LOCATIONS_PARAMETER, withLocations) return params } companion object { const val GET_RECOMMENDATION_REQUEST = "recommend" + + const val EXTENDED_PARAMETER = "extended" + const val IMAGE_SIZE_PARAMETER = "resize_image" + const val WITH_LOCATIONS_PARAMETER = "with_locations" } } From 05cb0c2bc4e9431eab11e9a4b25d4b0edb4af541 Mon Sep 17 00:00:00 2001 From: Maria Anchugova Date: Wed, 10 Jul 2024 16:20:53 +0500 Subject: [PATCH 08/11] refactor: made search methods universal --- .../src/main/kotlin/com/personalizatio/SDK.kt | 12 +-- .../api/managers/SearchManager.kt | 90 +------------------ .../personalizatio/api/params/SearchParams.kt | 5 -- .../features/search/SearchManagerImpl.kt | 74 +++++---------- .../features/search/SearchParameter.kt | 8 -- 5 files changed, 29 insertions(+), 160 deletions(-) delete mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchParameter.kt diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt index 37144eab..3f55f080 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt @@ -265,9 +265,9 @@ open class SDK { * @param listener Callback */ @Deprecated( - "This class will be removed in future versions.", + "This class will be removed in future versions. Use searchManager.", level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( - "searchManager.searchInstant(query, listener) or searchManager.searchFull(query, listener)" + "searchManager.searchInstant(...) or searchManager.searchFull(...)" ) ) fun search(query: String, type: SearchParams.TYPE, listener: OnApiCallbackListener) { @@ -283,9 +283,9 @@ open class SDK { * @param listener v */ @Deprecated( - "This class will be removed in future versions.", + "This class will be removed in future versions. Use searchManager.", level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( - "searchManager.searchInstant(query, params, listener) or searchManager.searchFull(query, params, listener)" + "searchManager.searchInstant(...) or searchManager.searchFull(...)" ) ) fun search( @@ -304,9 +304,9 @@ open class SDK { } @Deprecated( - "This class will be removed in future versions.", + "This class will be removed in future versions. Use searchManager.", level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( - "searchManager.searchBlank(listener)" + "searchManager.searchBlank(...)" ) ) fun searchBlank(listener: OnApiCallbackListener) { diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt index b85932d5..a1090c0b 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/SearchManager.kt @@ -1,6 +1,5 @@ package com.personalizatio.api.managers -import com.personalizatio.api.OnApiCallbackListener import com.personalizatio.api.responses.search.SearchBlankResponse import com.personalizatio.api.responses.search.SearchFullResponse import com.personalizatio.api.responses.search.SearchInstantResponse @@ -8,30 +7,6 @@ import com.personalizatio.api.params.SearchParams interface SearchManager { - /** - * Search full - * - * @param query Search phrase - * @param onGetSearchFull Callback for search full - * @param onError Callback for error - */ - fun searchFull( - query: String, - onGetSearchFull: (SearchFullResponse) -> Unit, - onError: (Int, String?) -> Unit = { _: Int, _: String? -> } - ) - - /** - * Search full - * - * @param query Search phrase - * @param listener Callback - */ - fun searchFull( - query: String, - listener: OnApiCallbackListener - ) - /** * Search full * @@ -42,85 +17,26 @@ interface SearchManager { */ fun searchFull( query: String, - searchParams: SearchParams, + searchParams: SearchParams = SearchParams(), onSearchFull: (SearchFullResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) - /** - * Search full - * - * @param query Search phrase - * @param searchParams SearchParams - * @param listener Callback - */ - fun searchFull( - query: String, - searchParams: SearchParams, - listener: OnApiCallbackListener - ) - - /** - * Search instant - * - * @param query Search phrase - * @param onSearchInstant Callback for search instant - * @param onError Callback for error - */ - fun searchInstant( - query: String, - onSearchInstant: (SearchInstantResponse) -> Unit, - onError: (Int, String?) -> Unit = { _: Int, _: String? -> } - ) - - /** - * Search instant - * - * @param query Search phrase - * @param listener Callback - */ - fun searchInstant( - query: String, - listener: OnApiCallbackListener - ) - /** * Search instant * * @param query Search phrase - * @param searchParams SearchParams + * @param locations Comma separated list of locations IDs * @param onSearchInstant Callback for search instant * @param onError Callback for error */ fun searchInstant( query: String, - searchParams: SearchParams, + locations: String? = null, onSearchInstant: (SearchInstantResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) - /** - * Search instant - * - * @param query Search phrase - * @param searchParams SearchParams - * @param listener Callback - */ - fun searchInstant( - query: String, - searchParams: SearchParams, - listener: OnApiCallbackListener - ) - - /** - * Search blank - * - * @param listener Callback - */ - fun searchBlank( - listener: OnApiCallbackListener - ) - /** * Search blank * diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/SearchParams.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/SearchParams.kt index 05b3e186..6a1d7b41 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/SearchParams.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/params/SearchParams.kt @@ -27,11 +27,6 @@ class SearchParams : AbstractParams() { NO_CLARIFICATION("no_clarification") } - enum class TYPE(var value: String) { - INSTANT("instant_search"), - FULL("full_search") - } - class SearchFilters { private val filters = HashMap>() diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt index ac5e32ed..77d03971 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchManagerImpl.kt @@ -13,21 +13,13 @@ import org.json.JSONObject internal class SearchManagerImpl(private val sdk: SDK) : SearchManager { - override fun searchFull( - query: String, - onGetSearchFull: (SearchFullResponse) -> Unit, - onError: (Int, String?) -> Unit - ) { - searchFull(query, SearchParams(), onGetSearchFull, onError) - } - override fun searchFull( query: String, searchParams: SearchParams, onSearchFull: (SearchFullResponse) -> Unit, onError: (Int, String?) -> Unit ) { - searchFull(query, searchParams, object : OnApiCallbackListener() { + search(query, TYPE.FULL, searchParams, object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { response?.let { val searchFullResponse = Gson().fromJson(it.toString(), SearchFullResponse::class.java) @@ -41,33 +33,17 @@ internal class SearchManagerImpl(private val sdk: SDK) : SearchManager { }) } - override fun searchFull(query: String, listener: OnApiCallbackListener) { - searchFull(query, SearchParams(), listener) - } - - override fun searchFull( - query: String, - searchParams: SearchParams, - listener: OnApiCallbackListener - ) { - search(query, SearchParams.TYPE.FULL, searchParams, listener) - } - override fun searchInstant( query: String, + locations: String?, onSearchInstant: (SearchInstantResponse) -> Unit, onError: (Int, String?) -> Unit ) { - searchInstant(query, SearchParams(), onSearchInstant, onError) - } + val searchParams = SearchParams() - override fun searchInstant( - query: String, - searchParams: SearchParams, - onSearchInstant: (SearchInstantResponse) -> Unit, - onError: (Int, String?) -> Unit - ) { - searchInstant(query, searchParams, object : OnApiCallbackListener() { + if(locations != null) searchParams.put(LOCATIONS_PARAMETER, locations) + + search(query, TYPE.INSTANT, searchParams, object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { response?.let { val searchInstantResponse = Gson().fromJson(it.toString(), SearchInstantResponse::class.java) @@ -81,27 +57,11 @@ internal class SearchManagerImpl(private val sdk: SDK) : SearchManager { }) } - override fun searchInstant( - query: String, - listener: OnApiCallbackListener - ) { - searchInstant(query, SearchParams(), listener) - } - - - override fun searchInstant( - query: String, - searchParams: SearchParams, - listener: OnApiCallbackListener - ) { - search(query, SearchParams.TYPE.INSTANT, searchParams, listener) - } - override fun searchBlank( onSearchBlank: (SearchBlankResponse) -> Unit, onError: (Int, String?) -> Unit ) { - searchBlank(object : OnApiCallbackListener() { + sdk.getAsync(BLANK_SEARCH_REQUEST, Params().build(), object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { response?.let { val searchBlankResponse = Gson().fromJson(it.toString(), SearchBlankResponse::class.java) @@ -115,24 +75,30 @@ internal class SearchManagerImpl(private val sdk: SDK) : SearchManager { }) } - override fun searchBlank(listener: OnApiCallbackListener) { - sdk.getAsync(BLANK_SEARCH_REQUEST, Params().build(), listener) - } - private fun search( query: String, - type: SearchParams.TYPE, + type: TYPE, params: SearchParams, listener: OnApiCallbackListener ) { params - .put(SearchParameter.SEARCH_TYPE, type.value) - .put(SearchParameter.SEARCH_QUERY, query) + .put(TYPE_PARAMETER, type.value) + .put(QUERY_PARAMETER, query) + sdk.getAsync(SEARCH_REQUEST, params.build(), listener) } companion object { private const val SEARCH_REQUEST = "search" private const val BLANK_SEARCH_REQUEST = "search/blank" + + private const val TYPE_PARAMETER = "type" + private const val QUERY_PARAMETER = "search_query" + private const val LOCATIONS_PARAMETER = "locations" + } + + private enum class TYPE(var value: String) { + INSTANT("instant_search"), + FULL("full_search") } } diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchParameter.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchParameter.kt deleted file mode 100644 index bc792680..00000000 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/search/SearchParameter.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.personalizatio.features.search - -import com.personalizatio.AbstractParams.ParamInterface - -internal enum class SearchParameter(override val value: String) : ParamInterface { - SEARCH_TYPE("type"), - SEARCH_QUERY("search_query") -} \ No newline at end of file From 49e645b334cda02f1c7b067edeac3a6422841aa0 Mon Sep 17 00:00:00 2001 From: Maria Anchugova Date: Wed, 10 Jul 2024 17:58:27 +0500 Subject: [PATCH 09/11] refactor: made custom track event methods universal --- .../src/main/kotlin/com/personalizatio/SDK.kt | 7 ++-- .../api/managers/TrackEventManager.kt | 10 ++++- .../track_event/TrackEventManagerImpl.kt | 37 ++++++++++++------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt index 3f55f080..31919b03 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt @@ -409,9 +409,8 @@ open class SDK { */ @Deprecated( "This method will be removed in future versions.", - level = DeprecationLevel.WARNING, replaceWith = ReplaceWith( - "trackEventManager.track(event, category, label, value, listener)" - ) + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith("trackEventManager.customTrack(event, category, label, value = value, listener = listener)") ) fun track( event: String, @@ -420,7 +419,7 @@ open class SDK { value: Int? = null, listener: OnApiCallbackListener? = null ) { - trackEventManager.track(event, category, label, value, listener) + trackEventManager.customTrack(event, category, label, value = value, listener = listener) } /** diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/TrackEventManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/TrackEventManager.kt index 161d9ca4..8182e915 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/TrackEventManager.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/TrackEventManager.kt @@ -31,13 +31,21 @@ interface TrackEventManager { * Tracking custom events * * @param event Event key + * @param email Email + * @param phone Phone + * @param loyaltyId Loyalty ID + * @param externalId External ID * @param category Event category * @param label Event label * @param value Event value * @param listener Callback */ - fun track( + fun customTrack( event: String, + email: String? = null, + phone: String? = null, + loyaltyId: String? = null, + externalId: String? = null, category: String? = null, label: String? = null, value: Int? = null, diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/track_event/TrackEventManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/track_event/TrackEventManagerImpl.kt index 10a724e8..e574e7d7 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/track_event/TrackEventManagerImpl.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/track_event/TrackEventManagerImpl.kt @@ -1,7 +1,6 @@ package com.personalizatio.features.track_event import com.personalizatio.Params -import com.personalizatio.Params.InternalParameter import com.personalizatio.Params.TrackEvent import com.personalizatio.SDK import com.personalizatio.api.OnApiCallbackListener @@ -19,7 +18,7 @@ internal class TrackEventManagerImpl(val sdk: SDK) : TrackEventManager { params: Params, listener: OnApiCallbackListener? ) { - params.put(InternalParameter.EVENT, event.value) + params.put(EVENT_PARAMETER, event.value) if (sdk.lastRecommendedBy != null) { params.put(sdk.lastRecommendedBy!!) sdk.lastRecommendedBy = null @@ -27,29 +26,41 @@ internal class TrackEventManagerImpl(val sdk: SDK) : TrackEventManager { sdk.sendAsync(PUSH_REQUEST, params.build(), listener) } - override fun track( + override fun customTrack( event: String, + email: String?, + phone: String?, + loyaltyId: String?, + externalId: String?, category: String?, label: String?, value: Int?, listener: OnApiCallbackListener? ) { val params = Params() - params.put(InternalParameter.EVENT, event) - if (category != null) { - params.put(InternalParameter.CATEGORY, category) - } - if (label != null) { - params.put(InternalParameter.LABEL, label) - } - if (value != null) { - params.put(InternalParameter.VALUE, value) - } + params.put(EVENT_PARAMETER, event) + if (email != null) { params.put(EMAIL_PARAMETER, email) } + if (phone != null) { params.put(PHONE_PARAMETER, phone) } + if (loyaltyId != null) { params.put(LOYALTY_ID_PARAMETER, loyaltyId) } + if (externalId != null) { params.put(EXTERNAL_ID_PARAMETER, externalId) } + if (category != null) { params.put(CATEGORY_PARAMETER, category) } + if (label != null) { params.put(LABEL_PARAMETER, label) } + if (value != null) { params.put(VALUE_PARAMETER, value) } + sdk.sendAsync(CUSTOM_PUSH_REQUEST, params.build(), listener) } companion object { private const val CUSTOM_PUSH_REQUEST = "push/custom" private const val PUSH_REQUEST = "push" + + private const val EVENT_PARAMETER = "event" + private const val EMAIL_PARAMETER = "email" + private const val PHONE_PARAMETER = "phone" + private const val LOYALTY_ID_PARAMETER = "loyalty_id" + private const val EXTERNAL_ID_PARAMETER = "external_id" + private const val CATEGORY_PARAMETER = "category" + private const val LABEL_PARAMETER = "label" + private const val VALUE_PARAMETER = "value" } } From e4bd58405b8de6a1290767dc3c6e4daf5489534d Mon Sep 17 00:00:00 2001 From: Maria Anchugova Date: Wed, 10 Jul 2024 21:09:48 +0500 Subject: [PATCH 10/11] refactor: implemented network manager --- .../com/personalizatio/RegisterManager.kt | 3 +- .../src/main/kotlin/com/personalizatio/SDK.kt | 57 ++--- .../main/kotlin/com/personalizatio/api/Api.kt | 17 -- .../kotlin/com/personalizatio/api/ApiImpl.kt | 162 ------------- .../com/personalizatio/api/ApiMethod.kt | 4 + .../api/managers/NetworkManager.kt | 31 +++ .../network/NetworkManagerImpl.kt | 218 ++++++++++++++++++ .../personalizatio/network/NetworkMethod.kt | 8 + 8 files changed, 284 insertions(+), 216 deletions(-) delete mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/Api.kt delete mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/ApiImpl.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/NetworkManager.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/network/NetworkManagerImpl.kt create mode 100644 personalizatio-sdk/src/main/kotlin/com/personalizatio/network/NetworkMethod.kt diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/RegisterManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/RegisterManager.kt index a34a74de..2f6e119d 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/RegisterManager.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/RegisterManager.kt @@ -7,7 +7,6 @@ import com.google.android.gms.tasks.Task import com.google.firebase.messaging.FirebaseMessaging import com.personalizatio.SDK.Companion.TAG import com.personalizatio.SDK.Companion.debug -import com.personalizatio.api.ApiMethod import com.personalizatio.api.OnApiCallbackListener import com.personalizatio.utils.PreferencesUtils import kotlinx.coroutines.CoroutineScope @@ -89,7 +88,7 @@ class RegisterManager(val sdk: SDK) { try { val params = JSONObject() params.put("tz", (TimeZone.getDefault().rawOffset / 3600000.0).toInt().toString()) - sdk.send(ApiMethod.GET("init"), params, object : OnApiCallbackListener() { + sdk.networkManager.get("init", params, object : OnApiCallbackListener() { @Volatile private var attempt = 0 diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt index 31919b03..25844485 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/SDK.kt @@ -9,15 +9,15 @@ import com.google.firebase.messaging.RemoteMessage import com.personalizatio.Params.InternalParameter import com.personalizatio.Params.RecommendedBy import com.personalizatio.Params.TrackEvent -import com.personalizatio.api.Api -import com.personalizatio.api.ApiMethod import com.personalizatio.api.OnApiCallbackListener +import com.personalizatio.api.managers.NetworkManager import com.personalizatio.api.managers.TrackEventManager import com.personalizatio.api.managers.RecommendationManager import com.personalizatio.api.managers.SearchManager import com.personalizatio.features.track_event.TrackEventManagerImpl import com.personalizatio.features.recommendation.RecommendationManagerImpl import com.personalizatio.features.search.SearchManagerImpl +import com.personalizatio.network.NetworkManagerImpl import com.personalizatio.notification.NotificationHandler import com.personalizatio.notification.NotificationHelper import com.personalizatio.notifications.Source @@ -39,7 +39,6 @@ open class SDK { private lateinit var source: Source private lateinit var shopId: String private lateinit var stream: String - private lateinit var api: Api private val queue: MutableList = Collections.synchronizedList(ArrayList()) private lateinit var notificationHandler: NotificationHandler @@ -48,9 +47,9 @@ open class SDK { private var seance: String? = null private var search: Search? = null private var did: String? = null - private var initialized = false + var initialized = false - private val registerManager: RegisterManager by lazy { + val registerManager: RegisterManager by lazy { RegisterManager(this) } @@ -70,6 +69,8 @@ open class SDK { SearchManagerImpl(this) } + lateinit var networkManager: NetworkManager + /** * @param shopId Shop key */ @@ -84,8 +85,6 @@ open class SDK { notificationId: String, autoSendPushToken: Boolean = true ) { - this.api = Api.getApi(apiUrl) - this.context = context this.shopId = shopId this.stream = stream @@ -104,6 +103,7 @@ open class SDK { notificationHandler = NotificationHandler(context) { prefs() } notificationHandler.createNotificationChannel() + networkManager = NetworkManagerImpl(this, apiUrl, shopId, seance, segment, stream, source) registerManager.initialize(autoSendPushToken) } @@ -163,7 +163,7 @@ open class SDK { /** * Update last activity time */ - private fun updateSidActivity() { + fun updateSidActivity() { val edit = prefs().edit() edit.putString(SID_FIELD, seance) edit.putLong(SID_LAST_ACT_FIELD, System.currentTimeMillis()) @@ -208,46 +208,33 @@ open class SDK { fun notificationClicked(extras: Bundle?) { notificationHandler.notificationClicked( extras = extras, - sendAsync = { method, params -> sendAsync(method, params) }, + sendAsync = { method, params -> networkManager.postAsync(method, params) }, source = source ) } - /** - * Direct query execution - */ - internal fun send(apiMethod: ApiMethod, params: JSONObject, listener: OnApiCallbackListener?) { - updateSidActivity() - - api.send(apiMethod, params, listener, shopId, registerManager.did, seance, segment, stream, source) - } - - private fun sendAsync(method: String, params: JSONObject) { - sendAsync(method, params, null) - } - /** * Asynchronous execution of a request if did is not specified and initialization has not been completed */ + @Deprecated( + "This method will be removed in future versions.", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith("networkManager.postAsync(method, params, listener)") + ) fun sendAsync(method: String, params: JSONObject, listener: OnApiCallbackListener?) { - val thread = Thread { send(ApiMethod.POST(method), params, listener) } - if (registerManager.did != null && initialized) { - thread.start() - } else { - queue.add(thread) - } + networkManager.postAsync(method, params, listener) } /** * Asynchronous execution of a request if did is not specified and initialization has not been completed */ + @Deprecated( + "This method will be removed in future versions.", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith("networkManager.getAsync(method, params, listener)") + ) fun getAsync(method: String, params: JSONObject, listener: OnApiCallbackListener?) { - val thread = Thread { send(ApiMethod.GET(method), params, listener) } - if (registerManager.did != null && initialized) { - thread.start() - } else { - queue.add(thread) - } + networkManager.getAsync(method, params, listener) } /** @@ -758,7 +745,7 @@ open class SDK { params.put(CODE_FIELD, id) } if (params.length() > 0) { - sendAsync(TRACK_RECEIVED, params) + networkManager.postAsync(TRACK_RECEIVED, params) } } catch (e: JSONException) { Log.e(TAG, e.message, e) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/Api.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/Api.kt deleted file mode 100644 index 1ac7a3ac..00000000 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/Api.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.personalizatio.api - -import com.personalizatio.notifications.Source -import org.json.JSONObject - -interface Api { - - fun send(apiMethod: ApiMethod, params: JSONObject, listener: OnApiCallbackListener?, - shopId: String, did: String?, seance: String?, segment: String, stream: String, source: Source) - - companion object { - - fun getApi(baseUrl: String): Api { - return ApiImpl(baseUrl) - } - } -} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/ApiImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/ApiImpl.kt deleted file mode 100644 index 1cd833db..00000000 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/ApiImpl.kt +++ /dev/null @@ -1,162 +0,0 @@ -package com.personalizatio.api - -import android.net.Uri -import com.personalizatio.SDK -import com.personalizatio.notifications.Source -import org.json.JSONArray -import org.json.JSONException -import org.json.JSONObject -import org.json.JSONTokener -import java.io.BufferedReader -import java.io.BufferedWriter -import java.io.IOException -import java.io.InputStream -import java.io.InputStreamReader -import java.io.OutputStreamWriter -import java.net.ConnectException -import java.net.HttpURLConnection -import java.net.URL -import java.nio.charset.StandardCharsets - -class ApiImpl( - private val baseUrl: String -): Api { - - /** - * Прямое выполенение запроса - */ - override fun send(apiMethod: ApiMethod, params: JSONObject, listener: OnApiCallbackListener?, - shopId: String, did: String?, seance: String?, segment: String, stream: String, source: Source - ) { - try { - params.put("shop_id", shopId) - if (did != null) { - params.put("did", did) - } - if (seance != null) { - params.put("seance", seance) - params.put("sid", seance) - } - params.put("segment", segment) - params.put("stream", stream) - - val sourceJson = source.getJsonObject(sourceTimeDuration) - if (sourceJson != null) { - params.put("source", sourceJson) - } - - send(apiMethod, params, listener) - } catch (e: JSONException) { - SDK.error(e.message, e) - } - } - - private fun send(apiMethod: ApiMethod, params: JSONObject, listener: OnApiCallbackListener?) { - val thread = Thread { - try { - val buildUri = build(apiMethod, params) - val url = getUrl(apiMethod, buildUri) - val connection = getConnection(apiMethod, url, params) - - connection.connect() - - if (apiMethod is ApiMethod.POST) { - SDK.debug(connection.responseCode.toString() + ": " + apiMethod.type + " " + url + " with body: " + params) - } else { - SDK.debug(connection.responseCode.toString() + ": " + apiMethod.type + " " + buildUri.toString()) - } - - if (listener != null && connection.responseCode == HttpURLConnection.HTTP_OK) { - val json = JSONTokener(readStream(connection.inputStream)).nextValue() - if (json is JSONObject) { - listener.onSuccess(json) - } else if (json is JSONArray) { - listener.onSuccess(json) - } - } - - if (connection.responseCode >= 400) { - val error = readStream(connection.errorStream) - SDK.error(error) - listener?.onError(connection.responseCode, error) - } - - connection.disconnect() - } catch (e: ConnectException) { - SDK.error(e.message) - listener?.onError(504, e.message) - } catch (e: Exception) { - SDK.error(e.message, e) - listener?.onError(-1, e.message) - } - } - - thread.start() - } - - private fun build(apiMethod: ApiMethod, params: JSONObject): Uri { - val builder = Uri.parse(baseUrl + apiMethod.method).buildUpon() - - val it = params.keys() - while (it.hasNext()) { - val key = it.next() - builder.appendQueryParameter(key, params.getString(key)) - } - - return builder.build() - } - - private fun getUrl(apiMethod: ApiMethod, buildUri: Uri) : URL { - return if (apiMethod is ApiMethod.POST) { - URL(baseUrl + apiMethod.method) - } else { - URL(buildUri.toString()) - } - } - - private fun getConnection(apiMethod: ApiMethod, url: URL, params: JSONObject) : HttpURLConnection { - val connection = url.openConnection() as HttpURLConnection - connection.setRequestProperty("User-Agent", SDK.userAgent()) - connection.requestMethod = apiMethod.type - connection.connectTimeout = 5000 - - if (apiMethod is ApiMethod.POST) { - connection.setRequestProperty("Content-Type", "application/json") - connection.doOutput = true - connection.doInput = true - val os = BufferedWriter(OutputStreamWriter(connection.outputStream, StandardCharsets.UTF_8)) - os.write(params.toString()) - os.flush() - os.close() - } - - return connection - } - - private fun readStream(inputStream: InputStream): String { - var reader: BufferedReader? = null - val response = StringBuffer() - try { - reader = BufferedReader(InputStreamReader(inputStream)) - var line: String? - while ((reader.readLine().also { line = it }) != null) { - response.append(line) - } - } catch (e: IOException) { - e.printStackTrace() - } finally { - if (reader != null) { - try { - reader.close() - } catch (e: IOException) { - e.printStackTrace() - } - } - } - return response.toString() - } - - companion object { - private var sourceTimeDuration = 172800 * 1000 // 2 days - } -} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/ApiMethod.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/ApiMethod.kt index 845a9a3d..2b14b684 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/ApiMethod.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/ApiMethod.kt @@ -1,5 +1,9 @@ package com.personalizatio.api +@Deprecated( + "This class will be removed in future versions. Used function from sdk.networkManager", + level = DeprecationLevel.WARNING +) sealed class ApiMethod(val type: String, val method: String) { class POST(method: String): ApiMethod(type = "POST", method = method) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/NetworkManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/NetworkManager.kt new file mode 100644 index 00000000..d78983e5 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/NetworkManager.kt @@ -0,0 +1,31 @@ +package com.personalizatio.api.managers + +import com.personalizatio.api.OnApiCallbackListener +import org.json.JSONObject + +interface NetworkManager { + + /** + * Direct query execution + */ + fun post(method: String, params: JSONObject, listener: OnApiCallbackListener?) + + /** + * Asynchronous execution of a request if did is not specified and initialization has not been completed + */ + fun postAsync(method: String, params: JSONObject, listener: OnApiCallbackListener? = null) + + /** + * Direct query execution + */ + fun get(method: String, params: JSONObject, listener: OnApiCallbackListener?) + + /** + * Asynchronous execution of a request if did is not specified and initialization has not been completed + */ + fun getAsync(method: String, params: JSONObject, listener: OnApiCallbackListener?) + + fun executeQueueTasks() {} + + fun addTaskToQueue(thread: Thread) +} \ No newline at end of file diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/network/NetworkManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/network/NetworkManagerImpl.kt new file mode 100644 index 00000000..5c1ffff7 --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/network/NetworkManagerImpl.kt @@ -0,0 +1,218 @@ +package com.personalizatio.network + +import android.net.Uri +import com.personalizatio.SDK +import com.personalizatio.api.OnApiCallbackListener +import com.personalizatio.api.managers.NetworkManager +import com.personalizatio.notifications.Source +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject +import org.json.JSONTokener +import java.io.BufferedReader +import java.io.BufferedWriter +import java.io.IOException +import java.io.InputStream +import java.io.InputStreamReader +import java.io.OutputStreamWriter +import java.net.ConnectException +import java.net.HttpURLConnection +import java.net.URL +import java.nio.charset.StandardCharsets +import java.util.Collections + +internal class NetworkManagerImpl( + private val sdk: SDK, + private val baseUrl: String, + private val shopId: String, + private val seance: String?, + private val segment: String, + private val stream: String, + private val source: Source +): NetworkManager { + + private val queue: MutableList = Collections.synchronizedList(ArrayList()) + + override fun post(method: String, params: JSONObject, listener: OnApiCallbackListener?) { + send(NetworkMethod.POST(method), params, listener) + } + + override fun postAsync(method: String, params: JSONObject, listener: OnApiCallbackListener?) { + sendAsync { send(NetworkMethod.POST(method), params, listener) } + } + + override fun get(method: String, params: JSONObject, listener: OnApiCallbackListener?) { + send(NetworkMethod.GET(method), params, listener) + } + + override fun getAsync(method: String, params: JSONObject, listener: OnApiCallbackListener?) { + sendAsync { send(NetworkMethod.GET(method), params, listener) } + } + + private fun send(networkMethod: NetworkMethod, params: JSONObject, listener: OnApiCallbackListener?) { + sdk.updateSidActivity() + + sendMethod(networkMethod, params, listener) + } + + private fun sendAsync(sendFunction: () -> Unit) { + val thread = Thread(sendFunction) + if (sdk.registerManager.did != null && sdk.initialized) { + thread.start() + } else { + queue.add(thread) + } + } + + override fun executeQueueTasks() { + for (thread in queue) { + thread.start() + } + queue.clear() + } + + override fun addTaskToQueue(thread: Thread) { + queue.add(thread) + } + + private fun sendMethod(networkMethod: NetworkMethod, params: JSONObject, listener: OnApiCallbackListener?) { + try { + params.put(SHOP_ID_PARAMS_FIELD, shopId) + if (sdk.registerManager.did != null) { + params.put(DID_PARAMS_FIELD, sdk.registerManager.did) + } + if (seance != null) { + params.put(SEANCE_PARAMS_FIELD, seance) + params.put(SID_PARAMS_FIELD, seance) + } + params.put(SEGMENT_PARAMS_FIELD, segment) + params.put(STREAM_PARAMS_FIELD, stream) + + val sourceJson = source.getJsonObject(sourceTimeDuration) + if (sourceJson != null) { + params.put(SOURCE_PARAMS_FIELD, sourceJson) + } + + executeMethod(networkMethod, params, listener) + } catch (e: JSONException) { + SDK.error(e.message, e) + } + } + + private fun executeMethod(networkMethod: NetworkMethod, params: JSONObject, listener: OnApiCallbackListener?) { + val thread = Thread { + try { + val buildUri = build(networkMethod, params) + val url = getUrl(networkMethod, buildUri) + val connection = getConnection(networkMethod, url, params) + + connection.connect() + + if (networkMethod is NetworkMethod.POST) { + SDK.debug(connection.responseCode.toString() + ": " + networkMethod.type + " " + url + " with body: " + params) + } else { + SDK.debug(connection.responseCode.toString() + ": " + networkMethod.type + " " + buildUri.toString()) + } + + if (listener != null && connection.responseCode == HttpURLConnection.HTTP_OK) { + val json = JSONTokener(readStream(connection.inputStream)).nextValue() + if (json is JSONObject) { + listener.onSuccess(json) + } else if (json is JSONArray) { + listener.onSuccess(json) + } + } + + if (connection.responseCode >= 400) { + val error = readStream(connection.errorStream) + SDK.error(error) + listener?.onError(connection.responseCode, error) + } + + connection.disconnect() + } catch (e: ConnectException) { + SDK.error(e.message) + listener?.onError(504, e.message) + } catch (e: Exception) { + SDK.error(e.message, e) + listener?.onError(-1, e.message) + } + } + + thread.start() + } + + private fun build(networkMethod: NetworkMethod, params: JSONObject): Uri { + val builder = Uri.parse(baseUrl + networkMethod.method).buildUpon() + + val it = params.keys() + while (it.hasNext()) { + val key = it.next() + builder.appendQueryParameter(key, params.getString(key)) + } + + return builder.build() + } + + private fun getUrl(networkMethod: NetworkMethod, buildUri: Uri) : URL { + return if (networkMethod is NetworkMethod.POST) { + URL(baseUrl + networkMethod.method) + } else { + URL(buildUri.toString()) + } + } + + private fun getConnection(networkMethod: NetworkMethod, url: URL, params: JSONObject) : HttpURLConnection { + val connection = url.openConnection() as HttpURLConnection + connection.setRequestProperty("User-Agent", SDK.userAgent()) + connection.requestMethod = networkMethod.type + connection.connectTimeout = 5000 + + if (networkMethod is NetworkMethod.POST) { + connection.setRequestProperty("Content-Type", "application/json") + connection.doOutput = true + connection.doInput = true + val os = BufferedWriter(OutputStreamWriter(connection.outputStream, StandardCharsets.UTF_8)) + os.write(params.toString()) + os.flush() + os.close() + } + + return connection + } + + private fun readStream(inputStream: InputStream): String { + var reader: BufferedReader? = null + val response = StringBuffer() + try { + reader = BufferedReader(InputStreamReader(inputStream)) + var line: String? + while ((reader.readLine().also { line = it }) != null) { + response.append(line) + } + } catch (e: IOException) { + e.printStackTrace() + } finally { + if (reader != null) { + try { + reader.close() + } catch (e: IOException) { + e.printStackTrace() + } + } + } + return response.toString() + } + + companion object { + private var sourceTimeDuration = 172800 * 1000 // 2 days + + private const val SHOP_ID_PARAMS_FIELD = "shop_id" + private const val DID_PARAMS_FIELD = "did" + private const val SEANCE_PARAMS_FIELD = "seance" + private const val SID_PARAMS_FIELD = "sid" + private const val SEGMENT_PARAMS_FIELD = "segment" + private const val STREAM_PARAMS_FIELD = "stream" + private const val SOURCE_PARAMS_FIELD = "source" + } +} diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/network/NetworkMethod.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/network/NetworkMethod.kt new file mode 100644 index 00000000..2184055b --- /dev/null +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/network/NetworkMethod.kt @@ -0,0 +1,8 @@ +package com.personalizatio.network + +internal sealed class NetworkMethod(val type: String, val method: String) { + + class POST(method: String): NetworkMethod(type = "POST", method = method) + + class GET(method: String): NetworkMethod(type = "GET", method = method) +} From af63bbe232fdffc2a643447ebdb1ebfde69a4850 Mon Sep 17 00:00:00 2001 From: Maria Anchugova Date: Thu, 11 Jul 2024 16:01:43 +0500 Subject: [PATCH 11/11] feat (recommendation): replaced some parameters to Params on get recommendation methods --- .../api/managers/RecommendationManager.kt | 12 +++------ .../RecommendationManagerImpl.kt | 25 +++---------------- 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt index 5de081ce..22b49b48 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/api/managers/RecommendationManager.kt @@ -11,15 +11,13 @@ interface RecommendationManager { * Request a dynamic block of recommendations * * @param recommenderCode Recommendation block code - * @param imageSize Image size (px) - * @param withLocations With locations + * @param params Parameters for the request * @param onGetRecommendation Callback for get recommendation * @param onError Callback for error */ fun getRecommendation( recommenderCode: String, - imageSize: Int? = null, - withLocations: Boolean? = null, + params: Params = Params(), onGetRecommendation: (GetRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) @@ -28,15 +26,13 @@ interface RecommendationManager { * Request a dynamic block of recommendations with all information about recommended products * * @param recommenderCode Recommendation block code + * @param params Parameters for the request * @param onGetExtendedRecommendation Callback for get extended recommendation - * @param imageSize Image size (px) - * @param withLocations With locations * @param onError Callback for error */ fun getExtendedRecommendation( recommenderCode: String, - imageSize: Int? = null, - withLocations: Boolean? = null, + params: Params = Params(), onGetExtendedRecommendation: (GetExtendedRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit = { _: Int, _: String? -> } ) diff --git a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt index 0ef3103b..108cefa3 100644 --- a/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt +++ b/personalizatio-sdk/src/main/kotlin/com/personalizatio/features/recommendation/RecommendationManagerImpl.kt @@ -13,12 +13,11 @@ internal class RecommendationManagerImpl(private val sdk: SDK) : RecommendationM override fun getRecommendation( recommenderCode: String, - imageSize: Int?, - withLocations: Boolean?, + params: Params, onGetRecommendation: (GetRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit ) { - val params = getRecommendationParams(false, imageSize, withLocations) + params.put(EXTENDED_PARAMETER, false) getRecommendation(recommenderCode, params, object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { @@ -36,12 +35,11 @@ internal class RecommendationManagerImpl(private val sdk: SDK) : RecommendationM override fun getExtendedRecommendation( recommenderCode: String, - imageSize: Int?, - withLocations: Boolean?, + params: Params, onGetExtendedRecommendation: (GetExtendedRecommendationResponse) -> Unit, onError: (Int, String?) -> Unit ) { - val params = getRecommendationParams(true, imageSize, withLocations) + params.put(EXTENDED_PARAMETER, true) getRecommendation(recommenderCode, params, object : OnApiCallbackListener() { override fun onSuccess(response: JSONObject?) { @@ -61,24 +59,9 @@ internal class RecommendationManagerImpl(private val sdk: SDK) : RecommendationM sdk.getAsync("$GET_RECOMMENDATION_REQUEST/$recommenderCode", params.build(), listener) } - private fun getRecommendationParams( - extended: Boolean?, - imageSize: Int?, - withLocations: Boolean?) : Params { - val params = Params() - - if(extended != null) params.put(EXTENDED_PARAMETER, extended) - if(imageSize != null) params.put(IMAGE_SIZE_PARAMETER, imageSize) - if(withLocations != null) params.put(WITH_LOCATIONS_PARAMETER, withLocations) - - return params - } - companion object { const val GET_RECOMMENDATION_REQUEST = "recommend" const val EXTENDED_PARAMETER = "extended" - const val IMAGE_SIZE_PARAMETER = "resize_image" - const val WITH_LOCATIONS_PARAMETER = "with_locations" } }