generated from pagopa/pagopa-functions-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PAGOPA-1745 adding dynamic mechanism to retrieve station and broker
- Loading branch information
1 parent
5b4d32c
commit f4db1ad
Showing
11 changed files
with
338 additions
and
18 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
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
134 changes: 134 additions & 0 deletions
134
src/main/java/it/gov/pagopa/reporting/client/ApiConfigClient.java
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,134 @@ | ||
package it.gov.pagopa.reporting.client; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.api.client.http.*; | ||
import com.google.api.client.http.javanet.NetHttpTransport; | ||
import com.google.api.client.json.JsonFactory; | ||
import com.google.api.client.json.JsonObjectParser; | ||
import com.google.api.client.json.gson.GsonFactory; | ||
import com.google.api.client.util.ExponentialBackOff; | ||
import com.google.gson.reflect.TypeToken; | ||
import it.gov.pagopa.reporting.exception.Cache4XXException; | ||
import it.gov.pagopa.reporting.exception.Cache5XXException; | ||
import it.gov.pagopa.reporting.models.cache.CacheResponse; | ||
import it.gov.pagopa.reporting.models.cache.CreditorInstitutionStation; | ||
import it.gov.pagopa.reporting.models.cache.Station; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ApiConfigClient { | ||
|
||
private static ApiConfigClient instance = null; | ||
|
||
private final HttpTransport httpTransport = new NetHttpTransport(); | ||
private final JsonFactory jsonFactory = new GsonFactory(); | ||
private final String apiConfigCacheHost = System.getenv("CACHE_CLIENT_HOST"); // es: https://api.xxx.platform.pagopa.it | ||
private final String getCacheDetails = | ||
System.getenv("CACHE_PATH") != null ? System.getenv("CACHE_PATH") : "/cache?keys=creditorInstitutionStations,stations"; | ||
private final String apiKey = System.getenv("CACHE_API_KEY"); | ||
|
||
|
||
// Retry ExponentialBackOff config | ||
private final boolean enableRetry = | ||
System.getenv("ENABLE_CLIENT_RETRY") != null ? Boolean.parseBoolean(System.getenv("ENABLE_CLIENT_RETRY")) : Boolean.FALSE; | ||
private final int initialIntervalMillis = | ||
System.getenv("INITIAL_INTERVAL_MILLIS") != null ? Integer.parseInt(System.getenv("INITIAL_INTERVAL_MILLIS")) : 500; | ||
private final int maxElapsedTimeMillis = | ||
System.getenv("MAX_ELAPSED_TIME_MILLIS") != null ? Integer.parseInt(System.getenv("MAX_ELAPSED_TIME_MILLIS")) : 1000; | ||
private final int maxIntervalMillis = | ||
System.getenv("MAX_INTERVAL_MILLIS") != null ? Integer.parseInt(System.getenv("MAX_INTERVAL_MILLIS")) : 1000; | ||
private final double multiplier = | ||
System.getenv("MULTIPLIER") != null ? Double.parseDouble(System.getenv("MULTIPLIER")) : 1.5; | ||
private final double randomizationFactor = | ||
System.getenv("RANDOMIZATION_FACTOR") != null ? Double.parseDouble(System.getenv("RANDOMIZATION_FACTOR")) : 0.5; | ||
|
||
public static ApiConfigClient getInstance() { | ||
if (instance == null) { | ||
instance = new ApiConfigClient(); | ||
} | ||
return instance; | ||
} | ||
|
||
public CacheResponse getCache() throws IOException, IllegalArgumentException, Cache5XXException, Cache4XXException { | ||
GenericUrl url = new GenericUrl(apiConfigCacheHost + getCacheDetails); | ||
HttpRequest request = this.buildGetRequestToApiConfigCache(url); | ||
|
||
if (enableRetry) { | ||
this.setRequestRetry(request); | ||
} | ||
|
||
return this.executeCallToApiConfigCache(request); | ||
} | ||
|
||
public HttpRequest buildGetRequestToApiConfigCache(GenericUrl url) throws IOException { | ||
|
||
HttpRequestFactory requestFactory = httpTransport.createRequestFactory( | ||
(HttpRequest request) -> | ||
request.setParser(new JsonObjectParser(jsonFactory)) | ||
); | ||
|
||
HttpRequest request = requestFactory.buildGetRequest(url); | ||
HttpHeaders headers = request.getHeaders(); | ||
headers.set("Ocp-Apim-Subscription-Key", apiKey); | ||
return request; | ||
} | ||
|
||
public void setRequestRetry(HttpRequest request) { | ||
/** | ||
* Retry section config | ||
*/ | ||
ExponentialBackOff backoff = new ExponentialBackOff.Builder() | ||
.setInitialIntervalMillis(initialIntervalMillis) | ||
.setMaxElapsedTimeMillis(maxElapsedTimeMillis) | ||
.setMaxIntervalMillis(maxIntervalMillis) | ||
.setMultiplier(multiplier) | ||
.setRandomizationFactor(randomizationFactor) | ||
.build(); | ||
|
||
// Exponential Backoff is turned off by default in HttpRequest -> it's necessary include an instance of HttpUnsuccessfulResponseHandler to the HttpRequest to activate it | ||
// The default back-off on anabnormal HTTP response is BackOffRequired.ON_SERVER_ERROR (5xx) | ||
request.setUnsuccessfulResponseHandler( | ||
new HttpBackOffUnsuccessfulResponseHandler(backoff)); | ||
} | ||
|
||
public CacheResponse executeCallToApiConfigCache(HttpRequest request) throws IOException, IllegalArgumentException, Cache5XXException, Cache4XXException { | ||
|
||
Type type = new TypeToken<List<CreditorInstitutionStation>>() {}.getType(); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
CacheResponse cacheResponse = CacheResponse.builder().build(); | ||
List<CreditorInstitutionStation> creditorInstitutionStationList = new ArrayList<>(); | ||
List<Station> stationList = new ArrayList<>(); | ||
try { | ||
InputStream resIs = request.execute().getContent(); | ||
Map<String,Object> responseMap = mapper.readValue(resIs, HashMap.class); | ||
Map<String,Object> creditorInstitutionStations = (HashMap) responseMap.get("creditorInstitutionStations"); | ||
for (Map.Entry<String, Object> creditorInstitutionStation : creditorInstitutionStations.entrySet()) { | ||
creditorInstitutionStationList.add(mapper.readValue(mapper.writeValueAsString(creditorInstitutionStation.getValue()), CreditorInstitutionStation.class)); | ||
} | ||
Map<String,Object> stations = (HashMap) responseMap.get("stations"); | ||
for (Map.Entry<String, Object> station : stations.entrySet()) { | ||
stationList.add(mapper.readValue(mapper.writeValueAsString(station.getValue()), Station.class)); | ||
} | ||
cacheResponse.setStations(stationList); | ||
cacheResponse.setCreditorInstitutionStations(creditorInstitutionStationList); | ||
} catch (HttpResponseException e) { | ||
if (e.getStatusCode() / 100 == 4) { | ||
String message = String.format("Error %s calling the service URL %s", e.getStatusCode(), request.getUrl()); | ||
throw new Cache4XXException(message); | ||
|
||
} else if (e.getStatusCode() / 100 == 5) { | ||
String message = String.format("Error %s calling the service URL %s", e.getStatusCode(), request.getUrl()); | ||
throw new Cache5XXException(message); | ||
|
||
} | ||
} | ||
return cacheResponse; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/it/gov/pagopa/reporting/exception/AppException.java
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,18 @@ | ||
package it.gov.pagopa.reporting.exception; | ||
|
||
public class AppException extends Exception { | ||
|
||
/** | ||
* generated serialVersionUID | ||
*/ | ||
private static final long serialVersionUID = -7564079264281462536L; | ||
|
||
public AppException() { | ||
super(); | ||
} | ||
|
||
public AppException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/it/gov/pagopa/reporting/exception/Cache4XXException.java
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,18 @@ | ||
package it.gov.pagopa.reporting.exception; | ||
|
||
public class Cache4XXException extends Exception { | ||
|
||
/** | ||
* generated serialVersionUID | ||
*/ | ||
private static final long serialVersionUID = -7564079264281462536L; | ||
|
||
public Cache4XXException() { | ||
super(); | ||
} | ||
|
||
public Cache4XXException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/it/gov/pagopa/reporting/exception/Cache5XXException.java
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,18 @@ | ||
package it.gov.pagopa.reporting.exception; | ||
|
||
public class Cache5XXException extends Exception { | ||
|
||
/** | ||
* generated serialVersionUID | ||
*/ | ||
private static final long serialVersionUID = -7564079264281462536L; | ||
|
||
public Cache5XXException() { | ||
super(); | ||
} | ||
|
||
public Cache5XXException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/it/gov/pagopa/reporting/models/cache/CacheResponse.java
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,27 @@ | ||
package it.gov.pagopa.reporting.models.cache; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.*; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Setter | ||
@ToString | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@Builder | ||
public class CacheResponse { | ||
|
||
@JsonProperty(value = "stations") | ||
private List<Station> stations; | ||
|
||
@JsonProperty(value = "creditorInstitutionStations") | ||
private List<CreditorInstitutionStation> creditorInstitutionStations; | ||
|
||
private LocalDateTime retrieveDate; | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/it/gov/pagopa/reporting/models/cache/CreditorInstitutionStation.java
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,21 @@ | ||
package it.gov.pagopa.reporting.models.cache; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.*; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Setter | ||
@ToString | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class CreditorInstitutionStation { | ||
|
||
@JsonProperty(value = "creditor_institution_code") | ||
private String creditorInstitutionCode; | ||
|
||
@JsonProperty(value = "station_code") | ||
private String stationCode; | ||
} |
Oops, something went wrong.