Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #69

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/com/twitter/clientlib/ServerConfiguration.java
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ public ServerConfiguration(String URL, String description, Map<String, ServerVar
* @param variables A map between a variable name and its value.
* @return Formatted URL.
*/
public String URL(Map<String, String> variables) {
public String formatUrl(Map<String, String> variables) {
String url = this.URL;

// go through variables and replace placeholders
@@ -52,7 +52,7 @@ public String URL(Map<String, String> variables) {
*
* @return Formatted URL.
*/
public String URL() {
return URL(null);
public String formatUrl() {
return formatUrl(null);
}
}
15 changes: 10 additions & 5 deletions src/main/java/com/twitter/clientlib/api/ApiCommon.java
Original file line number Diff line number Diff line change
@@ -44,7 +44,10 @@ protected String[] reduceAuthNames(String[] localVarAuthNames) {

public boolean handleRateLimit(ApiException e, Integer retries) throws ApiException {
boolean retryCall = false;
if (e.getCode() == 429 && retries > 0) {
boolean isRateLimitExceeded = (e.getCode() == 429);
boolean hasRetriesLeft = (retries > 0);

if (isRateLimitExceeded && hasRetriesLeft) {
long timeToWait = getTimeToWait(e);
try {
Thread.sleep(timeToWait);
@@ -63,7 +66,7 @@ long getTimeToWait(ApiException e) {
List<String> xRateLimitReset = e.getResponseHeaders().get("x-rate-limit-reset");
if (xRateLimitReset != null && xRateLimitReset.get(0) != null) {
timeToWait = Long.parseLong(
xRateLimitReset.get(0)) * 1000 - Calendar.getInstance().getTimeInMillis();
xRateLimitReset.get(0)) * 1000 - Calendar.getInstance().getTimeInMillis();
}
}
return timeToWait;
@@ -72,13 +75,15 @@ long getTimeToWait(ApiException e) {
boolean isRateLimitRemaining(ApiException e) {
List<String> xRateLimitRemaining = e.getResponseHeaders().get("x-rate-limit-remaining");
return xRateLimitRemaining != null && xRateLimitRemaining.get(0) != null
&& Long.parseLong(xRateLimitRemaining.get(0)) == 0;
&& Long.parseLong(xRateLimitRemaining.get(0)) == 0;
}

Set<String> getFields(String fieldName, boolean isExclude, Set<String> fieldValues, Set<String> allFieldsValues) {
Set<String> result = fieldValues;
if(isExclude && SDKConfig.isFieldAllowlisted(fieldName) && allFieldsValues != null) {
result = allFieldsValues.stream().filter(e -> !(fieldValues != null && fieldValues.contains(e))).collect(Collectors.toSet());
if (isExclude && SDKConfig.isFieldAllowlisted(fieldName)) {
if (allFieldsValues != null) {
result = allFieldsValues.stream().filter(e -> !(fieldValues != null && fieldValues.contains(e))).collect(Collectors.toSet());
}
}
return result;
}
46 changes: 27 additions & 19 deletions src/main/java/com/twitter/clientlib/api/BookmarksApi.java
Original file line number Diff line number Diff line change
@@ -72,14 +72,31 @@ private okhttp3.Call getUsersIdBookmarksCall(String id, Integer maxResults, Stri
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();

if (maxResults != null) {
localVarQueryParams.addAll(localVarApiClient.parameterToPair("max_results", maxResults));
getLocalVarQueryParams(maxResults, paginationToken, localVarQueryParams);

getLocalVarCollectionQueryParams(tweetFields, expansions, mediaFields, pollFields, userFields, placeFields, localVarCollectionQueryParams);

final String[] localVarAccepts = {
"application/json", "application/problem+json"
};
final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts);
if (localVarAccept != null) {
localVarHeaderParams.put("Accept", localVarAccept);
}

if (paginationToken != null) {
localVarQueryParams.addAll(localVarApiClient.parameterToPair("pagination_token", paginationToken));
final String[] localVarContentTypes = {

};
final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes);
if (localVarContentType != null) {
localVarHeaderParams.put("Content-Type", localVarContentType);
}

String[] localVarAuthNames = new String[] { "OAuth2UserToken" };
return localVarApiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, reduceAuthNames(localVarAuthNames), _callback);
}

private void getLocalVarCollectionQueryParams(Set<String> tweetFields, Set<String> expansions, Set<String> mediaFields, Set<String> pollFields, Set<String> userFields, Set<String> placeFields, List<Pair> localVarCollectionQueryParams) {
if (tweetFields != null) {
localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("csv", "tweet.fields", tweetFields));
}
@@ -103,25 +120,16 @@ private okhttp3.Call getUsersIdBookmarksCall(String id, Integer maxResults, Stri
if (placeFields != null) {
localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("csv", "place.fields", placeFields));
}
}

final String[] localVarAccepts = {
"application/json", "application/problem+json"
};
final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts);
if (localVarAccept != null) {
localVarHeaderParams.put("Accept", localVarAccept);
private void getLocalVarQueryParams(Integer maxResults, String paginationToken, List<Pair> localVarQueryParams) {
if (maxResults != null) {
localVarQueryParams.addAll(localVarApiClient.parameterToPair("max_results", maxResults));
}

final String[] localVarContentTypes = {

};
final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes);
if (localVarContentType != null) {
localVarHeaderParams.put("Content-Type", localVarContentType);
if (paginationToken != null) {
localVarQueryParams.addAll(localVarApiClient.parameterToPair("pagination_token", paginationToken));
}

String[] localVarAuthNames = new String[] { "OAuth2UserToken" };
return localVarApiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, reduceAuthNames(localVarAuthNames), _callback);
}

@SuppressWarnings("rawtypes")
22 changes: 12 additions & 10 deletions src/main/java/com/twitter/clientlib/api/ComplianceApi.java
Original file line number Diff line number Diff line change
@@ -288,16 +288,7 @@ private okhttp3.Call getBatchComplianceJobValidateBeforeCall(String id, Set<Stri
}


private ApiResponse<Get2ComplianceJobsIdResponse> getBatchComplianceJobWithHttpInfo(String id, Set<String> complianceJobFields) throws ApiException {
okhttp3.Call localVarCall = getBatchComplianceJobValidateBeforeCall(id, complianceJobFields, null);
try {
Type localVarReturnType = new TypeToken<Get2ComplianceJobsIdResponse>(){}.getType();
return localVarApiClient.execute(localVarCall, localVarReturnType);
} catch (ApiException e) {
e.setErrorObject(localVarApiClient.getJSON().getGson().fromJson(e.getResponseBody(), new TypeToken<com.twitter.clientlib.model.ProblemOrError>(){}.getType()));
throw e;
}
}


private okhttp3.Call getBatchComplianceJobAsync(String id, Set<String> complianceJobFields, final ApiCallback<Get2ComplianceJobsIdResponse> _callback) throws ApiException {

@@ -413,6 +404,17 @@ public ApiResponse<Get2ComplianceJobsIdResponse> executeWithHttpInfo() throws Ap
public okhttp3.Call executeAsync(final ApiCallback<Get2ComplianceJobsIdResponse> _callback) throws ApiException {
return getBatchComplianceJobAsync(id, complianceJobFields, _callback);
}

private ApiResponse<Get2ComplianceJobsIdResponse> getBatchComplianceJobWithHttpInfo(String id, Set<String> complianceJobFields) throws ApiException {
okhttp3.Call localVarCall = getBatchComplianceJobValidateBeforeCall(id, complianceJobFields, null);
try {
Type localVarReturnType = new TypeToken<Get2ComplianceJobsIdResponse>(){}.getType();
return localVarApiClient.execute(localVarCall, localVarReturnType);
} catch (ApiException e) {
e.setErrorObject(localVarApiClient.getJSON().getGson().fromJson(e.getResponseBody(), new TypeToken<com.twitter.clientlib.model.ProblemOrError>(){}.getType()));
throw e;
}
}
}

/**
Original file line number Diff line number Diff line change
@@ -23,39 +23,24 @@
package com.twitter.clientlib.model;

import java.util.Objects;
import java.util.Arrays;

import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.twitter.clientlib.model.Expansions;
import com.twitter.clientlib.model.Get2ComplianceJobsResponseMeta;
import com.twitter.clientlib.model.Problem;
import com.twitter.clientlib.model.Space;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.twitter.clientlib.JSON;

@@ -65,7 +50,8 @@
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class Get2SpacesSearchResponse {
public static final String SERIALIZED_NAME_DATA = "data";
@SerializedName(SERIALIZED_NAME_DATA)
private final StringOperation stringOperation = new StringOperation(this);
@SerializedName(SERIALIZED_NAME_DATA)
private List<Space> data = null;

public static final String SERIALIZED_NAME_ERRORS = "errors";
@@ -194,45 +180,25 @@ public void setMeta(Get2ComplianceJobsResponseMeta meta) {

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Get2SpacesSearchResponse get2SpacesSearchResponse = (Get2SpacesSearchResponse) o;
return Objects.equals(this.data, get2SpacesSearchResponse.data) &&
Objects.equals(this.errors, get2SpacesSearchResponse.errors) &&
Objects.equals(this.includes, get2SpacesSearchResponse.includes) &&
Objects.equals(this.meta, get2SpacesSearchResponse.meta);
return stringOperation.equals(o);
}

@Override
public int hashCode() {
return Objects.hash(data, errors, includes, meta);
return stringOperation.hashCode();
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Get2SpacesSearchResponse {\n");
sb.append(" data: ").append(toIndentedString(data)).append("\n");
sb.append(" errors: ").append(toIndentedString(errors)).append("\n");
sb.append(" includes: ").append(toIndentedString(includes)).append("\n");
sb.append(" meta: ").append(toIndentedString(meta)).append("\n");
sb.append("}");
return sb.toString();
return stringOperation.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
return stringOperation.toIndentedString(o);
}


@@ -346,7 +312,67 @@ public static Get2SpacesSearchResponse fromJson(String jsonString) throws IOExce
* @return JSON string
*/
public String toJson() {
return JSON.getGson().toJson(this);
return stringOperation.toJson();
}

public static class StringOperation {
private final Get2SpacesSearchResponse get2SpacesSearchResponse;

public StringOperation(Get2SpacesSearchResponse get2SpacesSearchResponse) {
this.get2SpacesSearchResponse = get2SpacesSearchResponse;
}

@Override
public boolean equals(Object o) {
if (get2SpacesSearchResponse == o) {
return true;
}
if (o == null || get2SpacesSearchResponse.getClass() != o.getClass()) {
return false;
}
Get2SpacesSearchResponse get2SpacesSearchResponse = (Get2SpacesSearchResponse) o;
return Objects.equals(get2SpacesSearchResponse.getData(), get2SpacesSearchResponse.data) &&
Objects.equals(get2SpacesSearchResponse.getErrors(), get2SpacesSearchResponse.errors) &&
Objects.equals(get2SpacesSearchResponse.getIncludes(), get2SpacesSearchResponse.includes) &&
Objects.equals(get2SpacesSearchResponse.getMeta(), get2SpacesSearchResponse.meta);
}

@Override
public int hashCode() {
return Objects.hash(get2SpacesSearchResponse.getData(), get2SpacesSearchResponse.getErrors(), get2SpacesSearchResponse.getIncludes(), get2SpacesSearchResponse.getMeta());
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Get2SpacesSearchResponse {\n");
sb.append(" data: ").append(toIndentedString(get2SpacesSearchResponse.getData())).append("\n");
sb.append(" errors: ").append(toIndentedString(get2SpacesSearchResponse.getErrors())).append("\n");
sb.append(" includes: ").append(toIndentedString(get2SpacesSearchResponse.getIncludes())).append("\n");
sb.append(" meta: ").append(toIndentedString(get2SpacesSearchResponse.getMeta())).append("\n");
sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}

/**
* Convert an instance of Get2SpacesSearchResponse to an JSON string
*
* @return JSON string
*/
public String toJson() {
return JSON.getGson().toJson(get2SpacesSearchResponse);
}
}
}

Original file line number Diff line number Diff line change
@@ -204,10 +204,5 @@ protected void checkDuplicateRuleProblem(Problem problem, String detail, String
Assertions.assertEquals(value, duplicateProblem.getValue());
}

protected void checkFieldUnauthorizedProblem(Problem problem, String title, String field) {
Assertions.assertTrue(problem instanceof FieldUnauthorizedProblem);
FieldUnauthorizedProblem fieldUnauthorizedProblem = (FieldUnauthorizedProblem) problem;
Assertions.assertTrue(fieldUnauthorizedProblem.getTitle().equals(title));
Assertions.assertEquals(field, fieldUnauthorizedProblem.getField());
}

}
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
@@ -449,6 +450,13 @@ public void usersIdTimelineErrorTest() throws ApiException {
"Invalid Request", "One or more parameters to your request was invalid.");
}

protected void checkFieldUnauthorizedProblem(Problem problem, String title, String field) {
Assertions.assertTrue(problem instanceof FieldUnauthorizedProblem);
FieldUnauthorizedProblem fieldUnauthorizedProblem = (FieldUnauthorizedProblem) problem;
Assertions.assertTrue(fieldUnauthorizedProblem.getTitle().equals(title));
Assertions.assertEquals(field, fieldUnauthorizedProblem.getField());
}

/* Does not work for now
Streaming
*/