<commit_before>package gif

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/ethereum/go-ethereum/log"
	"github.com/status-im/status-go/multiaccounts/accounts"
)

type Gif struct {
	ID         string `json:"id"`
	Title      string `json:"title"`
	URL        string `json:"url"`
	TinyURL    string `json:"tinyUrl"`
	Height     int    `json:"height"`
	IsFavorite bool   `json:"isFavorite"`
}

type Container struct {
	Items []Gif `json:"items"`
}

var tenorAPIKey = ""
var defaultParams = "&media_filter=minimal&limit=50&key="

const maxRetry = 3
const baseURL = "https://g.tenor.com/v1/"

func NewGifAPI(db *accounts.Database) *API {
	return &API{db, &http.Client{Timeout: time.Minute}}
}

// API is class with methods available over RPC.
type API struct {
	db         *accounts.Database
	httpClient *http.Client
}

func (api *API) SetTenorAPIKey(key string) (err error) {
	log.Info("[GifAPI::SetTenorAPIKey]")
	err = api.db.SaveSetting("gifs/api-key", key)
	if err != nil {
		return err
	}
	tenorAPIKey = key
	return nil
}

func (api *API) GetContentWithRetry(path string) (value string, err error) {
	var currentRetry = 0
	var response *http.Response

	for currentRetry < maxRetry {
		response, err = api.httpClient.Get(baseURL + path + defaultParams + tenorAPIKey)
		if err != nil {
			log.Error("can't get content from path %s", path)
			currentRetry++
			time.Sleep(100 * time.Millisecond)
		} else {
			break
		}
	}

	defer response.Body.Close()<commit_msg>fix(@gif): fix retry when connection lost (#2520)<commit_after>package gif

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/ethereum/go-ethereum/log"
	"github.com/status-im/status-go/multiaccounts/accounts"
)

type Gif struct {
	ID         string `json:"id"`
	Title      string `json:"title"`
	URL        string `json:"url"`
	TinyURL    string `json:"tinyUrl"`
	Height     int    `json:"height"`
	IsFavorite bool   `json:"isFavorite"`
}

type Container struct {
	Items []Gif `json:"items"`
}

var tenorAPIKey = ""
var defaultParams = "&media_filter=minimal&limit=50&key="

const maxRetry = 3
const baseURL = "https://g.tenor.com/v1/"

func NewGifAPI(db *accounts.Database) *API {
	return &API{db}
}

// API is class with methods available over RPC.
type API struct {
	db *accounts.Database
}

func (api *API) SetTenorAPIKey(key string) (err error) {
	log.Info("[GifAPI::SetTenorAPIKey]")
	err = api.db.SaveSetting("gifs/api-key", key)
	if err != nil {
		return err
	}
	tenorAPIKey = key
	return nil
}

func (api *API) GetContentWithRetry(path string) (value string, err error) {
	var currentRetry = 0
	var response *http.Response
	for currentRetry < maxRetry {
		transport := &http.Transport{
			Proxy:                 http.ProxyFromEnvironment,
			ResponseHeaderTimeout: time.Second * 1,
		}

		client := http.Client{
			Timeout:   1 * time.Second,
			Transport: transport,
		}

		response, err = client.Get(baseURL + path + defaultParams + tenorAPIKey)

		if err != nil {
			log.Error("can't get content from path %s with %s", path, err.Error())
			currentRetry++
			time.Sleep(100 * time.Millisecond)
		} else {
			break
		}
	}

	if response == nil {
		return "", fmt.Errorf("Could not reach Tenor API")
	}
	defer response.Body.Close()
{'fraction_non_alphanumeric': 0.13790989886607416, 'fraction_numerical': 0.006742261722341404, 'mean_word_length': 3.642958748221906, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5282812', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1182, 'n_words': 291}