nz.co.blink.debit.client.v1.MetaApiClient Maven / Gradle / Ivy
Show all versions of blink-debit-api-client-java-spring6 Show documentation
/**
* Copyright (c) 2022 BlinkPay
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package nz.co.blink.debit.client.v1;
import nz.co.blink.debit.config.BlinkPayProperties;
import nz.co.blink.debit.dto.v1.BankMetadata;
import nz.co.blink.debit.enums.BlinkDebitConstant;
import nz.co.blink.debit.exception.BlinkServiceException;
import nz.co.blink.debit.helpers.AccessTokenHandler;
import nz.co.blink.debit.helpers.RequestHandler;
import nz.co.blink.debit.helpers.ResponseHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import java.util.UUID;
import static nz.co.blink.debit.enums.BlinkDebitConstant.METADATA_PATH;
import static nz.co.blink.debit.enums.BlinkDebitConstant.REQUEST_ID;
/**
* The client for bank metadata.
*/
@Component
public class MetaApiClient {
private final ReactorClientHttpConnector connector;
private final String debitUrl;
private final AccessTokenHandler accessTokenHandler;
private WebClient.Builder webClientBuilder;
/**
* Default constructor.
*
* @param connector the {@link ReactorClientHttpConnector}
* @param properties the {@link BlinkPayProperties}
* @param accessTokenHandler the {@link AccessTokenHandler}
*/
@Autowired
public MetaApiClient(@Qualifier("blinkDebitClientHttpConnector") ReactorClientHttpConnector connector,
BlinkPayProperties properties, AccessTokenHandler accessTokenHandler) {
this.connector = connector;
debitUrl = properties.getDebit().getUrl();
this.accessTokenHandler = accessTokenHandler;
}
/**
* Returns the {@link BankMetadata} {@link Flux}.
*
* @return the {@link BankMetadata} {@link Flux}
* @throws BlinkServiceException thrown when an exception occurs
*/
public Flux getMeta() throws BlinkServiceException {
return getMeta(null);
}
/**
* Returns the {@link BankMetadata} {@link Flux}.
*
* @param requestId the optional correlation ID
* @return the {@link BankMetadata} {@link Flux}
* @throws BlinkServiceException thrown when an exception occurs
*/
public Flux getMeta(final String requestId) throws BlinkServiceException {
String requestIdFinal = StringUtils.defaultIfBlank(requestId, UUID.randomUUID().toString());
return getWebClientBuilder(requestIdFinal)
.filter((clientRequest, exchangeFunction) -> RequestHandler.logRequest(null, clientRequest,
exchangeFunction))
.build()
.get()
.uri(METADATA_PATH.getValue())
.accept(MediaType.APPLICATION_JSON)
.headers(httpHeaders -> httpHeaders.add(REQUEST_ID.getValue(), requestIdFinal))
.exchangeToFlux(ResponseHandler.handleResponseFlux(BankMetadata.class));
}
private WebClient.Builder getWebClientBuilder(String requestId) throws BlinkServiceException {
if (webClientBuilder != null) {
return webClientBuilder;
}
return WebClient.builder()
.clientConnector(connector)
.defaultHeader(HttpHeaders.USER_AGENT, BlinkDebitConstant.USER_AGENT_VALUE.getValue())
.baseUrl(debitUrl)
.filter(accessTokenHandler.setAccessToken(requestId));
}
}