diff --git a/src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt b/src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt index eb33c9fc0..647a6fff3 100644 --- a/src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt +++ b/src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt @@ -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 @@ -64,7 +65,7 @@ class ApiClientHelper private constructor( constructor( apiServerDomain: ApiServerDomain, - credentials: ApiCredentials, + credentials: ApiCredentials? = null, loggingSettings: LoggingSettings, httpTransport: HttpTransport, jsonTransformerProvider: JsonTransformerProvider @@ -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 makeRequestInt(request: ApiRequest, responseParser: ResponseParser, responseFieldsOverride: ResponseFields? = null): V { val requestId = generateRequestId() @@ -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) { + it.withRequestKind(requestKind) + } else { + if (credentials != null) { + it.withCredentials(credentials) + } else { + it + } + } + } return when (method) { HttpMethod.GET -> HttpRequest.HttpGetRequest( uri = uri, @@ -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 } @@ -443,6 +474,11 @@ internal fun Map.withCredentials(credentials: ApiCredentials) = is ApiAppCredentials -> withAppCredentialsHeaders(credentials) } +@PublishedApi +internal fun Map.withRequestKind(requestKind: RequestKind) = toMutableMap().apply { + putAll(requestKind.buildHeaders()) +} + internal fun Map.withResponseFieldsParam(responseFields: ResponseFields): Map { return if (responseFields.isAll()) { this - RESPONSE_FIELDS_PARAM_NAME diff --git a/src/main/kotlin/com/ecwid/apiclient/v3/config/RequestKind.kt b/src/main/kotlin/com/ecwid/apiclient/v3/config/RequestKind.kt new file mode 100644 index 000000000..c30d448c0 --- /dev/null +++ b/src/main/kotlin/com/ecwid/apiclient/v3/config/RequestKind.kt @@ -0,0 +1,6 @@ +package com.ecwid.apiclient.v3.config + +abstract class RequestKind { + abstract fun buildBaseEndpointPath(): String + abstract fun buildHeaders(): Map +} diff --git a/src/test/kotlin/com/ecwid/apiclient/v3/entity/BaseEntityTest.kt b/src/test/kotlin/com/ecwid/apiclient/v3/entity/BaseEntityTest.kt index a4693b1ed..241ef7af3 100644 --- a/src/test/kotlin/com/ecwid/apiclient/v3/entity/BaseEntityTest.kt +++ b/src/test/kotlin/com/ecwid/apiclient/v3/entity/BaseEntityTest.kt @@ -70,9 +70,7 @@ abstract class BaseEntityTest { protected fun initStoreProfile() { val expectedProfile = UpdatedStoreProfile( - generalInfo = UpdatedStoreProfile.GeneralInfo( - storeUrl = "" - ), + generalInfo = UpdatedStoreProfile.GeneralInfo(), formatsAndUnits = UpdatedStoreProfile.FormatsAndUnits( orderNumberPrefix = "", orderNumberSuffix = ""