Skip to content

Commit e1833e7

Browse files
committed
add support for PRO and DEMO tokens
1 parent d5e86fa commit e1833e7

File tree

8 files changed

+164
-54
lines changed

8 files changed

+164
-54
lines changed

.github/workflows/maven-publish-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
cat <(echo -e "${{ secrets.OSSRH_GPG_SECRET_KEY }}") | gpg --batch --import
1414
# Verify gpg secret key
1515
gpg --list-secret-keys --keyid-format LONG
16-
- uses: actions/checkout@v3
16+
- uses: actions/checkout@v4
1717
- name: Set up JDK
1818
uses: actions/setup-java@v3
1919
with:

.github/workflows/maven.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
build:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v3
15+
- uses: actions/checkout@v4
1616
- name: Set up JDK
1717
uses: actions/setup-java@v3
1818
with:

pom.xml

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

77
<groupId>net.osslabz</groupId>
88
<artifactId>coingecko-java</artifactId>
9-
<version>1.0.1-SNAPSHOT</version>
9+
<version>1.1.0</version>
1010

1111
<name>${project.groupId}:${project.artifactId}</name>
1212
<description>Java wrapper for the CoinGecko API</description>
@@ -24,6 +24,7 @@
2424
<name>MIT License</name>
2525
<url>https://opensource.org/license/mit/</url>
2626
</license>
27+
2728
</licenses>
2829

2930
<developers>
@@ -50,6 +51,17 @@
5051
<version>${retrofit.version}</version>
5152
</dependency>
5253

54+
<dependency>
55+
<groupId>com.squareup.okhttp3</groupId>
56+
<artifactId>logging-interceptor</artifactId>
57+
<version>4.12.0</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.slf4j</groupId>
61+
<artifactId>slf4j-api</artifactId>
62+
<version>2.0.9</version>
63+
</dependency>
64+
5365
<dependency>
5466
<groupId>com.squareup.retrofit2</groupId>
5567
<artifactId>converter-jackson</artifactId>
@@ -63,6 +75,14 @@
6375
<scope>provided</scope>
6476
</dependency>
6577

78+
<!-- default logging impl for tests -->
79+
<dependency>
80+
<groupId>ch.qos.logback</groupId>
81+
<artifactId>logback-classic</artifactId>
82+
<version>1.4.14</version>
83+
<scope>test</scope>
84+
</dependency>
85+
6686
</dependencies>
6787

6888
<profiles>
@@ -86,7 +106,7 @@
86106
<plugin>
87107
<groupId>org.apache.maven.plugins</groupId>
88108
<artifactId>maven-javadoc-plugin</artifactId>
89-
<version>3.5.0</version>
109+
<version>3.6.3</version>
90110
<executions>
91111
<execution>
92112
<id>attach-javadocs</id>

src/main/java/com/litesoftwares/coingecko/CoinGeckoApi.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.litesoftwares.coingecko;
22

3+
import com.litesoftwares.coingecko.constant.TokenType;
4+
import com.litesoftwares.coingecko.domain.ApiToken;
35
import com.litesoftwares.coingecko.exception.CoinGeckoApiException;
46
import okhttp3.OkHttpClient;
7+
import okhttp3.Request;
8+
import okhttp3.logging.HttpLoggingInterceptor;
59
import retrofit2.Call;
610
import retrofit2.Response;
711
import retrofit2.Retrofit;
@@ -12,20 +16,38 @@
1216
import java.util.concurrent.TimeUnit;
1317

1418
public class CoinGeckoApi {
15-
private final String API_BASE_URL = "https://api.coingecko.com/api/v3/";
19+
20+
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CoinGeckoApi.class);
21+
private static final String API_BASE_URL_PUBLIC = "https://api.coingecko.com/api/v3/";
22+
23+
private static final String API_BASE_URL_PRO = "https://pro-api.coingecko.com/api/v3/";
1624

1725
private OkHttpClient okHttpClient = null;
1826
private Retrofit retrofit = null;
1927

20-
public <S> S createService(Class<S> serviceClass, Long connectionTimeoutSeconds, Long readTimeoutSeconds, Long writeTimeoutSeconds){
21-
okHttpClient = new OkHttpClient.Builder()
28+
public <S> S createService(Class<S> serviceClass, Long connectionTimeoutSeconds, Long readTimeoutSeconds, Long writeTimeoutSeconds, ApiToken apiToken) {
29+
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder()
2230
.connectTimeout(connectionTimeoutSeconds, TimeUnit.SECONDS)
2331
.readTimeout(readTimeoutSeconds, TimeUnit.SECONDS)
24-
.writeTimeout(writeTimeoutSeconds, TimeUnit.SECONDS)
25-
.build();
32+
.writeTimeout(writeTimeoutSeconds, TimeUnit.SECONDS);
33+
34+
if (apiToken != null) {
35+
httpClientBuilder.addInterceptor(chain -> {
36+
Request original = chain.request();
37+
Request request = original.newBuilder()
38+
.header(apiToken.getType().getHeaderName(), apiToken.getValue())
39+
.method(original.method(), original.body())
40+
.build();
41+
42+
return chain.proceed(request);
43+
});
44+
}
45+
httpClientBuilder.addInterceptor(new HttpLoggingInterceptor(log::trace).setLevel(HttpLoggingInterceptor.Level.HEADERS));
46+
47+
okHttpClient = httpClientBuilder.build();
2648

2749
retrofit = new Retrofit.Builder()
28-
.baseUrl(API_BASE_URL)
50+
.baseUrl(apiToken != null && TokenType.PRO.equals(apiToken.getType()) ? API_BASE_URL_PRO : API_BASE_URL_PUBLIC)
2951
.client(okHttpClient)
3052
.addConverterFactory(JacksonConverterFactory.create())
3153
.build();
@@ -38,7 +60,7 @@ public <T> T executeSync(Call<T> call) {
3860
Response<T> response = call.execute();
3961
if (response.isSuccessful()) {
4062
return response.body();
41-
} else if(response.code() == 429) {
63+
} else if (response.code() == 429) {
4264
// When the client gets rate limited the response is a CloudFlare error page,
4365
// not a regular error body.
4466
CoinGeckoApiError apiError = new CoinGeckoApiError();
@@ -68,9 +90,9 @@ public void shutdown() {
6890
}
6991
}
7092

71-
private CoinGeckoApiError getCoinGeckoApiError(Response<?> response) throws IOException{
72-
return (CoinGeckoApiError) retrofit.responseBodyConverter(CoinGeckoApiError.class,new Annotation[0])
93+
private CoinGeckoApiError getCoinGeckoApiError(Response<?> response) throws IOException {
94+
return (CoinGeckoApiError) retrofit.responseBodyConverter(CoinGeckoApiError.class, new Annotation[0])
7395
.convert(response.errorBody());
7496

7597
}
76-
}
98+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.litesoftwares.coingecko.constant;
2+
3+
public enum TokenType {
4+
DEMO("x-cg-demo-api-key"),
5+
PRO("x-cg-demo-api-key");
6+
7+
private final String headerName;
8+
9+
TokenType(String headerName) {
10+
this.headerName = headerName;
11+
}
12+
13+
public String getHeaderName() {
14+
return headerName;
15+
}
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.litesoftwares.coingecko.domain;
2+
3+
import com.litesoftwares.coingecko.constant.TokenType;
4+
5+
public class ApiToken {
6+
7+
private TokenType type;
8+
9+
private String value;
10+
11+
public ApiToken(TokenType type, String value) {
12+
this.type = type;
13+
this.value = value;
14+
}
15+
16+
public static ApiToken demo(String value) {
17+
return new ApiToken(TokenType.DEMO, value);
18+
}
19+
20+
public static ApiToken pro(String value) {
21+
return new ApiToken(TokenType.PRO, value);
22+
}
23+
24+
public TokenType getType() {
25+
return type;
26+
}
27+
28+
public String getValue() {
29+
return value;
30+
}
31+
}

0 commit comments

Comments
 (0)