Skip to content

ECWID-161269 - add a new modified RequestKind to replace ApiCredentials #485

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
50 changes: 43 additions & 7 deletions src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ private val REQUEST_ID_CHARACTERS = ('a'..'z') + ('A'..'Z') + ('0'..'9')

class ApiClientHelper private constructor(
private val apiServerDomain: ApiServerDomain,
private val credentials: ApiCredentials,
private val credentials: ApiCredentials?,
private val loggingSettings: LoggingSettings,
val httpTransport: HttpTransport,
val jsonTransformer: JsonTransformer
val jsonTransformer: JsonTransformer,
private val requestKind: RequestKind? = null,
) {

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

constructor(
apiServerDomain: ApiServerDomain,
storeCredentials: ApiStoreCredentials,
storeCredentials: ApiStoreCredentials? = null,
loggingSettings: LoggingSettings,
httpTransport: HttpTransport,
jsonTransformerProvider: JsonTransformerProvider
Expand All @@ -64,7 +65,7 @@ class ApiClientHelper private constructor(

constructor(
apiServerDomain: ApiServerDomain,
credentials: ApiCredentials,
credentials: ApiCredentials? = null,
loggingSettings: LoggingSettings,
httpTransport: HttpTransport,
jsonTransformerProvider: JsonTransformerProvider
Expand All @@ -76,6 +77,22 @@ class ApiClientHelper private constructor(
jsonTransformer = jsonTransformerProvider.build(createPolymorphicTypeList())
)

constructor(
apiServerDomain: ApiServerDomain,
credentials: ApiCredentials? = null,
loggingSettings: LoggingSettings,
httpTransport: HttpTransport,
jsonTransformerProvider: JsonTransformerProvider,
requestKind: RequestKind?,
) : this(
apiServerDomain = apiServerDomain,
credentials = credentials,
loggingSettings = loggingSettings,
httpTransport = httpTransport,
jsonTransformer = jsonTransformerProvider.build(createPolymorphicTypeList()),
requestKind = requestKind,
)

@PublishedApi
internal fun <V> makeRequestInt(request: ApiRequest, responseParser: ResponseParser<V>, responseFieldsOverride: ResponseFields? = null): V {
val requestId = generateRequestId()
Expand Down Expand Up @@ -260,8 +277,17 @@ class ApiClientHelper private constructor(
internal fun RequestInfo.toHttpRequest(requestId: String, responseFieldsOverride: ResponseFields?): HttpRequest {
val uri = createApiEndpointUri(pathSegments)
val params = if (responseFieldsOverride != null) params.withResponseFieldsParam(responseFieldsOverride) else params
val headers = headers.withRequestId(requestId).withCredentials(credentials)

val headers = headers.withRequestId(requestId).let {
if (requestKind != null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If requestKind is provided, we use all data from it; otherwise, we use the old data from credentials.
all this if block will be deleted soon

it.withRequestKind(requestKind)
} else {
if (credentials != null) {
it.withCredentials(credentials)
} else {
it
}
}
}
return when (method) {
HttpMethod.GET -> HttpRequest.HttpGetRequest(
uri = uri,
Expand Down Expand Up @@ -302,7 +328,12 @@ class ApiClientHelper private constructor(
null,
null
)
val encodedPath = buildBaseEndpointPath(credentials) + "/" + buildEndpointPath(pathSegments)

val encodedPath = if (requestKind != null) {
requestKind.buildBaseEndpointPath() + "/" + buildEndpointPath(pathSegments)
} else {
credentials?.let { buildBaseEndpointPath(it) } + "/" + buildEndpointPath(pathSegments)
}
return uri.toString() + encodedPath
}

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

@PublishedApi
internal fun Map<String, String>.withRequestKind(requestKind: RequestKind) = toMutableMap().apply {
putAll(requestKind.buildHeaders())
}

internal fun Map<String, String>.withResponseFieldsParam(responseFields: ResponseFields): Map<String, String> {
return if (responseFields.isAll()) {
this - RESPONSE_FIELDS_PARAM_NAME
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/com/ecwid/apiclient/v3/config/RequestKind.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ecwid.apiclient.v3.config

abstract class RequestKind {
abstract fun buildBaseEndpointPath(): String
abstract fun buildHeaders(): Map<String, String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ abstract class BaseEntityTest {

protected fun initStoreProfile() {
val expectedProfile = UpdatedStoreProfile(
generalInfo = UpdatedStoreProfile.GeneralInfo(
storeUrl = ""
),
generalInfo = UpdatedStoreProfile.GeneralInfo(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this thing breaks the behavior in tests because the platform is not IS and therefore we get dirty/hash urls

formatsAndUnits = UpdatedStoreProfile.FormatsAndUnits(
orderNumberPrefix = "",
orderNumberSuffix = ""
Expand Down