Skip to content

Commit

Permalink
added requirements to Micronaut HTTP client implementation (#98)
Browse files Browse the repository at this point in the history
* added requirements to Micronaut HTTP client implementation

* moved client initialization to separate factories
  • Loading branch information
musketyr authored Dec 6, 2023
1 parent 63b5829 commit e4dafc5
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@

import com.agorapulse.gru.Client;
import com.agorapulse.gru.Gru;
import com.agorapulse.gru.http.Http;
import com.agorapulse.gru.micronaut.http.MicronautHttpClient;
import com.agorapulse.gru.okhttp.OkHttp;
import io.micronaut.context.ApplicationContext;

import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;

import io.micronaut.context.annotation.Secondary;
import io.micronaut.http.client.HttpClient;
import jakarta.inject.Singleton;

/**
Expand All @@ -46,40 +41,6 @@ public Gru gru(Client client) {
return Gru.create(client);
}

@Singleton
@Secondary
@SuppressWarnings({"rawtypes", "unchecked"})
public Client client(ApplicationContext context, @io.micronaut.http.client.annotation.Client("gru") HttpClient client) {
Class testClass = context.getRequiredProperty(TEST_CLASS_PROPERTY_NAME, Class.class);
return Micronaut.createLazy(
test -> MicronautHttpClient.create(test, client),
() -> context.getBean(testClass),
() -> context
);
}

@Singleton
@SuppressWarnings({"rawtypes", "unchecked"})
@Requires(property = "gru.http.client", value = "okhttp", classes = OkHttp.class)
public Client okHttpClient(ApplicationContext context) {
Class testClass = context.getRequiredProperty(TEST_CLASS_PROPERTY_NAME, Class.class);
return Micronaut.createLazy(OkHttp::create,
() -> context.getBean(testClass),
() -> context
);
}

@Singleton
@SuppressWarnings({"rawtypes", "unchecked"})
@Requires(property = "gru.http.client", value = "jdk")
public Client jdkHttpClient(ApplicationContext context) {
Class testClass = context.getRequiredProperty(TEST_CLASS_PROPERTY_NAME, Class.class);
return Micronaut.createLazy(Http::create,
() -> context.getBean(testClass),
() -> context
);
}

@Singleton
@Bean(preDestroy = "close")
@Requires(classes = com.agorapulse.gru.kotlin.Gru.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2023 Agorapulse.
*
* 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
*
* https://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.agorapulse.gru.micronaut;

import com.agorapulse.gru.Client;
import com.agorapulse.gru.http.Http;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import jakarta.inject.Singleton;

/**
* Factory that creates {@link java.net.http.HttpClient} based implementation of the HTTP client.
*/
@Factory
@Requires(property = GruFactory.TEST_CLASS_PROPERTY_NAME)
public class JdkClientFactory {

@Bean
@Singleton
@SuppressWarnings({"rawtypes", "unchecked"})
public Client jdkHttpClient(ApplicationContext context) {
Class testClass = context.getRequiredProperty(GruFactory.TEST_CLASS_PROPERTY_NAME, Class.class);
return Micronaut.createLazy(Http::create,
() -> context.getBean(testClass),
() -> context
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2023 Agorapulse.
*
* 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
*
* https://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.agorapulse.gru.micronaut;

import com.agorapulse.gru.Client;
import com.agorapulse.gru.micronaut.http.MicronautHttpClient;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.annotation.*;
import io.micronaut.http.client.HttpClient;
import jakarta.inject.Singleton;

/**
* Factory that creates {@link HttpClient} implementation of the HTTP client.
*/
@Factory
@Requires(property = GruFactory.TEST_CLASS_PROPERTY_NAME)
@Requires(classes = HttpClient.class)
@Replaces(factory = JdkClientFactory.class)
public class MicronautHttpClientFactory {

@Bean
@Singleton
@Secondary
@SuppressWarnings({"rawtypes", "unchecked"})
public Client client(ApplicationContext context, @io.micronaut.http.client.annotation.Client("gru") HttpClient client) {
Class testClass = context.getRequiredProperty(GruFactory.TEST_CLASS_PROPERTY_NAME, Class.class);
return Micronaut.createLazy(
test -> MicronautHttpClient.create(test, client),
() -> context.getBean(testClass),
() -> context
);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2023 Agorapulse.
*
* 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
*
* https://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.agorapulse.gru.micronaut;

import com.agorapulse.gru.Client;
import com.agorapulse.gru.okhttp.OkHttp;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Replaces;
import io.micronaut.context.annotation.Requires;
import jakarta.inject.Singleton;

/**
* Factory that creates OkHttp implementation of the HTTP client.
*/
@Factory
@Requires(property = GruFactory.TEST_CLASS_PROPERTY_NAME)
@Requires(classes = OkHttp.class)
@Replaces(factory = JdkClientFactory.class)
@Requires(property = "gru.http.client", value = "okhttp", classes = OkHttp.class)
public class OkHttpClientFactory {

@Bean
@Singleton
@SuppressWarnings({"rawtypes", "unchecked"})
@Requires(property = "gru.http.client", value = "okhttp", classes = OkHttp.class)
public Client okHttpClient(ApplicationContext context) {
Class testClass = context.getRequiredProperty(GruFactory.TEST_CLASS_PROPERTY_NAME, Class.class);
return Micronaut.createLazy(OkHttp::create,
() -> context.getBean(testClass),
() -> context
);
}

}

0 comments on commit e4dafc5

Please sign in to comment.