Skip to content

Commit 73498f5

Browse files
committed
feat: extension support for the genereated api client
1 parent 120ec24 commit 73498f5

20 files changed

Lines changed: 558 additions & 159 deletions

File tree

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ client.log
5050
# JavaDoc
5151
/docs/javadoc/*
5252

53-
# Example Code
54-
/src/main/java/me/philippheuer/twitch4j/example/*
55-
5653
# Modules
5754
/modules/
5855

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ subprojects {
5656
// bom
5757
add("api", platform("io.github.openfeign:feign-bom:12.4"))
5858
add("api", platform("io.github.resilience4j:resilience4j-bom:2.1.0"))
59+
add("api", platform("com.fasterxml.jackson:jackson-bom:2.15.2"))
5960
}
6061
}
6162
}

common/build.gradle.kts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
`java-library`
3+
}
4+
5+
projectConfiguration {
6+
artifactId.set("common")
7+
artifactDisplayName.set("common")
8+
artifactDescription.set("common")
9+
}
10+
11+
dependencies {
12+
// annotations
13+
implementation("org.jetbrains:annotations:24.0.1")
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.primelib.primecodegenlib.java.common.api;
2+
3+
import lombok.SneakyThrows;
4+
5+
import java.util.function.Function;
6+
7+
/**
8+
* A functional interface representing a function that may throw a checked exception.
9+
* <p>
10+
* This functional interface is similar to the standard {@link Function}
11+
* interface, but it allows the implementation to throw a checked exception.
12+
*
13+
* @param <T> the type of the input to the function
14+
* @param <R> the type of the result of the function
15+
* @param <E> the type of the checked exception that may be thrown by the function
16+
*/
17+
@FunctionalInterface
18+
public interface ThrowingFunction<T, R, E extends Throwable> extends Function<T, R> {
19+
R invoke(T t) throws E;
20+
21+
@Override
22+
@SneakyThrows
23+
default R apply(T t) {
24+
return invoke(t);
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.github.primelib.primecodegenlib.java.common.util;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.ServiceLoader;
6+
7+
/**
8+
* Utility class for loading classes using JDK {@link ServiceLoader} facility.
9+
*/
10+
public class ServiceLoaderUtil {
11+
12+
/**
13+
* Method for locating available classes, using JDK {@link ServiceLoader} facility, along with module-provided SPI.
14+
* <p>
15+
* This method does not do any caching, so calls should be considered potentially expensive.
16+
*/
17+
public static <T> List<T> find(Class<T> clazz) {
18+
return find(clazz, null);
19+
}
20+
21+
/**
22+
* Method for locating available classes, using JDK {@link ServiceLoader} facility, along with module-provided SPI.
23+
* <p>
24+
* This method does not do any caching, so calls should be considered potentially expensive.
25+
*/
26+
public static <T> List<T> find(Class<T> clazz, ClassLoader classLoader) {
27+
ArrayList<T> modules = new ArrayList<>();
28+
ServiceLoader<T> loader = ServiceLoader.load(clazz, classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader);
29+
30+
for (T extension : loader) {
31+
modules.add(extension);
32+
}
33+
34+
return modules;
35+
}
36+
}

feign-blackbird/build.gradle.kts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plugins {
2+
`java-library`
3+
}
4+
5+
projectConfiguration {
6+
artifactId.set("feign-blackbird")
7+
artifactDisplayName.set("Feign - Jackson Blackbird Extension")
8+
artifactDescription.set("Feign - Jackson Blackbird Extension")
9+
}
10+
11+
dependencies {
12+
// modules
13+
api(project(":feign-common"))
14+
15+
// annotations
16+
implementation("org.jetbrains:annotations:24.0.1")
17+
18+
// feign
19+
api("io.github.openfeign:feign-core")
20+
21+
// blackbird
22+
api("com.fasterxml.jackson.module:jackson-module-blackbird")
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.primelib.primecodegenlib.java.feign.blackbird;
2+
3+
import com.fasterxml.jackson.databind.json.JsonMapper;
4+
import com.fasterxml.jackson.module.blackbird.BlackbirdModule;
5+
import io.github.primelib.primecodegenlib.java.feign.common.api.PrimeExtension;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
/**
9+
* Blackbird Module
10+
* <p>
11+
* This module adds the Blackbird Module to the Jackson ObjectMapper.
12+
*/
13+
public class BlackbirdExtension implements PrimeExtension {
14+
public BlackbirdExtension() {
15+
16+
}
17+
18+
@Override
19+
public void customizeObjectMapper(JsonMapper.@NotNull Builder builder) {
20+
builder.addModule(new BlackbirdModule());
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.github.primelib.primecodegenlib.java.feign.blackbird.BlackbirdExtension

feign-common/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ projectConfiguration {
99
}
1010

1111
dependencies {
12+
// modules
13+
api(project(":common"))
14+
1215
// annotations
1316
implementation("org.jetbrains:annotations:24.0.1")
1417

1518
// feign
1619
api("io.github.openfeign:feign-core")
20+
21+
// jackson
22+
compileOnly("com.fasterxml.jackson.core:jackson-databind")
1723
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.github.primelib.primecodegenlib.java.feign.common.api;
2+
3+
import com.fasterxml.jackson.databind.json.JsonMapper;
4+
import feign.InvocationHandlerFactory;
5+
import feign.Target;
6+
import io.github.primelib.primecodegenlib.java.common.api.ThrowingFunction;
7+
import org.jetbrains.annotations.ApiStatus;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
import java.lang.reflect.Method;
11+
import java.util.Map;
12+
13+
/**
14+
* A Prime Extension.
15+
*/
16+
@ApiStatus.Internal
17+
public interface PrimeExtension {
18+
19+
/**
20+
* Decorates the invocations of a function.
21+
*
22+
* @param backendName the name of the backend
23+
* @param function the function to decorate
24+
* @param dispatch the dispatch
25+
* @param target the target
26+
* @return the decorated method handler
27+
*/
28+
@NotNull
29+
default ThrowingFunction<Object[], Object, Throwable> decorateFeignInvocation(String backendName, ThrowingFunction<Object[], Object, Throwable> function, Map<Method, InvocationHandlerFactory.MethodHandler> dispatch, Target<?> target) {
30+
return function;
31+
}
32+
33+
/**
34+
* Customize the Jackson ObjectMapper.
35+
*
36+
* @param builder the builder
37+
*/
38+
default void customizeObjectMapper(@NotNull JsonMapper.Builder builder) {}
39+
}

0 commit comments

Comments
 (0)