Class RatpackRetrofit


  • public abstract class RatpackRetrofit
    extends Object
    Builder for providing integration of Retrofit2 with Ratpack's 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.jupiter.api.Assertions.*;
    
     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();
         });
       }
     }
     
    Since:
    1.4
    • Constructor Detail

      • RatpackRetrofit

        public RatpackRetrofit()
    • Method Detail

      • client

        public static RatpackRetrofit.Builder client​(URI endpoint)
        Creates a new builder for creating Retrofit clients.
        Parameters:
        endpoint - the endpoint for client implementations.
        Returns:
        a client builder
      • client

        public static RatpackRetrofit.Builder client​(String endpoint)
        Creates a new builder for creating Retrofit clients.
        Parameters:
        endpoint - the endpoint for client implementations. Converted to URI.
        Returns:
        a client builder