public abstract class RatpackRetrofit extends Object
HttpClient
.
This class allows for creating declarative type-safe interfaces that represent remote HTTP APIs.
Using this adapter allows for defining the interfaces to return Promise
types which will be fulfilled by Ratpack's http client.
import ratpack.exec.Promise;
import ratpack.retrofit.RatpackRetrofit;
import ratpack.test.embed.EmbeddedApp;
import retrofit2.http.GET;
import static org.junit.Assert.*;
public class ExampleRetrofitClient {
public interface HelloService {
@GET("hello") Promise<String> hello();
}
public static void main(String... args) throws Exception {
EmbeddedApp api = EmbeddedApp.of(s -> s
.handlers(chain -> chain
.get("hello", ctx -> ctx.render("hello"))
)
);
EmbeddedApp.of(s -> s
.registryOf(r -> r
.add(HelloService.class,
RatpackRetrofit.client(api.getAddress()).build(HelloService.class)
)
)
.handlers(chain -> {
chain.get(ctx -> {
ctx.render(ctx.get(HelloService.class).hello());
});
})
).test(testHttpClient -> {
assertEquals("hello", testHttpClient.getText());
api.close();
});
}
}
Modifier and Type | Class and Description |
---|---|
static class |
RatpackRetrofit.Builder |
Constructor and Description |
---|
RatpackRetrofit() |
Modifier and Type | Method and Description |
---|---|
static RatpackRetrofit.Builder |
client(String endpoint)
Creates a new builder for creating Retrofit clients.
|
static RatpackRetrofit.Builder |
client(URI endpoint)
Creates a new builder for creating Retrofit clients.
|
public static RatpackRetrofit.Builder client(URI endpoint)
endpoint
- the endpoint for client implementations.public static RatpackRetrofit.Builder client(String endpoint)
endpoint
- the endpoint for client implementations. Converted to URI
.