package com.stevesoltys.telegramirc.protocol.telegram.message.decoder.image.impl.imgur;

import com.stevesoltys.telegramirc.configuration.telegram.ImgurDecoderConfiguration;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.Optional;

/**
 * @author Steve Soltys
 */
@Component
public class ImgurUploader {

    private static final String UPLOAD_ENDPOINT = "https://api.imgur.com/3/image?image={image}";

    private static final String IMAGE_URI_VARIABLE = "image";

    private final ImgurDecoderConfiguration configuration;

    private final RestTemplate restTemplate;

    public ImgurUploader(ImgurDecoderConfiguration configuration) {
        this.configuration = configuration;

        restTemplate = new RestTemplate();
    }

    public Optional<String> upload(String imageUrl) {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        headers.set("Authorization", "Client-ID " + configuration.getApiKey());

        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<ImgurResponse> response = restTemplate.exchange(UPLOAD_ENDPOINT, HttpMethod.POST, entity,
                ImgurResponse.class, Collections.singletonMap(IMAGE_URI_VARIABLE, imageUrl));

        ImgurResponse imgurResponse = response.getBody();
        ImgurResponseData responseData = imgurResponse.getResponseData();

        if(responseData == null) {
            return Optional.empty();
        }

        return Optional.ofNullable(responseData.getLink());
    }
}

@@ -167,16 +167,73 @@
 ration;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -308,16 +308,80 @@
 ponent;%0A
+import org.springframework.web.client.HttpClientErrorException;%0A
 import o
@@ -713,24 +713,101 @@
 = %22image%22;%0A%0A
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());%0A%0A
     private 
@@ -1429,16 +1429,53 @@
 esponse%3E
+ response;%0A%0A        try %7B%0A           
  respons
@@ -1545,24 +1545,8 @@
 ity,
-%0A               
  Img
@@ -1562,16 +1562,36 @@
 e.class,
+%0A                   
  Collect
@@ -1637,24 +1637,191 @@
 mageUrl));%0A%0A
+        %7D catch (HttpClientErrorException ex) %7B%0A            logger.error(%22Error while uploading image to Imgur.%22, ex);%0A            return Optional.empty();%0A        %7D%0A%0A
         Imgu
@@ -1951,16 +1951,17 @@
       if
+ 
 (respons

Add exception handling to ImgurUploader