Class RatpackRetrofit


  • public abstract class RatpackRetrofit
    extends java.lang.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.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();
         });
       }
     }
     
    Since:
    1.4
    • Constructor Summary

      Constructors 
      Constructor Description
      RatpackRetrofit()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static RatpackRetrofit.Builder client​(java.lang.String endpoint)
      Creates a new builder for creating Retrofit clients.
      static RatpackRetrofit.Builder client​(java.net.URI endpoint)
      Creates a new builder for creating Retrofit clients.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RatpackRetrofit

        public RatpackRetrofit()
    • Method Detail

      • client

        public static RatpackRetrofit.Builder client​(java.net.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​(java.lang.String endpoint)
        Creates a new builder for creating Retrofit clients.
        Parameters:
        endpoint - the endpoint for client implementations. Converted to URI.
        Returns:
        a client builder