Skip to content

Commit 1b28b55

Browse files
committed
Use Protobuf/gRPC for JSON (de)serialization
* Switch to GRPC * Make all tests pass * Deprecate app ID/secret usage * Make solution moderation work * Change GRPC files location * Clean prints, comments, catch-prints * Fix pmd static analysis failure * Upgrade gradle * Add JDK11 to Travis * Upgrade grpc and protobuf-java-util * Add annotation compileOnly dependency * Extract all dependency version to one place * Remove kotlin dependency * Spelling fix * Upgrade all dependencies to latest version * Fix autoValue version * Remove sourceSet commented code * Deploy after finishing JDK11 run * Allow deploying alpha prereleases
1 parent 3802777 commit 1b28b55

File tree

155 files changed

+158734
-2744
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+158734
-2744
lines changed

.travis.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
branches:
22
only:
33
- master
4-
- /^\d+\.\d+\.\d+$/
4+
- /^\d+\.\d+\.\d+(-\w+\d*)*$/
55

66
sudo: true
77

@@ -10,6 +10,7 @@ language: java
1010
jdk:
1111
- oraclejdk8
1212
- oraclejdk9
13+
- oraclejdk11
1314

1415
before_cache:
1516
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
@@ -23,14 +24,15 @@ cache:
2324

2425
before_deploy:
2526
- openssl aes-256-cbc -K $encrypted_fe71053f5cc2_key -iv $encrypted_fe71053f5cc2_iv -in gradle.properties.enc -out gradle.properties -d
27+
- IS_ALPHA=false; if [[ $TRAVIS_TAG == *"alpha"* ]]; then IS_ALPHA=true; fi
2628

2729
deploy:
2830
- provider: script
2931
script:
3032
- ./gradlew bintrayUpload
3133
skip_cleanup: true
3234
on:
33-
jdk: oraclejdk9
35+
jdk: oraclejdk11
3436
tags: true
3537
repo: Clarifai/clarifai-java
3638
- provider: releases
@@ -40,6 +42,7 @@ deploy:
4042
file:
4143
- core/build/libs/core-*.jar
4244
on:
43-
jdk: oraclejdk9
45+
jdk: oraclejdk11
4446
tags: true
4547
repo: Clarifai/clarifai-java
48+
prerelease: $IS_ALPHA

build.gradle

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,26 @@ subprojects {
4141
}
4242

4343
ext.versions = [
44-
okhttp: "3.4.1",
45-
gson: "2.7",
4644
autoValue: "1.5.2",
47-
slf4j: "1.7.21",
48-
kotlin: "1.0.5-2",
49-
45+
autoValueWith: "1.0.0",
5046
customHashCodeEquals: "db3442d",
47+
grpc: "1.17.1",
48+
gson: "2.8.5",
49+
javaxAnnotationApi: "1.3.2",
50+
okhttp: "3.12.0",
51+
protobuf: "3.6.1",
52+
protobufJavaUtil: "3.6.1",
53+
54+
junit: "4.12",
55+
junitRetryRule: "cbdd972",
56+
slf4j: "1.7.21",
5157
]
5258

5359
ext.deps = [
54-
nullityAnnotations: "org.jetbrains:annotations:13.0",
55-
kotlin: [
56-
stdlib: "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin",
57-
reflect: "org.jetbrains.kotlin:kotlin-reflect:$versions.kotlin",
58-
gradlePlugin: "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin",
59-
],
60+
nullityAnnotations: "org.jetbrains:annotations:16.0.3",
6061
]
6162
}
6263

6364
task wrapper(type: Wrapper) {
64-
gradleVersion = "4.3.1"
65+
gradleVersion = "4.10.3"
6566
}

core/build.gradle

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@ dependencies {
66
compile(
77
"com.squareup.okhttp3:okhttp:$versions.okhttp",
88
"com.google.code.gson:gson:$versions.gson",
9+
"com.google.protobuf:protobuf-java:$versions.protobuf",
10+
11+
"io.grpc:grpc-netty-shaded:$versions.grpc",
12+
"io.grpc:grpc-protobuf:$versions.grpc",
13+
"io.grpc:grpc-stub:$versions.grpc",
14+
"com.google.protobuf:protobuf-java-util:$versions.protobufJavaUtil",
915
)
1016

1117
compileOnly(
1218
"com.google.auto.value:auto-value:$versions.autoValue",
1319
deps.nullityAnnotations,
1420
"com.github.kevinmost.auto-value-custom-hashcode-equals:adapter:$versions.customHashCodeEquals",
21+
"javax.annotation:javax.annotation-api:$versions.javaxAnnotationApi",
1522
)
1623

1724
apt(
1825
"com.google.auto.value:auto-value:$versions.autoValue",
19-
"com.gabrielittner.auto.value:auto-value-with:1.0.0",
26+
"com.gabrielittner.auto.value:auto-value-with:$versions.autoValueWith",
2027
"com.github.kevinmost.auto-value-custom-hashcode-equals:processor:$versions.customHashCodeEquals",
2128
)
2229
}
Lines changed: 14 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
11
package clarifai2.api;
22

33
import clarifai2.BuildConfig;
4-
import clarifai2.exception.ClarifaiException;
4+
import clarifai2.exception.ClarifaiClientClosedException;
5+
import clarifai2.exception.DeprecationException;
56
import clarifai2.internal.AutoValueTypeAdapterFactory;
67
import com.google.gson.Gson;
78
import com.google.gson.GsonBuilder;
89
import com.google.gson.JsonDeserializationContext;
910
import com.google.gson.JsonDeserializer;
1011
import com.google.gson.JsonElement;
11-
import com.google.gson.JsonObject;
1212
import okhttp3.Cache;
13-
import okhttp3.Credentials;
1413
import okhttp3.HttpUrl;
1514
import okhttp3.Interceptor;
1615
import okhttp3.OkHttpClient;
1716
import okhttp3.Request;
18-
import okhttp3.RequestBody;
1917
import okhttp3.Response;
2018
import org.jetbrains.annotations.NotNull;
2119
import org.jetbrains.annotations.Nullable;
2220

2321
import java.io.IOException;
2422
import java.lang.reflect.Type;
25-
import java.util.Collections;
26-
import java.util.concurrent.Callable;
27-
import java.util.concurrent.ExecutionException;
28-
29-
import static clarifai2.internal.InternalUtil.MEDIA_TYPE_JSON;
3023

3124
public abstract class BaseClarifaiClient implements ClarifaiClient {
3225

@@ -39,33 +32,13 @@ public abstract class BaseClarifaiClient implements ClarifaiClient {
3932
@NotNull
4033
public final HttpUrl baseURL;
4134

42-
@Nullable
43-
private final String appID;
44-
45-
@Nullable
46-
private final String appSecret;
47-
4835
@Nullable
4936
private final String apiKey;
5037

51-
@NotNull
52-
private final OkHttpClient tokenRefreshHTTPClient;
53-
54-
@Nullable
55-
private ClarifaiToken currentClarifaiToken = null;
56-
5738
private boolean closed = false;
5839

5940
BaseClarifaiClient(@NotNull ClarifaiBuilder builder) {
60-
if (builder.apiKey == null) {
61-
this.appID = notNullOrThrow(builder.appID, "appID cannot be null if apiKey is null");
62-
this.appSecret = notNullOrThrow(builder.appSecret, "appSecret cannot be null if apiKey is null");
63-
this.apiKey = null;
64-
} else {
65-
this.apiKey = builder.apiKey;
66-
this.appID = null;
67-
this.appSecret = null;
68-
}
41+
this.apiKey = builder.apiKey;
6942

7043
this.gson = vendGson();
7144

@@ -79,26 +52,12 @@ public Response intercept(Chain chain) throws IOException {
7952
final Request.Builder requestBuilder = chain.request().newBuilder()
8053
.header("X-Clarifai-Client", String.format("java:%s:%s", BuildConfig.VERSION, System.getProperty("java.version", "?")));
8154
if (closed) {
82-
throw new ClarifaiException("This " + ClarifaiClient.class.getSimpleName() + " has already been closed");
83-
}
84-
if (apiKey == null) {
85-
final ClarifaiToken credential = refreshIfNeeded();
86-
if (credential != null) {
87-
requestBuilder.addHeader("Authorization", "Bearer " + credential.getAccessToken());
88-
}
89-
return chain.proceed(requestBuilder.build());
90-
} else {
91-
requestBuilder.addHeader("Authorization", "Key " + apiKey);
92-
return chain.proceed(requestBuilder.build());
55+
throw new ClarifaiClientClosedException("This " + ClarifaiClient.class.getSimpleName() + " has already been closed");
9356
}
57+
requestBuilder.addHeader("Authorization", "Key " + apiKey);
58+
return chain.proceed(requestBuilder.build());
9459
}
9560
}).build();
96-
97-
this.tokenRefreshHTTPClient = unmodifiedClient.newBuilder().build();
98-
99-
if (apiKey == null) {
100-
refreshIfNeeded();
101-
}
10261
}
10362

10463
private static void closeOkHttpClient(@NotNull OkHttpClient client) {
@@ -114,68 +73,17 @@ private static void closeOkHttpClient(@NotNull OkHttpClient client) {
11473
}
11574

11675
@Override public boolean hasValidToken() {
117-
return currentClarifaiToken != null && System.currentTimeMillis() <= currentClarifaiToken.getExpiresAt();
76+
throw new DeprecationException(
77+
"Using app ID/secret is deprecated, and the hasValidToken method as well. Please switch to using API key. "
78+
+ "See here how: http://help.clarifai.com/api/account-related/all-about-api-keys"
79+
);
11880
}
11981

12082
@NotNull @Override public ClarifaiToken getToken() throws IllegalStateException {
121-
if (!hasValidToken()) {
122-
throw new IllegalStateException("No valid token in this " + ClarifaiClient.class.getSimpleName() + ". "
123-
+ "Use hasValidToken() to check if a token exists before invoking this method to avoid this exception.");
124-
}
125-
//noinspection ConstantConditions
126-
return currentClarifaiToken;
127-
}
128-
129-
@Nullable
130-
private ClarifaiToken refreshIfNeeded() {
131-
synchronized (this) {
132-
if (!hasValidToken()) {
133-
currentClarifaiToken = refresh();
134-
}
135-
return currentClarifaiToken;
136-
}
137-
}
138-
139-
@Nullable
140-
private ClarifaiToken refresh() {
141-
try {
142-
return tokenRefreshHTTPClient.dispatcher().executorService().invokeAny(Collections.singletonList(
143-
new Callable<ClarifaiToken>() {
144-
@Override public ClarifaiToken call() throws Exception {
145-
final Response tokenResponse = tokenRefreshHTTPClient.newCall(new Request.Builder()
146-
.url(baseURL.newBuilder().addPathSegments("v2/token").build())
147-
.header("Authorization", Credentials.basic(appID, appSecret))
148-
.header("X-Clarifai-Client", "java:" + BuildConfig.VERSION)
149-
.post(RequestBody.create(MEDIA_TYPE_JSON, "\"grant_type\":\"client_credentials\""))
150-
.build()
151-
).execute();
152-
if (tokenResponse.isSuccessful()) {
153-
final JsonObject response = gson.fromJson(tokenResponse.body().string(), JsonObject.class);
154-
return new ClarifaiToken(response.get("access_token").getAsString(),
155-
response.get("expires_in").getAsInt()
156-
);
157-
} else if (tokenResponse.code() == 401) {
158-
throw new ClarifaiException("Clarifai app ID and/or app secret are incorrect");
159-
}
160-
return null;
161-
}
162-
}
163-
));
164-
} catch (InterruptedException | ExecutionException e) {
165-
final Throwable cause = e.getCause();
166-
if (cause instanceof ClarifaiException) {
167-
throw ((ClarifaiException) cause);
168-
}
169-
return null;
170-
}
171-
}
172-
173-
@NotNull
174-
private static <T> T notNullOrThrow(@Nullable T check, String errorMsg) {
175-
if (check == null) {
176-
throw new IllegalArgumentException(errorMsg);
177-
}
178-
return check;
83+
throw new DeprecationException(
84+
"Using app ID/secret is deprecated, and the getToken method as well. Please switch to using API key. "
85+
+ "See here how: http://help.clarifai.com/api/account-related/all-about-api-keys"
86+
);
17987
}
18088

18189
@NotNull
@@ -194,8 +102,6 @@ public ClarifaiClient deserialize(JsonElement json, Type typeOfT, JsonDeserializ
194102

195103
@Override public void close() {
196104
closed = true;
197-
closeOkHttpClient(tokenRefreshHTTPClient);
198105
closeOkHttpClient(httpClient);
199106
}
200107
}
201-

core/src/main/java/clarifai2/api/ClarifaiBuilder.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package clarifai2.api;
22

3+
import clarifai2.exception.DeprecationException;
34
import okhttp3.HttpUrl;
45
import okhttp3.OkHttpClient;
56
import org.jetbrains.annotations.NotNull;
6-
import org.jetbrains.annotations.Nullable;
77

88
import java.net.URL;
99
import java.util.List;
@@ -14,13 +14,7 @@
1414

1515
public final class ClarifaiBuilder {
1616

17-
@Nullable
18-
final String appID;
19-
20-
@Nullable
21-
final String appSecret;
22-
23-
@Nullable
17+
@NotNull
2418
final String apiKey;
2519

2620
@NotNull
@@ -30,15 +24,14 @@ public final class ClarifaiBuilder {
3024
HttpUrl baseURL = HttpUrl.parse("https://api.clarifai.com");
3125

3226
public ClarifaiBuilder(@NotNull String appID, @NotNull String appSecret) {
33-
this.appID = appID;
34-
this.appSecret = appSecret;
35-
this.apiKey = null;
27+
throw new DeprecationException(
28+
"Using app ID/secret is deprecated. Please switch to using API key. See here how: " +
29+
"http://help.clarifai.com/api/account-related/all-about-api-keys"
30+
);
3631
}
3732

3833
public ClarifaiBuilder(@NotNull String apiKey) {
3934
this.apiKey = apiKey;
40-
this.appID = null;
41-
this.appSecret = null;
4235
}
4336

4437
@NotNull

0 commit comments

Comments
 (0)