| 1 | package org.sanmibuh.framework.notifier.telegram; | |
| 2 | ||
| 3 | import org.springframework.http.HttpStatus; | |
| 4 | import org.springframework.web.client.RestClient; | |
| 5 | import org.springframework.web.client.RestClientException; | |
| 6 | import org.springframework.web.client.RestClientResponseException; | |
| 7 | ||
| 8 | final class TelegramNotifier implements TelegramGateway { | |
| 9 | ||
| 10 | private final TelegramProperties properties; | |
| 11 | private final RestClient client; | |
| 12 | ||
| 13 | TelegramNotifier(final RestClient.Builder builder, final TelegramProperties properties) { | |
| 14 | this.properties = properties; | |
| 15 | client = builder.baseUrl(properties.baseUrl() + "/bot" + properties.botToken()).build(); | |
| 16 | } | |
| 17 | ||
| 18 | @Override | |
| 19 | public void notify(final String message) { | |
| 20 | try { | |
| 21 | client | |
| 22 | .post() | |
| 23 | .uri("/sendMessage") | |
| 24 | .body(new TelegramMessageRequest(properties.chatId(), message)) | |
| 25 | .retrieve() | |
| 26 | .toBodilessEntity(); | |
| 27 | } catch (final RestClientResponseException exception) { | |
| 28 |
1
1. notify : negated conditional → KILLED |
if (exception.getStatusCode().is5xxServerError() |
| 29 |
1
1. notify : negated conditional → KILLED |
|| exception.getStatusCode() == HttpStatus.TOO_MANY_REQUESTS) { |
| 30 | throw new TelegramTemporarilyUnavailableException(); | |
| 31 | } | |
| 32 | throw new TelegramNotificationFailedException(); | |
| 33 | } catch (final RestClientException _) { | |
| 34 | throw new TelegramTemporarilyUnavailableException(); | |
| 35 | } | |
| 36 | } | |
| 37 | } | |
Mutations | ||
| 28 |
1.1 |
|
| 29 |
1.1 |