Skip to content

Commit

Permalink
feature: remove sendForList method and use Type class instead (th…
Browse files Browse the repository at this point in the history
…ousandeyes#85)

* [GitHub Bot] Generated java SDK

* feature: remove `sendForList` method and use `Type` class instead
+ Change headers type from List to Collection

---------

Co-authored-by: API Team <api-team@thousandeyes.com>
  • Loading branch information
jabrowne-te and API Team authored Oct 1, 2024
1 parent f0f6b95 commit 7762c40
Show file tree
Hide file tree
Showing 91 changed files with 136 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.account.management.administrative.model.AccountGroupDetail;
import com.thousandeyes.sdk.account.management.administrative.model.AccountGroupRequest;
import com.thousandeyes.sdk.account.management.administrative.model.AccountGroups;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.account.management.administrative.model.Error;
import com.thousandeyes.sdk.account.management.administrative.model.Permissions;
import com.thousandeyes.sdk.account.management.administrative.model.UnauthorizedError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.account.management.administrative.model.Error;
import com.thousandeyes.sdk.account.management.administrative.model.RoleDetail;
import com.thousandeyes.sdk.account.management.administrative.model.RoleRequestBody;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.account.management.administrative.model.AuditUserEvents;
import com.thousandeyes.sdk.account.management.administrative.model.Error;
import java.time.OffsetDateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.account.management.administrative.model.CreatedUser;
import com.thousandeyes.sdk.account.management.administrative.model.Error;
import java.net.URI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.agents.model.AgentProxies;
import com.thousandeyes.sdk.agents.model.Error;
import com.thousandeyes.sdk.agents.model.UnauthorizedError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.agents.model.Error;
import com.thousandeyes.sdk.agents.model.ListNotificationRulesResponse;
import com.thousandeyes.sdk.agents.model.NotificationRuleDetail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.agents.model.AgentDetails;
import com.thousandeyes.sdk.agents.model.AgentDetailsExpand;
import com.thousandeyes.sdk.agents.model.AgentListExpand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.agents.model.AgentClusterAssignRequest;
import com.thousandeyes.sdk.agents.model.AgentClusterUnassignRequest;
import com.thousandeyes.sdk.agents.model.AgentDetails;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.alerts.model.Error;
import com.thousandeyes.sdk.alerts.model.Rule;
import com.thousandeyes.sdk.alerts.model.RuleDetail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.alerts.model.AlertSuppressionWindowDetail;
import com.thousandeyes.sdk.alerts.model.AlertSuppressionWindowRequest;
import com.thousandeyes.sdk.alerts.model.AlertSuppressionWindows;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.alerts.model.AlertDetail;
import com.thousandeyes.sdk.alerts.model.Alerts;
import com.thousandeyes.sdk.alerts.model.Error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.thousandeyes.sdk.client.ApiRequest;
import com.thousandeyes.sdk.utils.Config;
import org.apache.commons.lang3.tuple.Pair;

import org.apache.commons.lang3.reflect.TypeUtils;
import com.thousandeyes.sdk.bgp.monitors.model.Error;
import com.thousandeyes.sdk.bgp.monitors.model.Monitors;
import com.thousandeyes.sdk.bgp.monitors.model.UnauthorizedError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.StringJoiner;
import java.util.function.Consumer;

Expand All @@ -50,19 +50,10 @@ public static NativeApiClientBuilder builder() {
}

@Override
public <T> ApiResponse<T> send(ApiRequest request, Class<T> returnType) throws ApiException {
public <T> ApiResponse<T> send(ApiRequest request, Type returnType) throws ApiException {
return sendRequestAndProcessResponse(
getRequestBuilder(request),
mapper.readerFor(returnType));
}

@Override
public <T> ApiResponse<List<T>> sendForList(ApiRequest request, Class<T> returnType)
throws ApiException
{
return sendRequestAndProcessResponse(
getRequestBuilder(request),
mapper.readerForListOf(returnType));
mapper.readerFor(mapper.constructType(returnType)));
}

private HttpRequest.Builder getRequestBuilder(ApiRequest request) throws ApiException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.stream.Stream;

import org.apache.commons.lang3.reflect.TypeUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -114,7 +115,7 @@ void shouldSendRequestForListCorrectly(String testName, ApiRequest request)
var expectedResponse = List.of(new Response("name", OffsetDateTime.now(ZoneId.of("UTC"))));
stubHttpClient(expectedResponse);

var response = apiClient.sendForList(request, Response.class);
var response = apiClient.send(request, TypeUtils.parameterize(List.class, Response.class));

assertEquals(expectedResponse, response.getData());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@

package com.thousandeyes.sdk.client;

import java.util.List;
import java.lang.reflect.Type;



public interface ApiClient {
<T> ApiResponse<T> send(ApiRequest request, Class<T> returnType) throws ApiException;

<T> ApiResponse<List<T>> sendForList(ApiRequest request, Class<T> returnType)
throws ApiException;
<T> ApiResponse<T> send(ApiRequest request, Type returnType) throws ApiException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package com.thousandeyes.sdk.client;

import java.util.List;
import java.lang.reflect.Type;
import java.util.concurrent.Callable;

import lombok.RequiredArgsConstructor;
Expand All @@ -30,19 +30,12 @@ public abstract class ApiClientDecorator implements ApiClient {
private final ApiClient apiClient;

@Override
public final <T> ApiResponse<T> send(ApiRequest request, Class<T> returnType)
public final <T> ApiResponse<T> send(ApiRequest request, Type returnType)
throws ApiException
{
return decorate(() -> apiClient.send(request, returnType));
}

@Override
public final <T> ApiResponse<List<T>> sendForList(ApiRequest request, Class<T> returnType)
throws ApiException
{
return decorate(() -> apiClient.sendForList(request, returnType));
}

public abstract <T> ApiResponse<T> decorate(Callable<ApiResponse<T>> requestCallable)
throws ApiException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package com.thousandeyes.sdk.client;

import java.util.List;
import java.util.Collection;
import java.util.Map;

import lombok.Getter;
Expand All @@ -28,10 +28,11 @@
@Getter
public class ApiException extends Exception {
private int code = 0;
private Map<String, List<String>> responseHeaders = null;
private Map<String, ? extends Collection<String>> responseHeaders = null;
private Object responseBody = null;

public ApiException() {}
public ApiException() {
}

public ApiException(Throwable throwable) {
super(throwable);
Expand All @@ -41,22 +42,32 @@ public ApiException(String message) {
super(message);
}

public ApiException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders, Object responseBody) {
public ApiException(String message, Throwable throwable, int code,
Map<String, ? extends Collection<String>> responseHeaders,
Object responseBody)
{
super(message, throwable);
this.code = code;
this.responseHeaders = responseHeaders;
this.responseBody = responseBody;
}

public ApiException(String message, int code, Map<String, List<String>> responseHeaders, Object responseBody) {
public ApiException(String message, int code,
Map<String, ? extends Collection<String>> responseHeaders,
Object responseBody)
{
this(message, null, code, responseHeaders, responseBody);
}

public ApiException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders) {
public ApiException(String message, Throwable throwable, int code,
Map<String, ? extends Collection<String>> responseHeaders)
{
this(message, throwable, code, responseHeaders, null);
}

public ApiException(int code, Map<String, List<String>> responseHeaders, Object responseBody) {
public ApiException(int code, Map<String, ? extends Collection<String>> responseHeaders,
Object responseBody)
{
this(null, null, code, responseHeaders, responseBody);
}

Expand All @@ -65,7 +76,10 @@ public ApiException(int code, String message) {
this.code = code;
}

public ApiException(int code, String message, Map<String, List<String>> responseHeaders, Object responseBody) {
public ApiException(int code, String message,
Map<String, ? extends Collection<String>> responseHeaders,
Object responseBody)
{
this(code, message);
this.responseHeaders = responseHeaders;
this.responseBody = responseBody;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.thousandeyes.sdk.client;

import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand All @@ -38,6 +39,6 @@ public class ApiRequest {
Object requestBody;
List<Pair<String, String>> queryParams;
@Singular
Map<String, List<String>> headers;
Map<String, ? extends Collection<String>> headers;
Duration readTimeout;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@

package com.thousandeyes.sdk.client;

import java.util.List;
import java.util.Collection;
import java.util.Map;

import lombok.Getter;



@Getter
public class ApiResponse<T> {
final private int statusCode;
final private Map<String, List<String>> headers;
final private Map<String, ? extends Collection<String>> headers;
final private T data;
public ApiResponse(int statusCode, Map<String, List<String>> headers) {

public ApiResponse(int statusCode, Map<String, ? extends Collection<String>> headers) {
this(statusCode, headers, null);
}
public ApiResponse(int statusCode, Map<String, List<String>> headers, T data) {

public ApiResponse(int statusCode, Map<String, ? extends Collection<String>> headers, T data) {
this.statusCode = statusCode;
this.headers = headers;
this.data = data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
package com.thousandeyes.sdk.client;

import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -60,11 +62,11 @@ private <T> T awaitAndRetry(ApiException apiException, Callable<T> requestCallab
throw apiException;
}

private OptionalLong retryAfterInSeconds(Map<String, List<String>> headers) {
private OptionalLong retryAfterInSeconds(Map<String, ? extends Collection<String>> headers) {
return RATE_LIMIT_RESET_HEADERS.stream()
.flatMap(headerName -> headers.getOrDefault(headerName,
List.of())
.stream())
.flatMap(headerName -> Optional.ofNullable(
headers.get(headerName)).stream())
.flatMap(Collection::stream)
.mapToLong(Long::parseLong)
.map(rlResetInstant -> rlResetInstant -
Instant.now().getEpochSecond())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.mockito.Mockito.when;

import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -57,7 +58,7 @@ private static String getTimeInSeconds(Instant instant) {

@Test
void shouldNotAwaitOtherStatusCode() throws ApiException {
when(client.send(any(), eq(String.class))).thenReturn(okResponse);
when(client.<String>send(any(), eq(String.class))).thenReturn(okResponse);
var response = rlClient.send(mock(ApiRequest.class), String.class);
assertEquals(response, okResponse);
}
Expand All @@ -80,12 +81,12 @@ void shouldThrowIfApiExceptionHasNoHeaders() throws ApiException {
@ValueSource(strings = { "x-organization-rate-limit-reset", "x-instant-test-rate-limit-reset" })
void shouldRetryProperRateLimitApiException(String header) throws ApiException {
var resetTime = getTimeInSeconds(Instant.now().plusSeconds(2));
var headers = Map.of(header, List.of(resetTime));
Map<String, Collection<String>> headers = Map.of(header, List.of(resetTime));

var exception = new ApiException(TOO_MANY_REQUESTS, headers, null);

when(client.send(any(), eq(String.class))).thenThrow(exception)
.thenReturn(okResponse);
when(client.<String>send(any(), eq(String.class))).thenThrow(exception)
.thenReturn(okResponse);

var response = rlClient.send(mock(ApiRequest.class), String.class);
assertEquals(response, okResponse);
Expand Down
Loading

0 comments on commit 7762c40

Please sign in to comment.