Skip to content

Commit 6b46132

Browse files
tmoskovitchzacmos
authored andcommitted
Use OkHttp Authenticator for auto refresh token.
1 parent a197fa3 commit 6b46132

File tree

11 files changed

+233
-913
lines changed

11 files changed

+233
-913
lines changed

src/main/java/com/twitter/clientlib/ApiClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.util.regex.Matcher;
6363
import java.util.regex.Pattern;
6464

65+
import com.twitter.clientlib.auth.AccessTokenAuthenticator;
6566
import com.twitter.clientlib.auth.Authentication;
6667
import com.twitter.clientlib.auth.HttpBasicAuth;
6768
import com.twitter.clientlib.auth.HttpBearerAuth;
@@ -75,6 +76,8 @@
7576
*/
7677
public class ApiClient {
7778

79+
public final static String OAUTH2_USER_TOKEN = "OAuth2UserToken";
80+
7881
private String basePath = "https://api.twitter.com";
7982
private boolean debugging = false;
8083
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
@@ -209,6 +212,7 @@ private void initHttpClient(List<Interceptor> interceptors) {
209212
for (Interceptor interceptor: interceptors) {
210213
builder.addInterceptor(interceptor);
211214
}
215+
builder.authenticator(new AccessTokenAuthenticator(this));
212216

213217
httpClient = builder.connectTimeout(60, TimeUnit.SECONDS)
214218
.writeTimeout(60, TimeUnit.SECONDS)

src/main/java/com/twitter/clientlib/api/ApiCommon.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,10 @@ public void setClient(ApiClient apiClient) {
3737
localVarApiClient = apiClient;
3838
}
3939

40-
protected OAuth2AccessToken refreshToken() throws ApiException {
41-
return localVarApiClient.refreshToken();
42-
}
43-
4440
protected String[] reduceAuthNames(String[] localVarAuthNames) {
4541
return localVarApiClient.reduceAuthNames(localVarAuthNames);
4642
}
4743

48-
protected boolean isOAUth2AutoRefreshToken() {
49-
return localVarApiClient.isOAUth2AutoRefreshToken();
50-
}
51-
5244
public boolean handleRateLimit(ApiException e, Integer retries) throws ApiException {
5345
boolean retryCall = false;
5446
if (e.getCode() == 429 && retries > 0) {

src/main/java/com/twitter/clientlib/api/BookmarksApi.java

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,8 @@ private okhttp3.Call getUsersIdBookmarksValidateBeforeCall(String id, Integer ma
180180
</table>
181181
*/
182182
public GenericTweetsTimelineResponse getUsersIdBookmarks(String id, Integer maxResults, String paginationToken, Set<String> expansions, Set<String> tweetFields, Set<String> userFields, Set<String> mediaFields, Set<String> placeFields, Set<String> pollFields) throws ApiException {
183-
ApiResponse<GenericTweetsTimelineResponse> localVarResp;
184-
try{
185-
localVarResp = getUsersIdBookmarksWithHttpInfo(id, maxResults, paginationToken, expansions, tweetFields, userFields, mediaFields, placeFields, pollFields);
186-
} catch (ApiException e) {
187-
if (isOAUth2AutoRefreshToken() && e.getCode() == 401) {
188-
refreshToken();
189-
localVarResp = getUsersIdBookmarksWithHttpInfo(id, maxResults, paginationToken, expansions, tweetFields, userFields, mediaFields, placeFields, pollFields);
190-
} else {
191-
throw e;
192-
}
193-
}
194-
return localVarResp != null ? localVarResp.getData() : null;
183+
ApiResponse<GenericTweetsTimelineResponse> localVarResp = getUsersIdBookmarksWithHttpInfo(id, maxResults, paginationToken, expansions, tweetFields, userFields, mediaFields, placeFields, pollFields);
184+
return localVarResp != null ? localVarResp.getData() : null;
195185
}
196186

197187
/**
@@ -240,7 +230,6 @@ public ApiResponse<GenericTweetsTimelineResponse> getUsersIdBookmarksWithHttpInf
240230
return localVarApiClient.execute(localVarCall, localVarReturnType);
241231
} catch (ApiException e) {
242232
e.setErrorObject(localVarApiClient.getJSON().getGson().fromJson(e.getResponseBody(), new TypeToken<com.twitter.clientlib.model.ProblemOrError>(){}.getType()));
243-
e.setErrorObjectType(new GenericType<com.twitter.clientlib.model.ProblemOrError>(){});
244233
throw e;
245234
}
246235
}
@@ -355,18 +344,8 @@ private okhttp3.Call postUsersIdBookmarksValidateBeforeCall(AddBookmarkRequest a
355344
</table>
356345
*/
357346
public BookmarkMutationResponse postUsersIdBookmarks(AddBookmarkRequest addBookmarkRequest, String id) throws ApiException {
358-
ApiResponse<BookmarkMutationResponse> localVarResp;
359-
try{
360-
localVarResp = postUsersIdBookmarksWithHttpInfo(addBookmarkRequest, id);
361-
} catch (ApiException e) {
362-
if (isOAUth2AutoRefreshToken() && e.getCode() == 401) {
363-
refreshToken();
364-
localVarResp = postUsersIdBookmarksWithHttpInfo(addBookmarkRequest, id);
365-
} else {
366-
throw e;
367-
}
368-
}
369-
return localVarResp != null ? localVarResp.getData() : null;
347+
ApiResponse<BookmarkMutationResponse> localVarResp = postUsersIdBookmarksWithHttpInfo(addBookmarkRequest, id);
348+
return localVarResp != null ? localVarResp.getData() : null;
370349
}
371350

372351
/**
@@ -408,7 +387,6 @@ public ApiResponse<BookmarkMutationResponse> postUsersIdBookmarksWithHttpInfo(Ad
408387
return localVarApiClient.execute(localVarCall, localVarReturnType);
409388
} catch (ApiException e) {
410389
e.setErrorObject(localVarApiClient.getJSON().getGson().fromJson(e.getResponseBody(), new TypeToken<com.twitter.clientlib.model.ProblemOrError>(){}.getType()));
411-
e.setErrorObjectType(new GenericType<com.twitter.clientlib.model.ProblemOrError>(){});
412390
throw e;
413391
}
414392
}
@@ -517,18 +495,8 @@ private okhttp3.Call usersIdBookmarksDeleteValidateBeforeCall(String id, String
517495
</table>
518496
*/
519497
public BookmarkMutationResponse usersIdBookmarksDelete(String id, String tweetId) throws ApiException {
520-
ApiResponse<BookmarkMutationResponse> localVarResp;
521-
try{
522-
localVarResp = usersIdBookmarksDeleteWithHttpInfo(id, tweetId);
523-
} catch (ApiException e) {
524-
if (isOAUth2AutoRefreshToken() && e.getCode() == 401) {
525-
refreshToken();
526-
localVarResp = usersIdBookmarksDeleteWithHttpInfo(id, tweetId);
527-
} else {
528-
throw e;
529-
}
530-
}
531-
return localVarResp != null ? localVarResp.getData() : null;
498+
ApiResponse<BookmarkMutationResponse> localVarResp = usersIdBookmarksDeleteWithHttpInfo(id, tweetId);
499+
return localVarResp != null ? localVarResp.getData() : null;
532500
}
533501

534502
/**
@@ -570,7 +538,6 @@ public ApiResponse<BookmarkMutationResponse> usersIdBookmarksDeleteWithHttpInfo(
570538
return localVarApiClient.execute(localVarCall, localVarReturnType);
571539
} catch (ApiException e) {
572540
e.setErrorObject(localVarApiClient.getJSON().getGson().fromJson(e.getResponseBody(), new TypeToken<com.twitter.clientlib.model.ProblemOrError>(){}.getType()));
573-
e.setErrorObjectType(new GenericType<com.twitter.clientlib.model.ProblemOrError>(){});
574541
throw e;
575542
}
576543
}

src/main/java/com/twitter/clientlib/api/ComplianceApi.java

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,8 @@ private okhttp3.Call createBatchComplianceJobValidateBeforeCall(CreateBatchCompl
132132
</table>
133133
*/
134134
public SingleComplianceJobResponse createBatchComplianceJob(CreateBatchComplianceJobRequest createBatchComplianceJobRequest) throws ApiException {
135-
ApiResponse<SingleComplianceJobResponse> localVarResp;
136-
try{
137-
localVarResp = createBatchComplianceJobWithHttpInfo(createBatchComplianceJobRequest);
138-
} catch (ApiException e) {
139-
if (isOAUth2AutoRefreshToken() && e.getCode() == 401) {
140-
refreshToken();
141-
localVarResp = createBatchComplianceJobWithHttpInfo(createBatchComplianceJobRequest);
142-
} else {
143-
throw e;
144-
}
145-
}
146-
return localVarResp != null ? localVarResp.getData() : null;
135+
ApiResponse<SingleComplianceJobResponse> localVarResp = createBatchComplianceJobWithHttpInfo(createBatchComplianceJobRequest);
136+
return localVarResp != null ? localVarResp.getData() : null;
147137
}
148138

149139
/**
@@ -184,7 +174,6 @@ public ApiResponse<SingleComplianceJobResponse> createBatchComplianceJobWithHttp
184174
return localVarApiClient.execute(localVarCall, localVarReturnType);
185175
} catch (ApiException e) {
186176
e.setErrorObject(localVarApiClient.getJSON().getGson().fromJson(e.getResponseBody(), new TypeToken<com.twitter.clientlib.model.ProblemOrError>(){}.getType()));
187-
e.setErrorObjectType(new GenericType<com.twitter.clientlib.model.ProblemOrError>(){});
188177
throw e;
189178
}
190179
}
@@ -284,18 +273,8 @@ private okhttp3.Call getBatchComplianceJobValidateBeforeCall(String id, final Ap
284273
</table>
285274
*/
286275
public SingleComplianceJobResponse getBatchComplianceJob(String id) throws ApiException {
287-
ApiResponse<SingleComplianceJobResponse> localVarResp;
288-
try{
289-
localVarResp = getBatchComplianceJobWithHttpInfo(id);
290-
} catch (ApiException e) {
291-
if (isOAUth2AutoRefreshToken() && e.getCode() == 401) {
292-
refreshToken();
293-
localVarResp = getBatchComplianceJobWithHttpInfo(id);
294-
} else {
295-
throw e;
296-
}
297-
}
298-
return localVarResp != null ? localVarResp.getData() : null;
276+
ApiResponse<SingleComplianceJobResponse> localVarResp = getBatchComplianceJobWithHttpInfo(id);
277+
return localVarResp != null ? localVarResp.getData() : null;
299278
}
300279

301280
/**
@@ -336,7 +315,6 @@ public ApiResponse<SingleComplianceJobResponse> getBatchComplianceJobWithHttpInf
336315
return localVarApiClient.execute(localVarCall, localVarReturnType);
337316
} catch (ApiException e) {
338317
e.setErrorObject(localVarApiClient.getJSON().getGson().fromJson(e.getResponseBody(), new TypeToken<com.twitter.clientlib.model.ProblemOrError>(){}.getType()));
339-
e.setErrorObjectType(new GenericType<com.twitter.clientlib.model.ProblemOrError>(){});
340318
throw e;
341319
}
342320
}
@@ -445,18 +423,8 @@ private okhttp3.Call listBatchComplianceJobsValidateBeforeCall(ComplianceJobType
445423
</table>
446424
*/
447425
public MultiComplianceJobResponse listBatchComplianceJobs(ComplianceJobType type, ComplianceJobStatus status) throws ApiException {
448-
ApiResponse<MultiComplianceJobResponse> localVarResp;
449-
try{
450-
localVarResp = listBatchComplianceJobsWithHttpInfo(type, status);
451-
} catch (ApiException e) {
452-
if (isOAUth2AutoRefreshToken() && e.getCode() == 401) {
453-
refreshToken();
454-
localVarResp = listBatchComplianceJobsWithHttpInfo(type, status);
455-
} else {
456-
throw e;
457-
}
458-
}
459-
return localVarResp != null ? localVarResp.getData() : null;
426+
ApiResponse<MultiComplianceJobResponse> localVarResp = listBatchComplianceJobsWithHttpInfo(type, status);
427+
return localVarResp != null ? localVarResp.getData() : null;
460428
}
461429

462430
/**
@@ -498,7 +466,6 @@ public ApiResponse<MultiComplianceJobResponse> listBatchComplianceJobsWithHttpIn
498466
return localVarApiClient.execute(localVarCall, localVarReturnType);
499467
} catch (ApiException e) {
500468
e.setErrorObject(localVarApiClient.getJSON().getGson().fromJson(e.getResponseBody(), new TypeToken<com.twitter.clientlib.model.ProblemOrError>(){}.getType()));
501-
e.setErrorObjectType(new GenericType<com.twitter.clientlib.model.ProblemOrError>(){});
502469
throw e;
503470
}
504471
}

src/main/java/com/twitter/clientlib/api/GeneralApi.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,8 @@ private okhttp3.Call getOpenApiSpecValidateBeforeCall(final ApiCallback _callbac
116116
</table>
117117
*/
118118
public Object getOpenApiSpec() throws ApiException {
119-
ApiResponse<Object> localVarResp;
120-
try{
121-
localVarResp = getOpenApiSpecWithHttpInfo();
122-
} catch (ApiException e) {
123-
if (isOAUth2AutoRefreshToken() && e.getCode() == 401) {
124-
refreshToken();
125-
localVarResp = getOpenApiSpecWithHttpInfo();
126-
} else {
127-
throw e;
128-
}
129-
}
130-
return localVarResp != null ? localVarResp.getData() : null;
119+
ApiResponse<Object> localVarResp = getOpenApiSpecWithHttpInfo();
120+
return localVarResp != null ? localVarResp.getData() : null;
131121
}
132122

133123
/**
@@ -166,7 +156,6 @@ public ApiResponse<Object> getOpenApiSpecWithHttpInfo() throws ApiException {
166156
return localVarApiClient.execute(localVarCall, localVarReturnType);
167157
} catch (ApiException e) {
168158
e.setErrorObject(localVarApiClient.getJSON().getGson().fromJson(e.getResponseBody(), new TypeToken<com.twitter.clientlib.model.ProblemOrError>(){}.getType()));
169-
e.setErrorObjectType(new GenericType<com.twitter.clientlib.model.ProblemOrError>(){});
170159
throw e;
171160
}
172161
}

0 commit comments

Comments
 (0)