-
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.
Co-authored-by: nsimonides <[email protected]> Co-authored-by: Willem Veelenturf <[email protected]>
- Loading branch information
1 parent
197b3f8
commit 0803f3d
Showing
30 changed files
with
533 additions
and
44 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
5 changes: 2 additions & 3 deletions
5
...main/java/community/flock/wirespec/examples/maven/spring/integration/TodoApplication.java
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
...java/community/flock/wirespec/examples/maven/spring/integration/client/TodoWebClient.java
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 @@ | ||
package community.flock.wirespec.examples.maven.spring.integration.client; | ||
|
||
import community.flock.wirespec.generated.examples.spring.GetTodosEndpoint; | ||
import community.flock.wirespec.integration.spring.java.client.WirespecWebClient; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Component | ||
public class TodoWebClient implements GetTodosEndpoint.Handler { | ||
|
||
private final WirespecWebClient wirespecWebClient; | ||
|
||
@Autowired | ||
public TodoWebClient(WirespecWebClient wirespecWebClient) { | ||
this.wirespecWebClient = wirespecWebClient; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<GetTodosEndpoint.Response<?>> getTodos(GetTodosEndpoint.Request request) { | ||
return wirespecWebClient.send(request); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...mmunity/flock/wirespec/examples/maven/spring/integration/client/WirespecClientConfig.java
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,16 @@ | ||
package community.flock.wirespec.examples.maven.spring.integration.client; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
@Configuration | ||
public class WirespecClientConfig { | ||
|
||
@Bean("wirespecSpringWebClient") | ||
public WebClient webClient( | ||
WebClient.Builder builder | ||
) { | ||
return builder.baseUrl("http://localhost:8080").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
2 changes: 1 addition & 1 deletion
2
...maven/spring/integration/TodoService.java → ...ring/integration/service/TodoService.java
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
71 changes: 71 additions & 0 deletions
71
...mMain/kotlin/community/flock/wirespec/integration/spring/java/client/WirespecWebClient.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,71 @@ | ||
package community.flock.wirespec.integration.spring.java.client | ||
|
||
import community.flock.wirespec.java.Wirespec | ||
import community.flock.wirespec.java.Wirespec.Serialization | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import org.springframework.web.reactive.function.client.WebClientResponseException | ||
import reactor.core.publisher.Mono | ||
import java.util.concurrent.CompletableFuture | ||
import kotlin.reflect.full.companionObjectInstance | ||
|
||
class WirespecWebClient( | ||
private val client: WebClient, | ||
private val wirespecSerde: Serialization<String>, | ||
) { | ||
fun <Req : Wirespec.Request<*>, Res : Wirespec.Response<*>> send( | ||
request: Req, | ||
): CompletableFuture<Res> { | ||
val declaringClass= request::class.java.declaringClass | ||
val handler = declaringClass.declaredClasses.toList() | ||
.find { it.simpleName == "Handler" } | ||
?: error("Handler not found") | ||
|
||
val instance = handler.kotlin.companionObjectInstance as Wirespec.Client<Req, Res> | ||
|
||
return with(instance.getClient(wirespecSerde)) { | ||
executeRequest(to(request), client).thenApply { | ||
from(it) | ||
} | ||
} | ||
} | ||
|
||
private fun executeRequest( | ||
request: Wirespec.RawRequest, | ||
client: WebClient, | ||
): CompletableFuture<Wirespec.RawResponse> = client | ||
.method(HttpMethod.valueOf(request.method)) | ||
.uri { uriBuilder -> | ||
uriBuilder | ||
.path(request.path.joinToString("/")) | ||
.apply { request.queries.forEach { (key, value) -> queryParam(key, value) } } | ||
.build() | ||
} | ||
.headers { headers -> | ||
request.headers.forEach { (key, value) -> headers.add(key, value) } | ||
} | ||
.bodyValue(request.body) | ||
.exchangeToMono { response -> | ||
response.bodyToMono(String::class.java) | ||
.map { body -> | ||
Wirespec.RawResponse( | ||
response.statusCode().value(), | ||
response.headers().asHttpHeaders().toSingleValueMap(), | ||
body | ||
) | ||
} | ||
} | ||
.onErrorResume { throwable -> | ||
when (throwable) { | ||
is WebClientResponseException -> | ||
Wirespec.RawResponse( | ||
throwable.statusCode.value(), | ||
throwable.headers.toSingleValueMap(), | ||
throwable.responseBodyAsString | ||
).let { Mono.just(it) } | ||
|
||
else -> Mono.error(throwable) | ||
} | ||
}.toFuture() | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...n/kotlin/community/flock/wirespec/integration/spring/java/configuration/EnableWirespec.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,8 @@ | ||
package community.flock.wirespec.integration.spring.java.configuration | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@MustBeDocumented | ||
@EnableWirespecController | ||
@EnableWirespecWebClient | ||
annotation class EnableWirespec |
9 changes: 9 additions & 0 deletions
9
...ommunity/flock/wirespec/integration/spring/java/configuration/EnableWirespecController.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,9 @@ | ||
package community.flock.wirespec.integration.spring.java.configuration | ||
|
||
import org.springframework.context.annotation.Import | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@MustBeDocumented | ||
@Import(WirespecSerializationConfiguration::class) | ||
annotation class EnableWirespecController |
9 changes: 9 additions & 0 deletions
9
...community/flock/wirespec/integration/spring/java/configuration/EnableWirespecWebClient.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,9 @@ | ||
package community.flock.wirespec.integration.spring.java.configuration | ||
|
||
import org.springframework.context.annotation.Import | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@MustBeDocumented | ||
@Import(WirespecSerializationConfiguration::class, WirespecWebClientConfiguration::class) | ||
annotation class EnableWirespecWebClient |
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
Oops, something went wrong.