Skip to content

Commit 73e814f

Browse files
committed
Refactor ServiceGenerator
Use HTTP_1_1 as single protocol
1 parent 7ee9377 commit 73e814f

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

src/main/java/com/aaaccell/fixer/Fixer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class Fixer {
44

55
public static FixerRequestBuilder builder(String accessKey) {
6-
return new FixerRequestBuilder(FixerServiceGenerator.createService(FixerService.class), accessKey);
6+
return new FixerRequestBuilder(new FixerServiceGenerator().createService(FixerService.class), accessKey);
77
}
88

99
}

src/main/java/com/aaaccell/fixer/FixerRequestBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
public class FixerRequestBuilder {
88

9-
private FixerService fixerService;
10-
private String accessKey;
9+
private final FixerService fixerService;
10+
private final String accessKey;
1111

1212
public FixerRequestBuilder(FixerService fixerService, String accessKey) {
1313
this.fixerService = fixerService;

src/main/java/com/aaaccell/fixer/FixerServiceGenerator.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,36 @@
33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
55
import okhttp3.OkHttpClient;
6+
import okhttp3.Protocol;
67
import okhttp3.logging.HttpLoggingInterceptor;
78
import retrofit2.Retrofit;
89
import retrofit2.converter.gson.GsonConverterFactory;
910

10-
public class FixerServiceGenerator {
11-
private static final String BASE_URL = "https://data.fixer.io/api/";
12-
13-
private static final Gson gson = new GsonBuilder()
14-
.registerTypeAdapterFactory(new FixerResponseTypeAdapterFactory())
15-
.create();
16-
17-
private static final Retrofit.Builder builder
18-
= new Retrofit.Builder()
19-
.baseUrl(BASE_URL)
20-
.addConverterFactory(GsonConverterFactory.create(gson));
11+
import java.util.Collections;
2112

22-
private static Retrofit retrofit = builder.build();
23-
24-
private static final OkHttpClient.Builder httpClient
25-
= new OkHttpClient.Builder();
13+
public class FixerServiceGenerator {
14+
protected static final String BASE_URL = "https://data.fixer.io/api/";
2615

27-
private static final HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
16+
protected OkHttpClient createHttpClient() {
17+
return new OkHttpClient.Builder()
18+
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
19+
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
20+
.build();
21+
}
2822

29-
public static <S> S createService(Class<S> serviceClass) {
30-
return createService(serviceClass, HttpLoggingInterceptor.Level.BASIC);
23+
protected Gson createGson() {
24+
return new GsonBuilder()
25+
.registerTypeAdapterFactory(new FixerResponseTypeAdapterFactory())
26+
.create();
3127
}
3228

33-
public static <S> S createService(Class<S> serviceClass, HttpLoggingInterceptor.Level logLevel) {
34-
logging.setLevel(logLevel);
35-
if (!httpClient.interceptors().contains(logging)) {
36-
httpClient.addInterceptor(logging);
37-
builder.client(httpClient.build());
38-
retrofit = builder.build();
39-
}
40-
return retrofit.create(serviceClass);
29+
public <S> S createService(Class<S> serviceClass) {
30+
return new Retrofit.Builder()
31+
.baseUrl(BASE_URL)
32+
.addConverterFactory(GsonConverterFactory.create(createGson()))
33+
.client(createHttpClient())
34+
.build()
35+
.create(serviceClass);
4136
}
4237

4338
}

0 commit comments

Comments
 (0)