Skip to content

Commit

Permalink
update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Apr 1, 2024
1 parent fc5aff3 commit f32f16a
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.Duration;
import java.util.Locale;
import java.time.ZoneOffset;
import java.time.OffsetDateTime;

import org.openapitools.client.auth.Authentication;
Expand Down Expand Up @@ -103,6 +113,8 @@ public ApiClient(RestTemplate restTemplate) {
init();
}

private static final Logger logger = LoggerFactory.getLogger(ApiClient.class);

protected void init() {
// Use RFC3339 format for date and datetime.
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
Expand Down Expand Up @@ -686,28 +698,55 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin
ResponseEntity<T> responseEntity = null;
int attempts = 0;
while (attempts < maxAttemptsForRetry) {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex.getStatusCode().equals(HttpStatus.TOO_MANY_REQUESTS)) {
HttpHeaders responseHeaders = ex.getResponseHeaders();
String retryAfterHeader = responseHeaders.getFirst("Retry-After");
if (retryAfterHeader == null) {
logger.error("Retry-After header not found in response. Max attempts reached.");
throw ex;
}
long retryAfterSeconds = 0;
try {
ZonedDateTime currentTime = ZonedDateTime.now(ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
ZonedDateTime retryAfterDate = ZonedDateTime.parse(retryAfterHeader, formatter);
Duration duration = Duration.between(currentTime, retryAfterDate);
retryAfterSeconds = duration.getSeconds();

} catch (DateTimeParseException e) {
retryAfterSeconds = Long.parseLong(retryAfterHeader);
}
attempts++;
if (attempts < maxAttemptsForRetry) {
logger.info("Attempting to retry after " + retryAfterSeconds + " seconds");
try {
Thread.sleep(retryAfterSeconds * 1000);
} catch (InterruptedException ix) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else if (ex instanceof HttpServerErrorException) {
attempts++;
if (attempts < maxAttemptsForRetry) {
logger.info("Retry Attempt : " + attempts);
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException ix) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
}

if (responseEntity == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.Duration;
import java.util.Locale;
import java.time.ZoneOffset;
import java.time.OffsetDateTime;

import org.openapitools.client.auth.Authentication;
Expand Down Expand Up @@ -101,6 +111,8 @@ public ApiClient(RestTemplate restTemplate) {
init();
}

private static final Logger logger = LoggerFactory.getLogger(ApiClient.class);

protected void init() {
// Use RFC3339 format for date and datetime.
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
Expand Down Expand Up @@ -629,28 +641,55 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin
ResponseEntity<T> responseEntity = null;
int attempts = 0;
while (attempts < maxAttemptsForRetry) {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex.getStatusCode().equals(HttpStatus.TOO_MANY_REQUESTS)) {
HttpHeaders responseHeaders = ex.getResponseHeaders();
String retryAfterHeader = responseHeaders.getFirst("Retry-After");
if (retryAfterHeader == null) {
logger.error("Retry-After header not found in response. Max attempts reached.");
throw ex;
}
long retryAfterSeconds = 0;
try {
ZonedDateTime currentTime = ZonedDateTime.now(ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
ZonedDateTime retryAfterDate = ZonedDateTime.parse(retryAfterHeader, formatter);
Duration duration = Duration.between(currentTime, retryAfterDate);
retryAfterSeconds = duration.getSeconds();

} catch (DateTimeParseException e) {
retryAfterSeconds = Long.parseLong(retryAfterHeader);
}
attempts++;
if (attempts < maxAttemptsForRetry) {
logger.info("Attempting to retry after " + retryAfterSeconds + " seconds");
try {
Thread.sleep(retryAfterSeconds * 1000);
} catch (InterruptedException ix) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else if (ex instanceof HttpServerErrorException) {
attempts++;
if (attempts < maxAttemptsForRetry) {
logger.info("Retry Attempt : " + attempts);
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException ix) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
}

if (responseEntity == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.Duration;
import java.util.Locale;
import java.time.ZoneOffset;
import java.time.OffsetDateTime;

import org.openapitools.client.auth.Authentication;
Expand Down Expand Up @@ -103,6 +113,8 @@ public ApiClient(RestTemplate restTemplate) {
init();
}

private static final Logger logger = LoggerFactory.getLogger(ApiClient.class);

protected void init() {
// Use RFC3339 format for date and datetime.
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
Expand Down Expand Up @@ -678,28 +690,55 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin
ResponseEntity<T> responseEntity = null;
int attempts = 0;
while (attempts < maxAttemptsForRetry) {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex instanceof HttpServerErrorException
|| ((HttpClientErrorException) ex)
.getStatusCode()
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
attempts++;
if (attempts < maxAttemptsForRetry) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
break;
} catch (HttpServerErrorException | HttpClientErrorException ex) {
if (ex.getStatusCode().equals(HttpStatus.TOO_MANY_REQUESTS)) {
HttpHeaders responseHeaders = ex.getResponseHeaders();
String retryAfterHeader = responseHeaders.getFirst("Retry-After");
if (retryAfterHeader == null) {
logger.error("Retry-After header not found in response. Max attempts reached.");
throw ex;
}
long retryAfterSeconds = 0;
try {
ZonedDateTime currentTime = ZonedDateTime.now(ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
ZonedDateTime retryAfterDate = ZonedDateTime.parse(retryAfterHeader, formatter);
Duration duration = Duration.between(currentTime, retryAfterDate);
retryAfterSeconds = duration.getSeconds();

} catch (DateTimeParseException e) {
retryAfterSeconds = Long.parseLong(retryAfterHeader);
}
attempts++;
if (attempts < maxAttemptsForRetry) {
logger.info("Attempting to retry after " + retryAfterSeconds + " seconds");
try {
Thread.sleep(retryAfterSeconds * 1000);
} catch (InterruptedException ix) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else if (ex instanceof HttpServerErrorException) {
attempts++;
if (attempts < maxAttemptsForRetry) {
logger.info("Retry Attempt : " + attempts);
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException ix) {
Thread.currentThread().interrupt();
}
} else {
throw ex;
}
} else {
throw ex;
}
}
}

if (responseEntity == null) {
Expand Down
Loading

0 comments on commit f32f16a

Please sign in to comment.