Skip to content

Commit 66903ab

Browse files
committed
ECWID-161269 - add a new modified RequestKind to replace ApiCredentials
1 parent eb56282 commit 66903ab

5 files changed

Lines changed: 89 additions & 3 deletions

File tree

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class ApiClientHelper private constructor(
4343
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)
@@ -76,6 +77,22 @@ class ApiClientHelper private constructor(
7677
jsonTransformer = jsonTransformerProvider.build(createPolymorphicTypeList())
7778
)
7879

80+
constructor(
81+
apiServerDomain: ApiServerDomain,
82+
credentials: ApiCredentials,
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,7 +277,12 @@ 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)
280+
val headers = headers.withRequestId(requestId)
281+
if (requestKind != null) {
282+
headers.withRequestKind(requestKind)
283+
} else {
284+
headers.withCredentials(credentials)
285+
}
264286

265287
return when (method) {
266288
HttpMethod.GET -> HttpRequest.HttpGetRequest(
@@ -302,7 +324,12 @@ class ApiClientHelper private constructor(
302324
null,
303325
null
304326
)
305-
val encodedPath = buildBaseEndpointPath(credentials) + "/" + buildEndpointPath(pathSegments)
327+
328+
val encodedPath = if (requestKind != null) {
329+
requestKind.buildBaseEndpointPath() + "/" + buildEndpointPath(pathSegments)
330+
} else {
331+
buildBaseEndpointPath(credentials) + "/" + buildEndpointPath(pathSegments)
332+
}
306333
return uri.toString() + encodedPath
307334
}
308335

@@ -443,6 +470,11 @@ internal fun Map<String, String>.withCredentials(credentials: ApiCredentials) =
443470
is ApiAppCredentials -> withAppCredentialsHeaders(credentials)
444471
}
445472

473+
@PublishedApi
474+
internal fun Map<String, String>.withRequestKind(requestKind: RequestKind) = toMutableMap().apply {
475+
putAll(requestKind.buildHeaders())
476+
}
477+
446478
internal fun Map<String, String>.withResponseFieldsParam(responseFields: ResponseFields): Map<String, String> {
447479
return if (responseFields.isAll()) {
448480
this - RESPONSE_FIELDS_PARAM_NAME
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.ecwid.apiclient.v3.config
2+
3+
private const val APP_CLIENT_ID_HEADER_NAME = "X-Ecwid-App-Client-Id"
4+
private const val APP_CLIENT_SECRET_HEADER_NAME = "X-Ecwid-App-Secret-Key"
5+
6+
class ApiV3AppRequestKind(
7+
private val clientId: String,
8+
private val clientSecret: String,
9+
) : RequestKind() {
10+
override fun buildBaseEndpointPath(): String {
11+
return "/api/v3"
12+
}
13+
14+
override fun buildHeaders(): Map<String, String> {
15+
return mapOf(
16+
APP_CLIENT_ID_HEADER_NAME to clientId,
17+
APP_CLIENT_SECRET_HEADER_NAME to clientSecret
18+
)
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.ecwid.apiclient.v3.config
2+
3+
class ApiV3InstantSiteRequestKind(
4+
private val storeId: Long,
5+
private val apiToken: String,
6+
) : RequestKind() {
7+
override fun buildBaseEndpointPath(): String {
8+
return "/instantsite/api/v1/$storeId"
9+
}
10+
11+
override fun buildHeaders(): Map<String, String> {
12+
return mapOf("Authorization" to "Bearer $apiToken")
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.ecwid.apiclient.v3.config
2+
3+
class ApiV3StoreRequestKind(
4+
private val storeId: Long,
5+
private val apiToken: String,
6+
) : RequestKind() {
7+
override fun buildBaseEndpointPath(): String {
8+
return "/api/v3/$storeId"
9+
}
10+
11+
override fun buildHeaders(): Map<String, String> {
12+
return mapOf("Authorization" to "Bearer $apiToken")
13+
}
14+
}
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+
}

0 commit comments

Comments
 (0)