Skip to content

Commit

Permalink
Merge pull request #404 from Ecwid/ECWID-139066
Browse files Browse the repository at this point in the history
ECWID-139066 - support PartialResult models for variation
  • Loading branch information
mplain authored May 14, 2024
2 parents c4a70f3 + 6439bbe commit e6bd894
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/main/kotlin/com/ecwid/apiclient/v3/ApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.ecwid.apiclient.v3.dto.batch.result.GetEscapedBatchResult
import com.ecwid.apiclient.v3.dto.batch.result.GetTypedBatchResult
import com.ecwid.apiclient.v3.dto.cart.request.*
import com.ecwid.apiclient.v3.dto.cart.result.*
import com.ecwid.apiclient.v3.dto.common.PartialResult
import com.ecwid.apiclient.v3.dto.coupon.request.*
import com.ecwid.apiclient.v3.dto.coupon.result.*
import com.ecwid.apiclient.v3.dto.customer.request.*
Expand Down Expand Up @@ -47,6 +48,7 @@ import com.ecwid.apiclient.v3.dto.variation.result.*
import com.ecwid.apiclient.v3.httptransport.HttpTransport
import com.ecwid.apiclient.v3.impl.*
import com.ecwid.apiclient.v3.jsontransformer.JsonTransformerProvider
import kotlin.reflect.KClass

open class ApiClient private constructor(
protected val apiClientHelper: ApiClientHelper,
Expand Down Expand Up @@ -212,14 +214,28 @@ interface ProductVariationsApiClient {
fun uploadVariationImage(request: ProductVariationImageUploadRequest): ProductVariationImageUploadResult
fun uploadProductVariationImageAsync(request: ProductVariationImageAsyncUploadRequest): ProductVariationImageAsyncUploadResult
fun getAllProductVariations(request: ProductVariationsRequest): ProductVariationsResult
fun <Result> getAllProductVariations(request: ProductVariationsRequest, resultClass: KClass<Result>): List<Result>
where Result : PartialResult<FetchedVariation>
fun getProductVariation(request: ProductVariationDetailsRequest): FetchedVariation
fun <Result> getProductVariation(request: ProductVariationDetailsRequest, resultClass: KClass<Result>): Result
where Result : PartialResult<FetchedVariation>
fun updateProductVariation(request: UpdateProductVariationRequest): UpdateProductVariationResult
fun deleteProductVariation(request: DeleteProductVariationRequest): DeleteProductVariationsResult
fun deleteAllProductVariations(request: DeleteAllProductVariationsRequest): DeleteProductVariationsResult
fun adjustVariationInventory(request: AdjustVariationInventoryRequest): AdjustVariationInventoryResult
fun deleteVariationImage(request: ProductVariationImageDeleteRequest): ProductVariationImageDeleteResult
}

@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
inline fun <reified Result : PartialResult<FetchedVariation>> ProductVariationsApiClient.getProductVariation(request: ProductVariationDetailsRequest): Result {
return getProductVariation(request, Result::class)
}

@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
inline fun <reified Result : PartialResult<FetchedVariation>> ProductVariationsApiClient.getAllProductVariations(request: ProductVariationsRequest): List<Result> {
return getAllProductVariations(request, Result::class)
}

// Carts
// https://developers.ecwid.com/api-documentation/carts
interface CartsApiClient {
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ class ApiClientHelper private constructor(
)
}

@Suppress("UNCHECKED_CAST")
fun <V : PartialResult<*>> makeObjectPartialResultRequestList(request: ApiRequest, resultClass: KClass<V>): List<V> {
return makeRequestInt(
request = request,
responseParser = ObjectResponseParser(jsonTransformer, resultClass.java.arrayType() as Class<Array<V>>),
responseFieldsOverride = responseFieldsOf(resultClass)
).toList()
}

@Suppress("unused")
fun <VBase : PartialResult<*>, VExt : PartialResult<*>> makeObjectPartialResultWithExtRequest(
request: ApiRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.ecwid.apiclient.v3.dto.variation.result

import com.ecwid.apiclient.v3.dto.common.ApiFetchedDTO
import com.ecwid.apiclient.v3.dto.common.*
import com.ecwid.apiclient.v3.dto.common.ApiFetchedDTO.ModifyKind
import com.ecwid.apiclient.v3.dto.common.FetchedAlt
import com.ecwid.apiclient.v3.dto.common.FetchedAttributeValue
import com.ecwid.apiclient.v3.dto.common.LocalizedValueMap
import com.ecwid.apiclient.v3.dto.product.enums.AttributeValueLocation
import com.ecwid.apiclient.v3.dto.product.enums.OutOfStockVisibilityBehaviour
import com.ecwid.apiclient.v3.dto.product.enums.RecurringSubscriptionInterval
Expand Down Expand Up @@ -58,7 +55,7 @@ data class FetchedVariation(
val customsHsTariffCode: String? = null,
val subscriptionSettings: SubscriptionSettings? = null,
val alt: FetchedAlt? = null,
) : ApiFetchedDTO {
) : ApiFetchedDTO, ApiResultDTO {

data class AttributeValue(
override val id: Int? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.ecwid.apiclient.v3.impl

import com.ecwid.apiclient.v3.ApiClientHelper
import com.ecwid.apiclient.v3.ProductVariationsApiClient
import com.ecwid.apiclient.v3.dto.common.PartialResult
import com.ecwid.apiclient.v3.dto.variation.request.*
import com.ecwid.apiclient.v3.dto.variation.result.*
import kotlin.reflect.KClass

internal class ProductVariationsApiClientImpl(
private val apiClientHelper: ApiClientHelper
Expand All @@ -23,9 +25,17 @@ internal class ProductVariationsApiClientImpl(
override fun getAllProductVariations(request: ProductVariationsRequest) =
apiClientHelper.makeObjectResultRequest<ProductVariationsResult>(request)

override fun <Result : PartialResult<FetchedVariation>> getAllProductVariations(request: ProductVariationsRequest, resultClass: KClass<Result>): List<Result> {
return apiClientHelper.makeObjectPartialResultRequestList(request, resultClass)
}

override fun getProductVariation(request: ProductVariationDetailsRequest) =
apiClientHelper.makeObjectResultRequest<FetchedVariation>(request)

override fun <Result : PartialResult<FetchedVariation>> getProductVariation(request: ProductVariationDetailsRequest, resultClass: KClass<Result>): Result {
return apiClientHelper.makeObjectPartialResultRequest(request, resultClass)
}

override fun updateProductVariation(request: UpdateProductVariationRequest) =
apiClientHelper.makeObjectResultRequest<UpdateProductVariationResult>(request)

Expand Down

0 comments on commit e6bd894

Please sign in to comment.