-
Notifications
You must be signed in to change notification settings - Fork 4
feat(java-sdk): allow passing OkHttpClient objects #195
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
| {{! This template had to be customized because of our changes to the DefaultApi and ApiClient classes }} | ||
| {{! Original template: https://github.com/OpenAPITools/openapi-generator/blob/v7.15.0/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/api_test.mustache }} | ||
|
|
||
| {{>licenseInfo}} | ||
|
|
||
| package {{invokerPackage}}; | ||
|
|
||
| import cloud.stackit.sdk.core.KeyFlowAuthenticator; | ||
| import cloud.stackit.sdk.core.auth.SetupAuth; | ||
| import cloud.stackit.sdk.core.config.CoreConfiguration; | ||
| import cloud.stackit.sdk.core.utils.TestUtils; | ||
| import java.io.IOException; | ||
| import okhttp3.Authenticator; | ||
| import okhttp3.OkHttpClient; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class DefaultApiTest { | ||
| @Test | ||
| public void TestCustomHttpClient() throws IOException { | ||
| // before | ||
| CoreConfiguration conf = | ||
| new CoreConfiguration().serviceAccountKey(TestUtils.MOCK_SERVICE_ACCOUNT_KEY); | ||
|
|
||
| // when | ||
| OkHttpClient httpClient = new OkHttpClient(); | ||
| ApiClient apiClient = new ApiClient(httpClient, conf); | ||
|
|
||
| // then | ||
| Assertions.assertEquals(httpClient, apiClient.getHttpClient()); | ||
| // make sure the http client object is exactly the same object | ||
| Assertions.assertSame(httpClient, apiClient.getHttpClient()); | ||
| } | ||
|
|
||
| @Test | ||
| public void TestNoCustomHttpClient() throws IOException { | ||
| // before | ||
| CoreConfiguration conf = | ||
| new CoreConfiguration().serviceAccountKey(TestUtils.MOCK_SERVICE_ACCOUNT_KEY); | ||
|
|
||
| // when | ||
| ApiClient apiClient = new ApiClient(conf); | ||
|
|
||
| // then | ||
| /* | ||
| * verify a fresh OkHttpClient got created which will have the auth header set | ||
| * by the {@link cloud.stackit.sdk.core.KeyFlowAuthenticator} | ||
| */ | ||
| OkHttpClient httpClient = new OkHttpClient(); | ||
| Authenticator authenticator = | ||
| new KeyFlowAuthenticator(httpClient, conf, SetupAuth.setupKeyFlow(conf)); | ||
| httpClient = httpClient.newBuilder().authenticator(authenticator).build(); | ||
|
|
||
| Assertions.assertNotNull(apiClient.getHttpClient()); | ||
| Assertions.assertEquals( | ||
| httpClient.authenticator().getClass(), | ||
| apiClient.getHttpClient().authenticator().getClass()); | ||
| } | ||
| } | ||
This file contains hidden or 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,57 @@ | ||
| {{>licenseInfo}} | ||
|
|
||
| package {{invokerPackage}}; | ||
|
|
||
| import cloud.stackit.sdk.core.KeyFlowAuthenticator; | ||
| import cloud.stackit.sdk.core.auth.SetupAuth; | ||
| import cloud.stackit.sdk.core.config.CoreConfiguration; | ||
| import cloud.stackit.sdk.core.utils.TestUtils; | ||
| import java.io.IOException; | ||
| import okhttp3.Authenticator; | ||
| import okhttp3.OkHttpClient; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** Tests for ApiClient */ | ||
| public class ApiClientTest { | ||
| @Test | ||
| public void TestCustomHttpClient() throws IOException { | ||
| // before | ||
| CoreConfiguration conf = | ||
| new CoreConfiguration().serviceAccountKey(TestUtils.MOCK_SERVICE_ACCOUNT_KEY); | ||
|
|
||
| // when | ||
| OkHttpClient httpClient = new OkHttpClient(); | ||
| ApiClient apiClient = new ApiClient(httpClient, conf); | ||
|
|
||
| // then | ||
| Assertions.assertEquals(httpClient, apiClient.getHttpClient()); | ||
| // make sure the http client object is exactly the same object | ||
| Assertions.assertSame(httpClient, apiClient.getHttpClient()); | ||
| } | ||
|
|
||
| @Test | ||
| public void TestNoCustomHttpClient() throws IOException { | ||
| // before | ||
| CoreConfiguration conf = | ||
| new CoreConfiguration().serviceAccountKey(TestUtils.MOCK_SERVICE_ACCOUNT_KEY); | ||
|
|
||
| // when | ||
| ApiClient apiClient = new ApiClient(conf); | ||
|
|
||
| // then | ||
| /* | ||
| * verify a fresh OkHttpClient got created which will have the auth header set | ||
| * by the {@link cloud.stackit.sdk.core.KeyFlowAuthenticator} | ||
| */ | ||
| OkHttpClient httpClient = new OkHttpClient(); | ||
| Authenticator authenticator = | ||
| new KeyFlowAuthenticator(httpClient, conf, SetupAuth.setupKeyFlow(conf)); | ||
| httpClient = httpClient.newBuilder().authenticator(authenticator).build(); | ||
|
|
||
| Assertions.assertNotNull(apiClient.getHttpClient()); | ||
| Assertions.assertEquals( | ||
| httpClient.authenticator().getClass(), | ||
| apiClient.getHttpClient().authenticator().getClass()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.