All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cleverpine.cptranslationsutil.service.implementation.TranslationServiceClientImpl Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package com.cleverpine.cptranslationsutil.service.implementation;

import com.cleverpine.cptranslationsutil.dto.ExportResponseDTO;
import com.cleverpine.cptranslationsutil.exception.TranslationServiceClientException;
import com.cleverpine.cptranslationsutil.service.contract.TranslationServiceClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;


import static com.cleverpine.cptranslationsutil.util.ErrorMessages.NUMBER_OF_RETRY_FOR_EXPORT_TRANSLATIONS_ERROR_WARNING;
import static com.cleverpine.cptranslationsutil.util.ErrorMessages.RETRIES_EXHAUSTED_FOR_EXPORT_OF_TRANSLATIONS_ERROR_MESSAGE;
import static com.cleverpine.cptranslationsutil.util.ErrorMessages.TRANSLATION_SERVICE_CLIENT_ERROR_MESSAGE;

@Slf4j
@RequiredArgsConstructor
public class TranslationServiceClientImpl implements TranslationServiceClient {

    private static final String EXPORT_BY_NAME_URI = "/projects/{projectName}/platforms/{platformName}/nameExport";
    private static final String MULTI_EXPORT_URI = "/projects/{projectName}/platforms/{platformName}/multiExport";
    public static final Integer NUMBER_OF_RETRIES = 3;

    private final WebClient webClient;

    @Override
    public Mono exportAllTranslationsByProjectNameAndPlatformNameOrRetryAndThrow(
            String projectName, String platformName
    ) {
        return webClient
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path(EXPORT_BY_NAME_URI)
                        .build(projectName, platformName))
                .retrieve()
                .bodyToMono(ExportResponseDTO.class)
                .retryWhen(Retry.fixedDelay(NUMBER_OF_RETRIES, Duration.ofSeconds(3))
                        .doAfterRetry(retrySignal ->
                                log.warn(String.format(NUMBER_OF_RETRY_FOR_EXPORT_TRANSLATIONS_ERROR_WARNING,
                                        retrySignal.totalRetries(), retrySignal.failure().getMessage())))
                        .onRetryExhaustedThrow((retryBackoffSpec, retrySignal) ->
                                new TranslationServiceClientException(
                                        RETRIES_EXHAUSTED_FOR_EXPORT_OF_TRANSLATIONS_ERROR_MESSAGE
                                )));

    }

    @Override
    public Mono exportAllTranslationsByProjectNameAndPlatformNameOrLogError(
            String projectName, String platformName
    ) {
        return webClient
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path(EXPORT_BY_NAME_URI)
                        .build(projectName, platformName))
                .retrieve()
                .onStatus(HttpStatus::isError,
                        error -> {
                            log.error(String.format(TRANSLATION_SERVICE_CLIENT_ERROR_MESSAGE, error.statusCode()));
                            return Mono.empty();
                        })
                .bodyToMono(ExportResponseDTO.class);
    }

    @Override
    public Mono exportAllTranslationsAndDefaultsByProjectNameAndPlatformNameOrRetryAndThrow(
            String projectName, String platformName
    ) {
        return webClient
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path(MULTI_EXPORT_URI)
                        .build(projectName, platformName))
                .retrieve()
                .bodyToMono(ExportResponseDTO.class)
                .retryWhen(Retry.fixedDelay(NUMBER_OF_RETRIES, Duration.ofSeconds(3))
                        .doAfterRetry(retrySignal ->
                                log.warn(String.format(NUMBER_OF_RETRY_FOR_EXPORT_TRANSLATIONS_ERROR_WARNING,
                                        retrySignal.totalRetries(), retrySignal.failure().getMessage())))
                        .onRetryExhaustedThrow((retryBackoffSpec, retrySignal) ->
                                new TranslationServiceClientException(
                                        RETRIES_EXHAUSTED_FOR_EXPORT_OF_TRANSLATIONS_ERROR_MESSAGE
                                )));

    }

    @Override
    public Mono exportAllTranslationsAndDefaultsByProjectNameAndPlatformNameOrLogError(
            String projectName, String platformName
    ) {
        return webClient
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path(MULTI_EXPORT_URI)
                        .build(projectName, platformName))
                .retrieve()
                .onStatus(HttpStatus::isError,
                        error -> {
                            log.error(String.format(TRANSLATION_SERVICE_CLIENT_ERROR_MESSAGE, error.statusCode()));
                            return Mono.empty();
                        })
                .bodyToMono(ExportResponseDTO.class);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy