<commit_before>package mapping

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/Shop2market/go-client/mapping/cache"
)

const PATH = "/api/v1/mapping_files.json"

type Repo struct {
	Endpoint string
	Username string
	Password string
	Notifier chan string

	Cache *cache.Cache
}

func New(endpoint, username, password string) *Repo {
	return &Repo{endpoint, username, password, make(chan string), cache.New()}
}

func (repo *Repo) FindAllMappings() (mappings map[string][][]string, err error) {
	if repo.Cache.IsValid() {
		mappings, err = repo.Cache.Get()
		if err != nil {
			repo.Notifier <- ".FindAllMappings(): cache not found"
		} else {
			return
		}
	}

	request, err := http.NewRequest("GET", repo.Endpoint, nil)
	if err != nil {
		repo.Notifier <- fmt.Sprintf(".FindAllMappings(): failed: %s | Endpoint: %s", err.Error(), repo.Endpoint)
		return
	}

	request.SetBasicAuth(repo.Username, repo.Password)
	response, err := http.DefaultClient.Do(request)
	if err != nil {
		repo.Notifier <- fmt.Sprintf(".FindAllMappings(): failed: %s", err.Error())
		return
	}
	defer response.Body.Close()
	if response.StatusCode >= 400 {
		err = fmt.Errorf("Responded with error: %s", response.Status)
		repo.Notifier <- fmt.Sprintf(".FindAllMappings(): failed: %s", err.Error())
		return
	}
	err = json.NewDecoder(response.Body).Decode(&mappings)<commit_msg>TTPD-174: makes errors more informative<commit_after>package mapping

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/Shop2market/go-client/mapping/cache"
)

const PATH = "/api/v1/mapping_files.json"

type Repo struct {
	Endpoint string
	Username string
	Password string
	Notifier chan string

	Cache *cache.Cache
}

func New(endpoint, username, password string) *Repo {
	return &Repo{endpoint, username, password, make(chan string), cache.New()}
}

func (repo *Repo) FindAllMappings() (mappings map[string][][]string, err error) {
	if repo.Cache.IsValid() {
		mappings, err = repo.Cache.Get()
		if err != nil {
			repo.Notifier <- ".FindAllMappings(): cache not found"
		} else {
			return
		}
	}

	request, err := http.NewRequest("GET", repo.Endpoint, nil)
	if err != nil {
		err = fmt.Errorf(".FindAllMappings(): failed: %s | Endpoint: %s", err.Error(), repo.Endpoint)
		repo.Notifier <- err.Error()
		return
	}

	request.SetBasicAuth(repo.Username, repo.Password)
	response, err := http.DefaultClient.Do(request)
	if err != nil {
		repo.Notifier <- fmt.Sprintf(".FindAllMappings(): failed: %s", err.Error())
		return
	}
	defer response.Body.Close()
	if response.StatusCode >= 400 {
		err = fmt.Errorf("Responded with error: %s", response.Status)
		repo.Notifier <- fmt.Sprintf(".FindAllMappings(): failed: %s", err.Error())
		return
	}
	err = json.NewDecoder(response.Body).Decode(&mappings)
{'fraction_non_alphanumeric': 0.15540296349837368, 'fraction_numerical': 0.0046982291290206, 'mean_word_length': 4.302681992337165, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '1502923', 'n_tokens_mistral': 992, 'n_tokens_neox': 937, 'n_words': 225}