<commit_before>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 <NAME>
 */
@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());
    }
}
<commit_msg>Add exception handling to ImgurUploader<commit_after>package com.stevesoltys.telegramirc.protocol.telegram.message.decoder.image.impl.imgur;

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

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

/**
 * @author <NAME>
 */
@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 Logger logger = LoggerFactory.getLogger(this.getClass());

    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;

        try {
            response = restTemplate.exchange(UPLOAD_ENDPOINT, HttpMethod.POST, entity, ImgurResponse.class,
                    Collections.singletonMap(IMAGE_URI_VARIABLE, imageUrl));

        } catch (HttpClientErrorException ex) {
            logger.error("Error while uploading image to Imgur.", ex);
            return Optional.empty();
        }

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

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

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

{'fraction_non_alphanumeric': 0.09634809634809635, 'fraction_numerical': 0.001036001036001036, 'mean_word_length': 3.938618925831202, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1095678', 'n_tokens_mistral': 1039, 'n_tokens_neox': 1012, 'n_words': 198}