-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use HttpClient factory to create singleton instances
Signed-off-by: Paolo Di Tommaso <[email protected]>
- Loading branch information
1 parent
cb95c0d
commit 464c6b6
Showing
10 changed files
with
94 additions
and
63 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
44 changes: 44 additions & 0 deletions
44
src/main/groovy/io/seqera/wave/http/HttpClientFactory.groovy
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,44 @@ | ||
package io.seqera.wave.http | ||
|
||
import java.net.http.HttpClient | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Slf4j | ||
import io.micronaut.context.annotation.Factory | ||
import io.seqera.wave.configuration.HttpClientConfig | ||
import jakarta.inject.Inject | ||
import jakarta.inject.Named | ||
import jakarta.inject.Singleton | ||
/** | ||
* Java HttpClient factory | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
*/ | ||
@Factory | ||
@Slf4j | ||
@CompileStatic | ||
class HttpClientFactory { | ||
|
||
@Inject | ||
HttpClientConfig httpConfig | ||
|
||
@Singleton | ||
@Named("follow-redirects") | ||
HttpClient followRedirectsHttpClient() { | ||
HttpClient.newBuilder() | ||
.version(HttpClient.Version.HTTP_1_1) | ||
.followRedirects(HttpClient.Redirect.NORMAL) | ||
.connectTimeout(httpConfig.connectTimeout) | ||
.build() | ||
} | ||
|
||
@Singleton | ||
@Named("never-redirects") | ||
HttpClient neverRedirectsHttpClient() { | ||
HttpClient.newBuilder() | ||
.version(HttpClient.Version.HTTP_1_1) | ||
.followRedirects(HttpClient.Redirect.NEVER) | ||
.connectTimeout(httpConfig.connectTimeout) | ||
.build() | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -4,15 +4,18 @@ import spock.lang.Requires | |
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
import java.net.http.HttpClient | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import io.seqera.wave.auth.RegistryAuth | ||
import io.seqera.wave.auth.RegistryAuthService | ||
import io.seqera.wave.auth.RegistryCredentialsProvider | ||
import io.seqera.wave.auth.RegistryLookupService | ||
import io.seqera.wave.configuration.HttpClientConfig | ||
import io.seqera.wave.proxy.ProxyClient | ||
import jakarta.inject.Inject | ||
import jakarta.inject.Named | ||
|
||
/** | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
|
@@ -27,7 +30,7 @@ class ProxyClientTest extends Specification { | |
@Inject RegistryLookupService lookupService | ||
@Inject RegistryAuthService loginService | ||
@Inject RegistryCredentialsProvider credentialsProvider | ||
@Inject HttpClientConfig config | ||
@Inject @Named("never-redirects") HttpClient httpClient | ||
|
||
def 'should call target manifests on docker.io' () { | ||
given: | ||
|
@@ -36,7 +39,7 @@ class ProxyClientTest extends Specification { | |
def registry = lookupService.lookup(REG) | ||
def creds = credentialsProvider.getDefaultCredentials(REG) | ||
and: | ||
def proxy = new ProxyClient(config) | ||
def proxy = new ProxyClient(httpClient) | ||
.withImage(IMAGE) | ||
.withRegistry(registry) | ||
.withLoginService(loginService) | ||
|
@@ -55,7 +58,7 @@ class ProxyClientTest extends Specification { | |
def IMAGE = 'library/hello-world' | ||
def registry = lookupService.lookup(REG) | ||
and: | ||
def proxy = new ProxyClient(config) | ||
def proxy = new ProxyClient(httpClient) | ||
.withImage(IMAGE) | ||
.withRegistry(registry) | ||
.withLoginService(loginService) | ||
|
@@ -74,7 +77,7 @@ class ProxyClientTest extends Specification { | |
def registry = lookupService.lookup(REG) | ||
def creds = credentialsProvider.getDefaultCredentials(REG) | ||
and: | ||
def proxy = new ProxyClient(config) | ||
def proxy = new ProxyClient(httpClient) | ||
.withImage(IMAGE) | ||
.withRegistry(registry) | ||
.withLoginService(loginService) | ||
|
@@ -94,7 +97,7 @@ class ProxyClientTest extends Specification { | |
def registry = lookupService.lookup(REG) | ||
def creds = credentialsProvider.getDefaultCredentials(REG) | ||
and: | ||
def proxy = new ProxyClient(config) | ||
def proxy = new ProxyClient(httpClient) | ||
.withImage(IMAGE) | ||
.withRegistry(registry) | ||
.withLoginService(loginService) | ||
|
@@ -127,7 +130,7 @@ class ProxyClientTest extends Specification { | |
def registry = lookupService.lookup(REG) | ||
def creds = credentialsProvider.getDefaultCredentials(REG) | ||
and: | ||
def proxy = new ProxyClient(config) | ||
def proxy = new ProxyClient(httpClient) | ||
.withImage(IMAGE) | ||
.withRegistry(registry) | ||
.withLoginService(loginService) | ||
|
@@ -147,7 +150,7 @@ class ProxyClientTest extends Specification { | |
def registry = lookupService.lookup(REG) | ||
def creds = credentialsProvider.getDefaultCredentials(REG) | ||
and: | ||
def proxy = new ProxyClient(config) | ||
def proxy = new ProxyClient(httpClient) | ||
.withImage(IMAGE) | ||
.withRegistry(registry) | ||
.withLoginService(loginService) | ||
|
@@ -208,7 +211,7 @@ class ProxyClientTest extends Specification { | |
def registry = lookupService.lookup(REG) | ||
def creds = credentialsProvider.getDefaultCredentials(REG) | ||
and: | ||
def proxy = new ProxyClient(config) | ||
def proxy = new ProxyClient(httpClient) | ||
.withImage(IMAGE) | ||
.withRegistry(registry) | ||
.withLoginService(loginService) | ||
|
Oops, something went wrong.