Skip to content

Commit

Permalink
minor update, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Dec 10, 2023
1 parent 2a05b93 commit 598a8fc
Show file tree
Hide file tree
Showing 9 changed files with 564 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {

private DateFormat dateFormat;

private int maxAttempts = 3;

private long waitTimeMillis = 10l;

public ApiClient() {
this.restTemplate = buildRestTemplate();
init();
Expand Down Expand Up @@ -168,6 +172,46 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
return this;
}

/**
* Get the max attempts for retry
*
* @return int the max attempts
*/
public int getMaxAttempts() {
return maxAttempts;
}

/**
* Set the max attemtps for retry
*
* @param maxAttempts the max attempts for retry
* @return ApiClient this client
*/
public ApiClient setMaxAttempts(int maxAttempts) {
this.maxAttempts = maxAttempts;
return this;
}

/**
* Get the wait time in milliseconds
*
* @return long wait time in milliseconds
*/
public long getWaitTimeMillis() {
return waitTimeMillis;
}

/**
* Set the wait time in milliseconds
*
* @param waitTimeMillis the wait time in milliseconds
* @return ApiClient this client
*/
public ApiClient setWaitTimeMillis(long waitTimeMillis) {
this.waitTimeMillis = waitTimeMillis;
return this;
}

/**
* Get authentications (key: authentication name, value: authentication).
*
Expand Down Expand Up @@ -724,25 +768,22 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
ResponseEntity<T> responseEntity = null;
int attempts = 0;
int maxAttempts = 3;
long waitTimeMillis = 10l;
while (attempts < maxAttempts) {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
// request succeeded, no need to retry
break;
}
catch (HttpServerErrorException ex) {
} catch (HttpServerErrorException ex) {
if (ex.getStatusCode() != HttpStatus.SERVICE_UNAVAILABLE) {
throw ex;
}
attempts++;
if (attempts < maxAttempts) {
try {
Thread.sleep(waitTimeMillis);
}
catch (InterruptedException e) {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
Expand Down Expand Up @@ -87,6 +88,10 @@ private String collectionToString(Collection<?> collection) {

private DateFormat dateFormat;

private int maxAttempts = 3;

private long waitTimeMillis = 10l;

public ApiClient() {
this.restTemplate = buildRestTemplate();
init();
Expand Down Expand Up @@ -136,6 +141,46 @@ public ApiClient setBasePath(String basePath) {
return this;
}

/**
* Get the max attempts for retry
*
* @return int the max attempts
*/
public int getMaxAttempts() {
return maxAttempts;
}

/**
* Set the max attemtps for retry
*
* @param maxAttempts the max attempts for retry
* @return ApiClient this client
*/
public ApiClient setMaxAttempts(int maxAttempts) {
this.maxAttempts = maxAttempts;
return this;
}

/**
* Get the wait time in milliseconds
*
* @return long wait time in milliseconds
*/
public long getWaitTimeMillis() {
return waitTimeMillis;
}

/**
* Set the wait time in milliseconds
*
* @param waitTimeMillis the wait time in milliseconds
* @return ApiClient this client
*/
public ApiClient setWaitTimeMillis(long waitTimeMillis) {
this.waitTimeMillis = waitTimeMillis;
return this;
}

/**
* Get authentications (key: authentication name, value: authentication).
*
Expand Down Expand Up @@ -637,7 +682,27 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin

RequestEntity<Object> requestEntity = requestBuilder.body(selectBody(body, formParams, contentType));

ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);
ResponseEntity<T> responseEntity = null;
int attempts = 0;
while (attempts < maxAttempts) {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
// request succeeded, no need to retry
break;
} catch (HttpServerErrorException ex) {
if (ex.getStatusCode() != HttpStatus.SERVICE_UNAVAILABLE) {
throw ex;
}
attempts++;
if (attempts < maxAttempts) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}

if (responseEntity.getStatusCode().is2xxSuccessful()) {
return responseEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Echo Server API
* Echo Server API
*
* The version of the OpenAPI document: 0.1.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/


package org.openapitools.client;

import org.junit.Assert;
import org.openapitools.client.api.*;
import org.openapitools.client.model.*;
import org.junit.Test;
import org.junit.Ignore;

import java.io.IOException;
import java.util.*;


/**
* API tests
*/
public class CustomTest {

private final QueryApi api = new QueryApi();
private final BodyApi bodyApi = new BodyApi();

/**
* Test body parameter(s)
* <p>
* Test body parameter(s)
*
*/
@Test
public void testEchoBodyPet() {
Pet pet = new Pet().id(12345L).name("Hello World").
photoUrls(Arrays.asList(new String[]{"http://a.com", "http://b.com"})).category(new Category().id(987L).name("new category"));

Pet p = bodyApi.testEchoBodyPet(pet);
Assert.assertNotNull(p);
Assert.assertEquals("Hello World", p.getName());
Assert.assertEquals(Long.valueOf(12345L), p.getId());

// response is empty body
Pet p2 = bodyApi.testEchoBodyPet(null);
Assert.assertNull(p2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
Expand Down Expand Up @@ -85,6 +86,10 @@ private String collectionToString(Collection<?> collection) {

private DateFormat dateFormat;

private maxAttempts = 3;

long waitTimeMillis = 10l;

public ApiClient() {
this.restTemplate = buildRestTemplate();
init();
Expand Down Expand Up @@ -132,6 +137,46 @@ public ApiClient setBasePath(String basePath) {
return this;
}

/**
* Get the max attempts for retry
*
* @return int the max attempts
*/
public int getMaxAttempts() {
return maxAttempts;
}

/**
* Set the max attemtps for retry
*
* @param maxAttempts the max attempts for retry
* @return ApiClient this client
*/
public ApiClient setMaxAttempts(int maxAttempts) {
this.maxAttempts = maxAttempts;
return this;
}

/**
* Get the wait time in milliseconds
*
* @return long wait time in milliseconds
*/
public long getWaitTimeMillis() {
return waitTimeMillis;
}

/**
* Set the wait time in milliseconds
*
* @param waitTimeMillis the wait time in milliseconds
* @return ApiClient this client
*/
public ApiClient setWaitTimeMillis(long waitTimeMillis) {
this.waitTimeMillis = waitTimeMillis;
return this;
}

/**
* Get authentications (key: authentication name, value: authentication).
*
Expand Down Expand Up @@ -580,7 +625,27 @@ public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<Strin

RequestEntity<Object> requestEntity = requestBuilder.body(selectBody(body, formParams, contentType));

ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);
ResponseEntity<T> responseEntity = null;
int attempts = 0;
while (attempts < maxAttempts) {
try {
responseEntity = restTemplate.exchange(requestEntity, returnType);
// request succeeded, no need to retry
break;
} catch (HttpServerErrorException ex) {
if (ex.getStatusCode() != HttpStatus.SERVICE_UNAVAILABLE) {
throw ex;
}
attempts++;
if (attempts < maxAttempts) {
try {
Thread.sleep(waitTimeMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}

if (responseEntity.getStatusCode().is2xxSuccessful()) {
return responseEntity;
Expand Down
Loading

0 comments on commit 598a8fc

Please sign in to comment.