162
vendor/github.com/status-im/status-go/services/gif/gif.go
generated
vendored
Normal file
162
vendor/github.com/status-im/status-go/services/gif/gif.go
generated
vendored
Normal file
@@ -0,0 +1,162 @@
|
||||
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"
|
||||
"github.com/status-im/status-go/multiaccounts/settings"
|
||||
)
|
||||
|
||||
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.SaveSettingField(settings.GifAPIKey, 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()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("Status error: %v", response.StatusCode)
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Read body: %v", err)
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func (api *API) FetchGifs(path string) (value string, err error) {
|
||||
log.Info("[GifAPI::fetchGifs]")
|
||||
return api.GetContentWithRetry(path)
|
||||
}
|
||||
|
||||
func (api *API) UpdateRecentGifs(updatedGifs json.RawMessage) (err error) {
|
||||
log.Info("[GifAPI::updateRecentGifs]")
|
||||
recentGifsContainer := Container{}
|
||||
err = json.Unmarshal(updatedGifs, &recentGifsContainer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = api.db.SaveSettingField(settings.GifRecents, recentGifsContainer.Items)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (api *API) UpdateFavoriteGifs(updatedGifs json.RawMessage) (err error) {
|
||||
log.Info("[GifAPI::updateFavoriteGifs]", updatedGifs)
|
||||
favsGifsContainer := Container{}
|
||||
err = json.Unmarshal(updatedGifs, &favsGifsContainer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = api.db.SaveSettingField(settings.GifFavourites, favsGifsContainer.Items)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (api *API) GetRecentGifs() (recentGifs []Gif, err error) {
|
||||
log.Info("[GifAPI::getRecentGifs]")
|
||||
gifs, err := api.db.GifRecents()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
recentGifs = make([]Gif, 0)
|
||||
savedRecentGifs := []Gif{}
|
||||
if len(gifs) > 0 {
|
||||
err = json.Unmarshal(gifs, &savedRecentGifs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
recentGifs = savedRecentGifs
|
||||
}
|
||||
return recentGifs, nil
|
||||
}
|
||||
|
||||
func (api *API) GetFavoriteGifs() (favoriteGifs []Gif, err error) {
|
||||
log.Info("[GifAPI::getFavoriteGifs]")
|
||||
gifs, err := api.db.GifFavorites()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
favoriteGifs = make([]Gif, 0)
|
||||
savedFavGifs := []Gif{}
|
||||
if len(gifs) > 0 {
|
||||
err = json.Unmarshal(gifs, &savedFavGifs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
favoriteGifs = savedFavGifs
|
||||
}
|
||||
return favoriteGifs, nil
|
||||
}
|
||||
45
vendor/github.com/status-im/status-go/services/gif/service.go
generated
vendored
Normal file
45
vendor/github.com/status-im/status-go/services/gif/service.go
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
package gif
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
|
||||
"github.com/status-im/status-go/multiaccounts/accounts"
|
||||
)
|
||||
|
||||
// Service represents out own implementation of personal sign operations.
|
||||
type Service struct {
|
||||
accountsDB *accounts.Database
|
||||
}
|
||||
|
||||
// New returns a new Service.
|
||||
func NewService(db *accounts.Database) *Service {
|
||||
return &Service{accountsDB: db}
|
||||
}
|
||||
|
||||
// Protocols returns a new protocols list. In this case, there are none.
|
||||
func (s *Service) Protocols() []p2p.Protocol {
|
||||
return []p2p.Protocol{}
|
||||
}
|
||||
|
||||
// APIs returns a list of new APIs.
|
||||
func (s *Service) APIs() []rpc.API {
|
||||
return []rpc.API{
|
||||
{
|
||||
Namespace: "gif",
|
||||
Version: "0.1.0",
|
||||
Service: NewGifAPI(s.accountsDB),
|
||||
Public: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Start is run when a service is started.
|
||||
func (s *Service) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop is run when a service is stopped.
|
||||
func (s *Service) Stop() error {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user