This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#13] POST/PUT/OPTIONS are also retried, removing a lot of duplicated…
… code
- Loading branch information
1 parent
84355ae
commit 04af0e3
Showing
12 changed files
with
127 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
...fg/infrastructure/web/resttemplate/fluent/common/response/executor/HttpEntityUtils.groovy
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...m/ofg/infrastructure/web/resttemplate/fluent/common/response/executor/RestExecutor.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.ofg.infrastructure.web.resttemplate.fluent.common.response.executor | ||
|
||
import com.google.common.util.concurrent.ListenableFuture | ||
import com.nurkiewicz.asyncretry.RetryExecutor | ||
import groovy.transform.CompileStatic | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.client.RestOperations | ||
|
||
import static com.ofg.infrastructure.web.resttemplate.fluent.common.response.executor.UrlParsingUtils.appendPathToHost | ||
|
||
/** | ||
* Utility class that extracts {@link HttpEntity} from the provided map of passed parameters | ||
*/ | ||
@CompileStatic | ||
final class RestExecutor<T> { | ||
private final RestOperations restOperations | ||
private final RetryExecutor retryExecutor | ||
private final Class<T> responseType | ||
|
||
RestExecutor(RestOperations restOperations, RetryExecutor retryExecutor, Class<T> responseType) { | ||
this.restOperations = restOperations | ||
this.retryExecutor = retryExecutor | ||
this.responseType = responseType | ||
} | ||
|
||
ResponseEntity<T> exchange(HttpMethod httpMethod, Map params) { | ||
return exchangeAsync(httpMethod, params).get() | ||
} | ||
|
||
ListenableFuture<ResponseEntity<T>> exchangeAsync(HttpMethod httpMethod, Map params) { | ||
if (params.url) { | ||
return callUrlWithRetry(httpMethod, params) | ||
} else if (params.urlTemplate) { | ||
return callUrlTemplateWithRetry(httpMethod, params) | ||
} | ||
throw new InvalidHttpMethodParametersException(params) | ||
} | ||
|
||
protected ListenableFuture<ResponseEntity<T>> callUrlTemplateWithRetry(HttpMethod httpMethod, Map params) { | ||
return retryExecutor.getWithRetry { | ||
return restOperations.exchange( | ||
appendPathToHost(params.host as String, params.urlTemplate as String), | ||
httpMethod, | ||
getHttpEntityFrom(params), | ||
responseType, | ||
params.urlVariablesArray as Object[] ?: params.urlVariablesMap as Map<String, ?>) | ||
} | ||
} | ||
|
||
|
||
protected ListenableFuture<ResponseEntity<T>> callUrlWithRetry(HttpMethod httpMethod, Map params) { | ||
return retryExecutor.getWithRetry { | ||
restOperations.exchange( | ||
new URI(appendPathToHost(params.host as String, params.url as URI)), | ||
httpMethod, | ||
getHttpEntityFrom(params), | ||
responseType) | ||
} | ||
} | ||
|
||
private HttpEntity<Object> getHttpEntityFrom(Map params) { | ||
if (params.httpEntity) { | ||
return params.httpEntity as HttpEntity | ||
} | ||
HttpHeaders headers = params.headers as HttpHeaders | ||
HttpEntity<?> httpEntity = new HttpEntity<Object>(params.request, headers) | ||
return httpEntity | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters