-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(domain): support domain helpers (#368)
- Support adding custom helpers for domains. - Add `extractToken` helper for Rapid.
- Loading branch information
Showing
20 changed files
with
229 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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 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
21 changes: 21 additions & 0 deletions
21
core/src/main/kotlin/com/expediagroup/sdk/core/client/ClientHelpers.kt
This file contains 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 @@ | ||
/* | ||
* Copyright (C) 2022 Expedia, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.expediagroup.sdk.core.client | ||
|
||
/** Handy utils and helpers for a client. */ | ||
abstract class ClientHelpers( | ||
val client: Client | ||
) |
This file contains 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 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
24 changes: 24 additions & 0 deletions
24
core/src/main/kotlin/com/expediagroup/sdk/domain/rapid/RapidHelpers.kt
This file contains 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,24 @@ | ||
/* | ||
* Copyright (C) 2022 Expedia, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.expediagroup.sdk.domain.rapid | ||
|
||
import com.expediagroup.sdk.core.client.BaseRapidClient | ||
import com.expediagroup.sdk.core.client.ClientHelpers | ||
|
||
class RapidHelpers(client: BaseRapidClient) : ClientHelpers(client) { | ||
/** Extracts the token parameter from a URL string if it exists; otherwise, returns null. */ | ||
fun extractToken(url: String): String? = Regex("token=([^&]*)").find(url)?.groupValues?.getOrNull(1) | ||
} |
115 changes: 115 additions & 0 deletions
115
core/src/test/kotlin/com/expediagroup/sdk/domain/rapid/RapidHelpersTest.kt
This file contains 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,115 @@ | ||
/* | ||
* Copyright (C) 2022 Expedia, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.expediagroup.sdk.domain.rapid | ||
|
||
import com.expediagroup.sdk.core.client.BaseRapidClient | ||
import com.expediagroup.sdk.core.configuration.RapidClientConfiguration | ||
import io.ktor.client.statement.HttpResponse | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertNull | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
|
||
class RapidHelpersTest { | ||
companion object { | ||
private val rapidClient = | ||
object : BaseRapidClient(RapidClientConfiguration()) { | ||
override suspend fun throwServiceException( | ||
response: HttpResponse, | ||
operationId: String | ||
) { | ||
throw UnsupportedOperationException() | ||
} | ||
} | ||
|
||
val rapidHelpers = RapidHelpers(rapidClient) | ||
} | ||
|
||
@Nested | ||
inner class TestExtractToken { | ||
@Test | ||
fun `Verify extractToken returns token when present`() { | ||
assertEquals("12345", rapidHelpers.extractToken("https://www.example.com?token=12345")) | ||
} | ||
|
||
@Test | ||
fun `Verify extractToken returns null when no token is present`() { | ||
assertNull(rapidHelpers.extractToken("https://www.example.com")) | ||
} | ||
|
||
@Test | ||
fun `Verify extractToken returns an empty string when token is empty`() { | ||
assertEquals("", rapidHelpers.extractToken("https://www.example.com?token=")) | ||
} | ||
|
||
@Test | ||
fun `Verify extractToken returns token when it is not the first parameter`() { | ||
assertEquals("12345", rapidHelpers.extractToken("https://www.example.com?foo=bar&token=12345")) | ||
} | ||
|
||
@Test | ||
fun `Verify extractToken returns null when token is not provided but other parameters are`() { | ||
assertNull(rapidHelpers.extractToken("https://www.example.com?foo=bar")) | ||
} | ||
|
||
@Test | ||
fun `Verify extractToken returns null when token is not provided but multiple other parameters are`() { | ||
assertNull(rapidHelpers.extractToken("https://www.example.com?foo=bar&baz=qux")) | ||
} | ||
|
||
@Test | ||
fun `Verify extractToken returns it when token is not the last parameter`() { | ||
assertEquals("12345", rapidHelpers.extractToken("https://www.example.com?token=12345&foo=bar")) | ||
} | ||
|
||
@Test | ||
fun `extractToken should handle multiple parameters and return the correct token`() { | ||
assertEquals("xyz456", rapidHelpers.extractToken("https://example.com/page?param1=value1&token=xyz456¶m2=value2")) | ||
} | ||
|
||
@Test | ||
fun `extractToken should handle URL-encoded characters in the token`() { | ||
assertEquals("abc%20456", rapidHelpers.extractToken("https://example.com/page?token=abc%20456¶m=value")) | ||
} | ||
|
||
@Test | ||
fun `extractToken should handle different token parameter names`() { | ||
assertEquals("abcd1234", rapidHelpers.extractToken("https://example.com/page?access_token=abcd1234")) | ||
assertEquals("efgh5678", rapidHelpers.extractToken("https://example.com/page?api_token=efgh5678")) | ||
} | ||
|
||
@Test | ||
fun `extractToken should handle multiple tokens`() { | ||
assertEquals("abcd1234", rapidHelpers.extractToken("https://example.com/page?token=abcd1234&token=efgh5678")) | ||
assertEquals("efgh5678", rapidHelpers.extractToken("https://example.com/page?api_token=efgh5678&access_token=abcd1234")) | ||
} | ||
|
||
@Test | ||
fun `extractToken should get only the query param token`() { | ||
assertNull(rapidHelpers.extractToken("https://example.com/tokenPage?query=tokenValue")) | ||
} | ||
|
||
@Test | ||
fun `extractToken should return an empty string when token is empty and other parameters are present`() { | ||
assertEquals("", rapidHelpers.extractToken("https://www.example.com?foo=bar&token=")) | ||
} | ||
|
||
@Test | ||
fun `extractToken should return an empty string when token is empty in the middle of other parameters`() { | ||
assertEquals("", rapidHelpers.extractToken("https://www.example.com?foo=bar&token=&baz=qux")) | ||
} | ||
} | ||
} |
This file contains 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
41 changes: 12 additions & 29 deletions
41
generator/openapi/src/main/resources/templates/expediagroup-sdk/client/client.mustache
This file contains 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
8 changes: 8 additions & 0 deletions
8
generator/openapi/src/main/resources/templates/expediagroup-sdk/imports/core.mustache
This file contains 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,8 @@ | ||
import com.expediagroup.sdk.core.client.{{#isRapid}}BaseRapidClient{{/isRapid}}{{#isExpediaGroup}}ExpediaGroupClient{{/isExpediaGroup}} | ||
import com.expediagroup.sdk.core.configuration.{{#isRapid}}RapidClientConfiguration{{/isRapid}}{{#isExpediaGroup}}ExpediaGroupClientConfiguration{{/isExpediaGroup}} | ||
import com.expediagroup.sdk.core.model.exception.ExpediaGroupException | ||
import com.expediagroup.sdk.core.model.exception.service.ExpediaGroupServiceException | ||
import com.expediagroup.sdk.core.model.EmptyResponse | ||
import com.expediagroup.sdk.core.model.Nothing | ||
import com.expediagroup.sdk.core.model.Properties | ||
import com.expediagroup.sdk.core.model.Response |
14 changes: 14 additions & 0 deletions
14
generator/openapi/src/main/resources/templates/expediagroup-sdk/imports/defaults.mustache
This file contains 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,14 @@ | ||
import io.ktor.client.call.body | ||
import io.ktor.client.request.HttpRequestBuilder | ||
import io.ktor.client.request.request | ||
import io.ktor.client.request.setBody | ||
import io.ktor.client.request.url | ||
import io.ktor.client.statement.HttpResponse | ||
import io.ktor.http.HttpMethod | ||
import io.ktor.http.contentType | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.ParametersBuilder | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.future.future | ||
import java.util.UUID |
2 changes: 2 additions & 0 deletions
2
generator/openapi/src/main/resources/templates/expediagroup-sdk/imports/domain.mustache
This file contains 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,2 @@ | ||
import {{packageName}}.validation.PropertyConstraintsValidator.validateConstraints | ||
import {{packageName}}.models.exception.* |
1 change: 1 addition & 0 deletions
1
generator/openapi/src/main/resources/templates/expediagroup-sdk/imports/helpers.mustache
This file contains 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 @@ | ||
{{#isRapid}}import com.expediagroup.sdk.domain.rapid.*{{/isRapid}} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.