retrofit2.Converter.Factory Java Examples

The following examples show how to use retrofit2.Converter.Factory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JspoonConverterFactoryTest.java    From jspoon with MIT License 5 votes vote down vote up
@Test
public void testJspoonConverterAllowsRetrofitToChooseAnotherConverter() throws Exception {
    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setBody("Ahoy matey!"));

    Converter mockConverter = mock(Converter.class);
    when(mockConverter.convert(any(ResponseBody.class)))
        .thenReturn(new Item("1", "Item 1"));

    Factory mockConverterFactory = mock(Factory.class);
    when(mockConverterFactory.responseBodyConverter(
        any(Type.class),
        any(Annotation[].class),
        any(Retrofit.class))).thenReturn(mockConverter);

    NonScrapingService service = new Builder()
        .baseUrl(server.url("/"))
        .addConverterFactory(JspoonConverterFactory.create())
        .addConverterFactory(mockConverterFactory)
        .build()
        .create(NonScrapingService.class);
    Item item = service.getItem("1").execute().body();
    assertEquals(item.id, "1");
    assertEquals(item.name, "Item 1");

    verify(mockConverter, atLeastOnce()).convert(any(ResponseBody.class));
}
 
Example #2
Source File: Sender.java    From Java-fcm with MIT License 4 votes vote down vote up
private Retrofit.Builder createRetrofit(Factory factory) {
    return new Retrofit.Builder()
            .baseUrl("https://fcm.googleapis.com/fcm/")
            .client(httpClient)
            .addConverterFactory(factory);
}
 
Example #3
Source File: InfluxDBImpl.java    From influxdb-java with MIT License 4 votes vote down vote up
/**
 * Constructs a new {@code InfluxDBImpl}.
 *
 * @param url
 *          The InfluxDB server API URL
 * @param username
 *          The InfluxDB user name
 * @param password
 *          The InfluxDB user password
 * @param okHttpBuilder
 *          The OkHttp Client Builder
 * @param retrofitBuilder
 *          The Retrofit Builder
 * @param responseFormat
 *          The {@code ResponseFormat} to use for response from InfluxDB
 *          server
 */
public InfluxDBImpl(final String url, final String username, final String password,
                    final OkHttpClient.Builder okHttpBuilder, final Retrofit.Builder retrofitBuilder,
                    final ResponseFormat responseFormat) {
  this.messagePack = ResponseFormat.MSGPACK.equals(responseFormat);
  this.hostName = parseHost(url);

  this.loggingInterceptor = new HttpLoggingInterceptor();
  setLogLevel(LOG_LEVEL);

  this.gzipRequestInterceptor = new GzipRequestInterceptor();
  OkHttpClient.Builder clonedOkHttpBuilder = okHttpBuilder.build().newBuilder()
          .addInterceptor(loggingInterceptor)
          .addInterceptor(gzipRequestInterceptor);
  if (username != null && password != null) {
    clonedOkHttpBuilder.addInterceptor(new BasicAuthInterceptor(username, password));
  }
  Factory converterFactory = null;
  switch (responseFormat) {
  case MSGPACK:
    clonedOkHttpBuilder.addInterceptor(chain -> {
      Request request = chain.request().newBuilder().addHeader("Accept", APPLICATION_MSGPACK).build();
      return chain.proceed(request);
    });

    converterFactory = MessagePackConverterFactory.create();
    chunkProccesor = new MessagePackChunkProccesor();
    break;
  case JSON:
  default:
    converterFactory = MoshiConverterFactory.create();

    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<QueryResult> adapter = moshi.adapter(QueryResult.class);
    chunkProccesor = new JSONChunkProccesor(adapter);
    break;
  }

  this.client = clonedOkHttpBuilder.build();
  Retrofit.Builder clonedRetrofitBuilder = retrofitBuilder.baseUrl(url).build().newBuilder();
  this.retrofit = clonedRetrofitBuilder.client(this.client)
          .addConverterFactory(converterFactory).build();
  this.influxDBService = this.retrofit.create(InfluxDBService.class);

}