-
Notifications
You must be signed in to change notification settings - Fork 16
ECWID-153684 - added new method searchBrands in API Client. #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c25ea96
ECWID-153684 - added new method searchBrands in API Client.
Alexeyyy a54a3cd
ECWID-153684 - fixed DETEKT.
Alexeyyy 041295c
ECWID-153684 - fixed test issue.
Alexeyyy e1cef13
ECWID-153684 - added tests.
Alexeyyy eb99b16
ECWID-153684 - fixed DETEKT.
Alexeyyy 87ac3dc
ECWID-153684 - restructured code.
Alexeyyy 0c32622
ECWID-153684 - fixed detekt.
Alexeyyy d337a94
ECWID-153684 - reverted sample properties.
Alexeyyy ef1bef7
ECWID-153684 - reverted one more thing.
Alexeyyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.ecwid.apiclient.v3 | ||
|
|
||
| import com.ecwid.apiclient.v3.dto.brand.request.BrandsSearchRequest | ||
| import com.ecwid.apiclient.v3.dto.brand.result.BrandsSearchResult | ||
| import com.ecwid.apiclient.v3.dto.common.PartialResult | ||
| import kotlin.reflect.KClass | ||
|
|
||
| // Brands | ||
| // https://api-docs.ecwid.com/reference/product-brands | ||
| interface BrandsApiClient { | ||
| fun searchBrands(request: BrandsSearchRequest): BrandsSearchResult | ||
| fun <Result> searchBrands(request: BrandsSearchRequest, resultClass: KClass<Result>): Result | ||
| where Result : PartialResult<BrandsSearchResult> | ||
| } | ||
|
|
||
| @Suppress("EXTENSION_SHADOWED_BY_MEMBER") | ||
| inline fun <reified Result : PartialResult<BrandsSearchResult>> BrandsApiClient.searchBrands( | ||
| request: BrandsSearchRequest | ||
| ): Result { | ||
| return searchBrands(request, resultClass = Result::class) | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/com/ecwid/apiclient/v3/dto/brand/request/BrandsSearchRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.ecwid.apiclient.v3.dto.brand.request | ||
|
|
||
| import com.ecwid.apiclient.v3.dto.ApiRequest | ||
| import com.ecwid.apiclient.v3.dto.common.PagingRequest | ||
| import com.ecwid.apiclient.v3.impl.RequestInfo | ||
| import com.ecwid.apiclient.v3.responsefields.ResponseFields | ||
|
|
||
| sealed class BrandsSearchRequest : ApiRequest { | ||
|
|
||
| data class ByFilters( | ||
| val limit: Int = 100, | ||
| override val offset: Int = 0, | ||
| val lang: String? = null, | ||
| val hiddenBrands: Boolean? = null, | ||
| val baseUrl: String? = null, | ||
| val cleanUrls: Boolean? = null, | ||
| val sortBy: SortOrder? = null, | ||
| val responseFields: ResponseFields = ResponseFields.All, | ||
| ) : BrandsSearchRequest(), PagingRequest<ByFilters> { | ||
| override fun toRequestInfo() = RequestInfo.createGetRequest( | ||
| pathSegments = listOf( | ||
| "brands", | ||
| ), | ||
| params = toParams(), | ||
| responseFields = responseFields, | ||
| ) | ||
|
|
||
| private fun toParams(): Map<String, String> { | ||
| val request = this | ||
| return mutableMapOf<String, String>().apply { | ||
| put("limit", request.limit.toString()) | ||
| put("offset", request.offset.toString()) | ||
| request.lang?.let { put("lang", it) } | ||
| request.hiddenBrands?.let { put("hiddenBrands", it.toString()) } | ||
| request.baseUrl?.let { put("baseUrl", it) } | ||
| request.cleanUrls?.let { put("cleanUrls", it.toString()) } | ||
| request.sortBy?.let { put("sortBy", it.name) } | ||
| }.toMap() | ||
| } | ||
|
|
||
| override fun copyWithOffset(offset: Int) = copy(offset = offset) | ||
| } | ||
|
|
||
| @Suppress("unused") | ||
| enum class SortOrder { | ||
| PRODUCT_COUNT_DESC, | ||
| NAME_ASC, | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/com/ecwid/apiclient/v3/dto/brand/result/BrandsSearchResult.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.ecwid.apiclient.v3.dto.brand.result | ||
|
|
||
| import com.ecwid.apiclient.v3.dto.common.ApiResultDTO | ||
|
|
||
| data class BrandsSearchResult( | ||
| val items: List<FetchedBrand> = listOf(), | ||
| val count: Int = 0, | ||
| val total: Int = 0, | ||
| val limit: Int = 0, | ||
| val offset: Int = 0 | ||
| ) : ApiResultDTO |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/ecwid/apiclient/v3/dto/brand/result/FetchedBrand.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.ecwid.apiclient.v3.dto.brand.result | ||
|
|
||
| import com.ecwid.apiclient.v3.dto.common.ApiFetchedDTO | ||
| import com.ecwid.apiclient.v3.dto.common.ApiResultDTO | ||
|
|
||
| data class FetchedBrand( | ||
| val name: String = "", | ||
| val nameTranslated: Map<String, String>? = null, | ||
| val productsFilteredByBrandUrl: String? = null, | ||
| ) : ApiFetchedDTO, ApiResultDTO { | ||
| override fun getModifyKind() = ApiFetchedDTO.ModifyKind.ReadOnly | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/com/ecwid/apiclient/v3/impl/BrandsApiClientImpl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.ecwid.apiclient.v3.impl | ||
|
|
||
| import com.ecwid.apiclient.v3.ApiClientHelper | ||
| import com.ecwid.apiclient.v3.BrandsApiClient | ||
| import com.ecwid.apiclient.v3.dto.brand.request.BrandsSearchRequest | ||
| import com.ecwid.apiclient.v3.dto.brand.result.BrandsSearchResult | ||
| import com.ecwid.apiclient.v3.dto.common.PartialResult | ||
| import kotlin.reflect.KClass | ||
|
|
||
| internal class BrandsApiClientImpl( | ||
| private val apiClientHelper: ApiClientHelper, | ||
| ) : BrandsApiClient { | ||
|
|
||
| override fun searchBrands(request: BrandsSearchRequest) = | ||
| apiClientHelper.makeObjectResultRequest<BrandsSearchResult>(request) | ||
|
|
||
| override fun <Result : PartialResult<BrandsSearchResult>> searchBrands( | ||
| request: BrandsSearchRequest, | ||
| resultClass: KClass<Result> | ||
| ): Result { | ||
| return apiClientHelper.makeObjectPartialResultRequest( | ||
| request = request, | ||
| resultClass = resultClass, | ||
| ) | ||
| } | ||
| } |
96 changes: 96 additions & 0 deletions
96
src/test/kotlin/com/ecwid/apiclient/v3/entity/BrandsTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package com.ecwid.apiclient.v3.entity | ||
|
|
||
| import com.ecwid.apiclient.v3.dto.brand.request.BrandsSearchRequest | ||
| import com.ecwid.apiclient.v3.dto.product.request.ProductCreateRequest | ||
| import com.ecwid.apiclient.v3.dto.product.request.ProductsSearchRequest.ByFilters | ||
| import com.ecwid.apiclient.v3.dto.product.request.UpdatedProduct | ||
| import com.ecwid.apiclient.v3.dto.product.request.UpdatedProduct.* | ||
| import com.ecwid.apiclient.v3.util.randomAlphanumeric | ||
| import com.ecwid.apiclient.v3.util.randomPrice | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class BrandsTest : BaseEntityTest() { | ||
|
|
||
| @BeforeEach | ||
| override fun beforeEach() { | ||
| super.beforeEach() | ||
|
|
||
| initStoreProfile() | ||
| removeAllProducts() | ||
| } | ||
|
|
||
| @Test | ||
| fun getBrands() { | ||
| val brandedProductsCreateResult = createProductsWithBrands() | ||
|
|
||
| // Waiting till product became available for searching | ||
| brandedProductsCreateResult.skus.forEach { sku -> | ||
| waitForIndexedProducts( | ||
| productsSearchRequest = ByFilters(sku = sku), | ||
| desiredProductCount = 1 | ||
| ) | ||
| } | ||
|
|
||
| val result = apiClient.searchBrands(BrandsSearchRequest.ByFilters()) | ||
| assertEquals( | ||
| brandedProductsCreateResult.brandNames, | ||
| result.items.map { it.name } | ||
| ) | ||
| } | ||
|
|
||
| private fun createProductsWithBrands(productsCount: Int = 5): BrandedProductsCreateResult { | ||
| val brands = mutableListOf<String>() | ||
| val skus = mutableListOf<String>() | ||
|
|
||
| for (i in 1..productsCount) { | ||
| val randomBrand = randomAlphanumeric(8) | ||
| val randomSku = randomAlphanumeric(10) | ||
| val price = randomPrice() | ||
|
|
||
| val productCreateRequest = ProductCreateRequest( | ||
| newProduct = UpdatedProduct( | ||
| name = "Product $i ${randomAlphanumeric(8)}", | ||
| sku = randomSku, | ||
| price = price, | ||
| compareToPrice = 2 * price, | ||
| enabled = true, | ||
| quantity = 10, | ||
| attributes = listOf( | ||
| AttributeValue.createBrandAttributeValue(randomBrand), | ||
| ), | ||
| options = listOf( | ||
| ProductOption.createSelectOption( | ||
| name = "Color", | ||
| choices = listOf( | ||
| ProductOptionChoice("Black"), | ||
| ProductOptionChoice("White"), | ||
| ProductOptionChoice("Yellow"), | ||
| ProductOptionChoice("Red") | ||
| ), | ||
| defaultChoice = 0, | ||
| required = true | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| val productCreateResult = apiClient.createProduct(productCreateRequest) | ||
| assertTrue(productCreateResult.id > 0) | ||
|
|
||
| skus.add(randomSku) | ||
| brands.add(randomBrand) | ||
| } | ||
|
|
||
| return BrandedProductsCreateResult( | ||
| brandNames = brands, | ||
| skus = skus, | ||
| ) | ||
| } | ||
|
|
||
| data class BrandedProductsCreateResult( | ||
| val brandNames: List<String>, | ||
| val skus: List<String>, | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...test/kotlin/com/ecwid/apiclient/v3/rule/nullablepropertyrules/BrandsSearchRequestRules.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.ecwid.apiclient.v3.rule.nullablepropertyrules | ||
|
|
||
| import com.ecwid.apiclient.v3.dto.brand.request.BrandsSearchRequest | ||
| import com.ecwid.apiclient.v3.rule.NullablePropertyRule | ||
| import com.ecwid.apiclient.v3.rule.NullablePropertyRule.AllowNullable | ||
|
|
||
| val brandsSearchRequestNullablePropertyRules: List<NullablePropertyRule<*, *>> = listOf( | ||
| AllowNullable(BrandsSearchRequest.ByFilters::lang), | ||
| AllowNullable(BrandsSearchRequest.ByFilters::hiddenBrands), | ||
| AllowNullable(BrandsSearchRequest.ByFilters::baseUrl), | ||
| AllowNullable(BrandsSearchRequest.ByFilters::cleanUrls), | ||
| AllowNullable(BrandsSearchRequest.ByFilters::sortBy), | ||
| ) |
10 changes: 10 additions & 0 deletions
10
src/test/kotlin/com/ecwid/apiclient/v3/rule/nullablepropertyrules/FetchedBrandRules.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.ecwid.apiclient.v3.rule.nullablepropertyrules | ||
|
|
||
| import com.ecwid.apiclient.v3.dto.brand.result.FetchedBrand | ||
| import com.ecwid.apiclient.v3.rule.NullablePropertyRule | ||
| import com.ecwid.apiclient.v3.rule.NullablePropertyRule.AllowNullable | ||
|
|
||
| val fetchedBrandNullablePropertyRules: List<NullablePropertyRule<*, *>> = listOf( | ||
| AllowNullable(FetchedBrand::nameTranslated), | ||
| AllowNullable(FetchedBrand::productsFilteredByBrandUrl), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.