2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.salus.internal.rest;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * @author Martin GrzeĊlowski - Initial contribution
24 public class RetryHttpClient implements RestClient {
25 private final Logger logger = LoggerFactory.getLogger(RetryHttpClient.class);
26 private final RestClient restClient;
27 private final int maxRetries;
29 public RetryHttpClient(RestClient restClient, int maxRetries) {
30 this.restClient = restClient;
31 if (maxRetries <= 0) {
32 throw new IllegalArgumentException("maxRetries cannot be lower or equal to 0, but was " + maxRetries);
34 this.maxRetries = maxRetries;
38 public @Nullable String get(String url, @Nullable Header... headers) throws SalusApiException {
39 for (int i = 0; i < maxRetries; i++) {
41 return restClient.get(url, headers);
42 } catch (SalusApiException ex) {
43 if (i < maxRetries - 1) {
44 logger.debug("Error while calling GET {}. Retrying {}/{}...", i + 1, maxRetries, url, ex);
50 throw new IllegalStateException("Should not happen!");
54 public @Nullable String post(String url, Content content, @Nullable Header... headers) throws SalusApiException {
55 for (int i = 0; i < maxRetries; i++) {
57 return restClient.post(url, content, headers);
58 } catch (SalusApiException ex) {
59 if (i < maxRetries - 1) {
60 logger.debug("Error while calling POST {}. Retrying {}/{}...", i + 1, maxRetries, url, ex);
66 throw new IllegalStateException("Should not happen!");