com.ecwid.apiclient.v3.impl.CategoriesApiClientImpl.kt Maven / Gradle / Ivy
The newest version!
package com.ecwid.apiclient.v3.impl
import com.ecwid.apiclient.v3.ApiClientHelper
import com.ecwid.apiclient.v3.CategoriesApiClient
import com.ecwid.apiclient.v3.dto.category.request.*
import com.ecwid.apiclient.v3.dto.category.result.*
import com.ecwid.apiclient.v3.dto.common.PagingResult
import com.ecwid.apiclient.v3.dto.common.PartialResult
import com.ecwid.apiclient.v3.dto.common.fetchPagesAsItemSequence
import com.ecwid.apiclient.v3.responsefields.AS_SEQUENCE_SEARCH_RESULT_REQUIRED_FIELDS
import kotlin.reflect.KClass
internal class CategoriesApiClientImpl(
private val apiClientHelper: ApiClientHelper
) : CategoriesApiClient {
override fun searchCategories(request: CategoriesSearchRequest) =
apiClientHelper.makeObjectResultRequest(request)
override fun searchCategoriesAsSequence(request: CategoriesSearchRequest) = sequence {
var offsetRequest = request.copy(
responseFields = request.responseFields + AS_SEQUENCE_SEARCH_RESULT_REQUIRED_FIELDS
)
do {
val searchResult = searchCategories(offsetRequest)
yieldAll(searchResult.items)
offsetRequest = offsetRequest.copy(offset = offsetRequest.offset + searchResult.count)
} while (searchResult.count >= searchResult.limit)
}
override fun searchCategoriesByPath(request: CategoriesByPathRequest) =
apiClientHelper.makeObjectResultRequest(request)
override fun searchCategoriesByPathAsSequence(request: CategoriesByPathRequest) = sequence {
var offsetRequest = request.copy(
responseFields = request.responseFields + AS_SEQUENCE_SEARCH_RESULT_REQUIRED_FIELDS
)
do {
val searchResult = searchCategoriesByPath(offsetRequest)
yieldAll(searchResult.items)
offsetRequest = offsetRequest.copy(offset = offsetRequest.offset + searchResult.count)
} while (searchResult.count >= searchResult.limit)
}
override fun getCategoryDetails(request: CategoryDetailsRequest) =
apiClientHelper.makeObjectResultRequest(request)
override fun createCategory(request: CategoryCreateRequest) =
apiClientHelper.makeObjectResultRequest(request)
override fun updateCategory(request: CategoryUpdateRequest) =
apiClientHelper.makeObjectResultRequest(request)
override fun deleteCategory(request: CategoryDeleteRequest) =
apiClientHelper.makeObjectResultRequest(request)
override fun uploadCategoryImage(request: CategoryImageUploadRequest) =
apiClientHelper.makeObjectResultRequest(request)
override fun uploadCategoryImageAsync(request: CategoryImageAsyncUploadRequest) =
apiClientHelper.makeObjectResultRequest(request)
override fun deleteCategoryImage(request: CategoryImageDeleteRequest) =
apiClientHelper.makeObjectResultRequest(request)
override fun assignProductsToCategory(request: AssignProductsToCategoryRequest): CategoryUpdateResult =
apiClientHelper.makeObjectResultRequest(request)
override fun unassignProductsFromCategory(request: UnassignProductsFromCategoryRequest): CategoryDeleteResult =
apiClientHelper.makeObjectResultRequest(request)
override fun > getCategoryDetails(
request: CategoryDetailsRequest,
resultClass: KClass,
): Result {
return apiClientHelper.makeObjectPartialResultRequest(
request = request,
resultClass = resultClass,
)
}
override fun > searchCategories(
request: CategoriesSearchRequest,
resultClass: KClass
): Result {
return apiClientHelper.makeObjectPartialResultRequest(
request = request,
resultClass = resultClass,
)
}
override fun > searchCategoriesByPath(
request: CategoriesByPathRequest,
resultClass: KClass
): Result {
return apiClientHelper.makeObjectPartialResultRequest(
request = request,
resultClass = resultClass,
)
}
override fun searchCategoriesAsSequence(
request: CategoriesSearchRequest,
resultClass: KClass
): Sequence- where Result : PartialResult
, Result : PagingResult- {
return fetchPagesAsItemSequence(request) { searchCategories(it, resultClass) }
}
override fun
searchCategoriesByPathAsSequence(
request: CategoriesByPathRequest,
resultClass: KClass,
): Sequence- where Result : PartialResult
, Result : PagingResult- {
return fetchPagesAsItemSequence(request) { searchCategoriesByPath(it, resultClass) }
}
}