Skip to content

Commit d14e51a

Browse files
authored
Merge pull request #485 from Ecwid/ECWID-161269
ECWID-161269 - add a new modified RequestKind to replace ApiCredentials
2 parents 7a5f588 + 3df038b commit d14e51a

3 files changed

Lines changed: 50 additions & 10 deletions

File tree

src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@ private val REQUEST_ID_CHARACTERS = ('a'..'z') + ('A'..'Z') + ('0'..'9')
4040

4141
class ApiClientHelper private constructor(
4242
private val apiServerDomain: ApiServerDomain,
43-
private val credentials: ApiCredentials,
43+
private val credentials: ApiCredentials?,
4444
private val loggingSettings: LoggingSettings,
4545
val httpTransport: HttpTransport,
46-
val jsonTransformer: JsonTransformer
46+
val jsonTransformer: JsonTransformer,
47+
private val requestKind: RequestKind? = null,
4748
) {
4849

4950
private val log = Logger.getLogger(this::class.qualifiedName)
5051

5152
constructor(
5253
apiServerDomain: ApiServerDomain,
53-
storeCredentials: ApiStoreCredentials,
54+
storeCredentials: ApiStoreCredentials? = null,
5455
loggingSettings: LoggingSettings,
5556
httpTransport: HttpTransport,
5657
jsonTransformerProvider: JsonTransformerProvider
@@ -64,7 +65,7 @@ class ApiClientHelper private constructor(
6465

6566
constructor(
6667
apiServerDomain: ApiServerDomain,
67-
credentials: ApiCredentials,
68+
credentials: ApiCredentials? = null,
6869
loggingSettings: LoggingSettings,
6970
httpTransport: HttpTransport,
7071
jsonTransformerProvider: JsonTransformerProvider
@@ -76,6 +77,22 @@ class ApiClientHelper private constructor(
7677
jsonTransformer = jsonTransformerProvider.build(createPolymorphicTypeList())
7778
)
7879

80+
constructor(
81+
apiServerDomain: ApiServerDomain,
82+
credentials: ApiCredentials? = null,
83+
loggingSettings: LoggingSettings,
84+
httpTransport: HttpTransport,
85+
jsonTransformerProvider: JsonTransformerProvider,
86+
requestKind: RequestKind?,
87+
) : this(
88+
apiServerDomain = apiServerDomain,
89+
credentials = credentials,
90+
loggingSettings = loggingSettings,
91+
httpTransport = httpTransport,
92+
jsonTransformer = jsonTransformerProvider.build(createPolymorphicTypeList()),
93+
requestKind = requestKind,
94+
)
95+
7996
@PublishedApi
8097
internal fun <V> makeRequestInt(request: ApiRequest, responseParser: ResponseParser<V>, responseFieldsOverride: ResponseFields? = null): V {
8198
val requestId = generateRequestId()
@@ -260,8 +277,17 @@ class ApiClientHelper private constructor(
260277
internal fun RequestInfo.toHttpRequest(requestId: String, responseFieldsOverride: ResponseFields?): HttpRequest {
261278
val uri = createApiEndpointUri(pathSegments)
262279
val params = if (responseFieldsOverride != null) params.withResponseFieldsParam(responseFieldsOverride) else params
263-
val headers = headers.withRequestId(requestId).withCredentials(credentials)
264-
280+
val headers = headers.withRequestId(requestId).let {
281+
if (requestKind != null) {
282+
it.withRequestKind(requestKind)
283+
} else {
284+
if (credentials != null) {
285+
it.withCredentials(credentials)
286+
} else {
287+
it
288+
}
289+
}
290+
}
265291
return when (method) {
266292
HttpMethod.GET -> HttpRequest.HttpGetRequest(
267293
uri = uri,
@@ -302,7 +328,12 @@ class ApiClientHelper private constructor(
302328
null,
303329
null
304330
)
305-
val encodedPath = buildBaseEndpointPath(credentials) + "/" + buildEndpointPath(pathSegments)
331+
332+
val encodedPath = if (requestKind != null) {
333+
requestKind.buildBaseEndpointPath() + "/" + buildEndpointPath(pathSegments)
334+
} else {
335+
credentials?.let { buildBaseEndpointPath(it) } + "/" + buildEndpointPath(pathSegments)
336+
}
306337
return uri.toString() + encodedPath
307338
}
308339

@@ -443,6 +474,11 @@ internal fun Map<String, String>.withCredentials(credentials: ApiCredentials) =
443474
is ApiAppCredentials -> withAppCredentialsHeaders(credentials)
444475
}
445476

477+
@PublishedApi
478+
internal fun Map<String, String>.withRequestKind(requestKind: RequestKind) = toMutableMap().apply {
479+
putAll(requestKind.buildHeaders())
480+
}
481+
446482
internal fun Map<String, String>.withResponseFieldsParam(responseFields: ResponseFields): Map<String, String> {
447483
return if (responseFields.isAll()) {
448484
this - RESPONSE_FIELDS_PARAM_NAME
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.ecwid.apiclient.v3.config
2+
3+
abstract class RequestKind {
4+
abstract fun buildBaseEndpointPath(): String
5+
abstract fun buildHeaders(): Map<String, String>
6+
}

src/test/kotlin/com/ecwid/apiclient/v3/entity/BaseEntityTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ abstract class BaseEntityTest {
7070

7171
protected fun initStoreProfile() {
7272
val expectedProfile = UpdatedStoreProfile(
73-
generalInfo = UpdatedStoreProfile.GeneralInfo(
74-
storeUrl = ""
75-
),
73+
generalInfo = UpdatedStoreProfile.GeneralInfo(),
7674
formatsAndUnits = UpdatedStoreProfile.FormatsAndUnits(
7775
orderNumberPrefix = "",
7876
orderNumberSuffix = ""

0 commit comments

Comments
 (0)