Skip to content

Commit

Permalink
add support for PRO and DEMO tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
rvullriede committed Feb 29, 2024
1 parent d5e86fa commit e1833e7
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven-publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
cat <(echo -e "${{ secrets.OSSRH_GPG_SECRET_KEY }}") | gpg --batch --import
# Verify gpg secret key
gpg --list-secret-keys --keyid-format LONG
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v3
with:
Expand Down
24 changes: 22 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.osslabz</groupId>
<artifactId>coingecko-java</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.1.0</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>Java wrapper for the CoinGecko API</description>
Expand All @@ -24,6 +24,7 @@
<name>MIT License</name>
<url>https://opensource.org/license/mit/</url>
</license>

</licenses>

<developers>
Expand All @@ -50,6 +51,17 @@
<version>${retrofit.version}</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.12.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>

<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
Expand All @@ -63,6 +75,14 @@
<scope>provided</scope>
</dependency>

<!-- default logging impl for tests -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.14</version>
<scope>test</scope>
</dependency>

</dependencies>

<profiles>
Expand All @@ -86,7 +106,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<version>3.6.3</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
42 changes: 32 additions & 10 deletions src/main/java/com/litesoftwares/coingecko/CoinGeckoApi.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.litesoftwares.coingecko;

import com.litesoftwares.coingecko.constant.TokenType;
import com.litesoftwares.coingecko.domain.ApiToken;
import com.litesoftwares.coingecko.exception.CoinGeckoApiException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
Expand All @@ -12,20 +16,38 @@
import java.util.concurrent.TimeUnit;

public class CoinGeckoApi {
private final String API_BASE_URL = "https://api.coingecko.com/api/v3/";

private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CoinGeckoApi.class);
private static final String API_BASE_URL_PUBLIC = "https://api.coingecko.com/api/v3/";

private static final String API_BASE_URL_PRO = "https://pro-api.coingecko.com/api/v3/";

private OkHttpClient okHttpClient = null;
private Retrofit retrofit = null;

public <S> S createService(Class<S> serviceClass, Long connectionTimeoutSeconds, Long readTimeoutSeconds, Long writeTimeoutSeconds){
okHttpClient = new OkHttpClient.Builder()
public <S> S createService(Class<S> serviceClass, Long connectionTimeoutSeconds, Long readTimeoutSeconds, Long writeTimeoutSeconds, ApiToken apiToken) {
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder()
.connectTimeout(connectionTimeoutSeconds, TimeUnit.SECONDS)
.readTimeout(readTimeoutSeconds, TimeUnit.SECONDS)
.writeTimeout(writeTimeoutSeconds, TimeUnit.SECONDS)
.build();
.writeTimeout(writeTimeoutSeconds, TimeUnit.SECONDS);

if (apiToken != null) {
httpClientBuilder.addInterceptor(chain -> {
Request original = chain.request();
Request request = original.newBuilder()
.header(apiToken.getType().getHeaderName(), apiToken.getValue())
.method(original.method(), original.body())
.build();

return chain.proceed(request);
});
}
httpClientBuilder.addInterceptor(new HttpLoggingInterceptor(log::trace).setLevel(HttpLoggingInterceptor.Level.HEADERS));

okHttpClient = httpClientBuilder.build();

retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.baseUrl(apiToken != null && TokenType.PRO.equals(apiToken.getType()) ? API_BASE_URL_PRO : API_BASE_URL_PUBLIC)
.client(okHttpClient)
.addConverterFactory(JacksonConverterFactory.create())
.build();
Expand All @@ -38,7 +60,7 @@ public <T> T executeSync(Call<T> call) {
Response<T> response = call.execute();
if (response.isSuccessful()) {
return response.body();
} else if(response.code() == 429) {
} else if (response.code() == 429) {
// When the client gets rate limited the response is a CloudFlare error page,
// not a regular error body.
CoinGeckoApiError apiError = new CoinGeckoApiError();
Expand Down Expand Up @@ -68,9 +90,9 @@ public void shutdown() {
}
}

private CoinGeckoApiError getCoinGeckoApiError(Response<?> response) throws IOException{
return (CoinGeckoApiError) retrofit.responseBodyConverter(CoinGeckoApiError.class,new Annotation[0])
private CoinGeckoApiError getCoinGeckoApiError(Response<?> response) throws IOException {
return (CoinGeckoApiError) retrofit.responseBodyConverter(CoinGeckoApiError.class, new Annotation[0])
.convert(response.errorBody());

}
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/litesoftwares/coingecko/constant/TokenType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.litesoftwares.coingecko.constant;

public enum TokenType {
DEMO("x-cg-demo-api-key"),
PRO("x-cg-demo-api-key");

private final String headerName;

TokenType(String headerName) {
this.headerName = headerName;
}

public String getHeaderName() {
return headerName;
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/litesoftwares/coingecko/domain/ApiToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.litesoftwares.coingecko.domain;

import com.litesoftwares.coingecko.constant.TokenType;

public class ApiToken {

private TokenType type;

private String value;

public ApiToken(TokenType type, String value) {
this.type = type;
this.value = value;
}

public static ApiToken demo(String value) {
return new ApiToken(TokenType.DEMO, value);
}

public static ApiToken pro(String value) {
return new ApiToken(TokenType.PRO, value);
}

public TokenType getType() {
return type;
}

public String getValue() {
return value;
}
}
Loading

0 comments on commit e1833e7

Please sign in to comment.