<commit_before>package edu.upc.caminstech.equipstic.client.dao;

import static java.util.stream.Collectors.toList;

import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

import edu.upc.caminstech.equipstic.client.EquipsTicClientConfiguration;
import edu.upc.caminstech.equipstic.client.EquipsTicClientException;
import edu.upc.caminstech.equipstic.client.Response;

/**
 * Classe d'ús intern de la llibreria.
 */
public class RestDao {

    protected final Logger logger = LoggerFactory.getLogger(RestDao.class);

    private final URI baseUri;
    private final RestTemplate restTemplate;

    public RestDao(EquipsTicClientConfiguration config) {
        this.baseUri = config.getBaseUri();
        this.restTemplate = config.getRestTemplate();
    }

    public URI getBaseUri() {
        return this.baseUri;
    }

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }

    /**
     * Mètode auxiliar que encapsula crides GET a la API, via
     * {@link RestTemplate}.
     */
    public <T> T get(String url, ParameterizedTypeReference<Response<T>> typeReference, Object... urlParams) {
        ResponseEntity<Response<T>> entity = null;

        String requestDetails = String.format("[url: %s, urlParams: %s, typeReference: %s]", baseUri + url,
                Arrays.toString(urlParams), typeReference);

        try {
            entity = restTemplate.exchange(baseUri + url, HttpMethod.GET, null, typeReference, urlParams);
        } catch (HttpClientErrorException e) {
            switch (e.getStatusCode()) {
            case NOT_FOUND:
                throw new EquipsTicClientException("No s'ha trobat el recurs " + requestDetails, e);
            case UNAUTHORIZED:
                throw new EquipsTicClientException("No teniu privilegis per accedir al recurs " + requestDetails, e);
            default:
                throw new EquipsTicClientException("S'ha produït un error " + requestDetails, e);
            }
        } catch (HttpServerErrorException e) {
            throw new EquipsTicClientException("El servidor ha generat un error " + requestDetails, e);
        }

        return entity.getBody().getData();
    }

    /**
     * Mètode auxiliar que encapsula crides DELETE a la API, via
     * {@link RestTemplate}.
     */
    public <T> void delete(String url, ParameterizedTypeReference<Response<T>> typeReference, Object... urlParams) {
        try {
            restTemplate.exchange(baseUri + url, HttpMethod.DELETE, null, typeReference, urlParams);
        } catch (HttpClientErrorException e) {
            if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
                throw new EquipsTicClientException(e);
            }
            throw e;
        }
    }

    /**
     * Mètode auxiliar <em>null-safe</em> per ordenar llistes genèriques.
     * 
     * @param list
     *            la llista a ordenar (pot ser {@code null}).
     * @return la llista ordenada (pot estar buida).
     */
    public static <T extends Comparable<T>> List<T> sorted(List<T> list) {
        /**
         * TODO: Aquest mètode s'hauria de moure a una altra classe, ja que és
         * massa genèric.
         */
        if (list == null) {
            return Collections.emptyList();
        }
        return list.stream().sorted().collect(toList());
    }
}<commit_msg>refactor: cleaned superfluous error discrimination<commit_after>package edu.upc.caminstech.equipstic.client.dao;

import static java.util.stream.Collectors.toList;

import java.net.URI;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import edu.upc.caminstech.equipstic.client.EquipsTicClientConfiguration;
import edu.upc.caminstech.equipstic.client.EquipsTicClientException;
import edu.upc.caminstech.equipstic.client.Response;

/**
 * Classe d'ús intern de la llibreria.
 */
public class RestDao {

    protected final Logger logger = LoggerFactory.getLogger(RestDao.class);

    private final URI baseUri;
    private final RestTemplate restTemplate;

    public RestDao(EquipsTicClientConfiguration config) {
        this.baseUri = config.getBaseUri();
        this.restTemplate = config.getRestTemplate();
    }

    public URI getBaseUri() {
        return this.baseUri;
    }

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }

    /**
     * Mètode auxiliar que encapsula crides GET a la API, via
     * {@link RestTemplate}.
     */
    public <T> T get(String url, ParameterizedTypeReference<Response<T>> typeReference, Object... urlParams) {
        try {
            ResponseEntity<Response<T>> entity = restTemplate.exchange(baseUri + url, HttpMethod.GET, null,
                    typeReference, urlParams);
            return entity.getBody().getData();
        } catch (RestClientResponseException e) {
            String msg = String.format("Error en obtenir el recurs [%s]", getResourcePath(url, urlParams));
            throw new EquipsTicClientException(msg, e);
        }
    }

    /**
     * Mètode auxiliar que encapsula crides DELETE a la API, via
     * {@link RestTemplate}.
     */
    public <T> void delete(String url, ParameterizedTypeReference<Response<T>> typeReference, Object... urlParams) {
        try {
            restTemplate.exchange(baseUri + url, HttpMethod.DELETE, null, typeReference, urlParams);
        } catch (RestClientResponseException e) {
            String msg = String.format("Error en obtenir el recurs [%s]", getResourcePath(url, urlParams));
            throw new EquipsTicClientException(msg, e);
        }
    }

    /**
     * Mètode auxiliar <em>null-safe</em> per ordenar llistes genèriques.
     * 
     * @param list
     *            la llista a ordenar (pot ser {@code null}).
     * @return la llista ordenada (pot estar buida).
     */
    public static <T extends Comparable<T>> List<T> sorted(List<T> list) {
        /**
         * TODO: Aquest mètode s'hauria de moure a una altra classe, ja que és
         * massa genèric.
         */
        if (list == null) {
            return Collections.emptyList();
        }
        return list.stream().sorted().collect(toList());
    }

    private String getResourcePath(String url, Object... urlParams) {
        return UriComponentsBuilder.fromPath(url).buildAndExpand(urlParams).toUri().toString();
    }

}
{'fraction_non_alphanumeric': 0.10198379435596536, 'fraction_numerical': 0.0005588153115395362, 'mean_word_length': 2.920591456736035, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, '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': '3020334', 'n_tokens_mistral': 2085, 'n_tokens_neox': 2067, 'n_words': 497}