-
Notifications
You must be signed in to change notification settings - Fork 0
/
summonerendpoints.go
82 lines (69 loc) · 2.52 KB
/
summonerendpoints.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package storage
import (
"encoding/json"
"fmt"
"io"
"net/http"
"sync/atomic"
"time"
"github.com/torlenor/alolstats/riotclient"
)
// SummonerResponse contains summary informations of a summoner
type SummonerResponse struct {
Name string `json:"name"`
ProfileIcon int `json:"profileIconId"`
SummonerLevel int64 `json:"summonerLevel"`
RevisionDate int64 `json:"revisionDate"`
Timestamp time.Time `json:"timestamp"`
LeagueRankings riotclient.LeaguePositionDTOList `json:"leagues"`
}
func (s *Storage) prepareSummonerResponse(summonerName string, forceUpdate bool) (*SummonerResponse, error) {
summoner, err := s.GetSummonerByName(summonerName, forceUpdate)
if err != nil {
return nil, fmt.Errorf("Error getting SummonerByName data")
}
summonerResponse := SummonerResponse{
Name: summoner.Name,
ProfileIcon: summoner.ProfileIcon,
SummonerLevel: summoner.SummonerLevel,
RevisionDate: summoner.RevisionDate,
Timestamp: summoner.Timestamp,
}
leagues, err := s.GetLeaguesForSummonerBySummonerID(summoner.ID, forceUpdate)
if err == nil {
summonerResponse.LeagueRankings = leagues
} else {
s.log.Warnf("Unable to get League Data for Summoner %s: %s", summoner.Name, err)
summonerResponse.LeagueRankings = riotclient.LeaguePositionDTOList{}
}
return &summonerResponse, nil
}
func (s *Storage) summonerByNameEndpoint(w http.ResponseWriter, r *http.Request) {
s.log.Debugln("Received Rest API SummonerByName request from", r.RemoteAddr)
var summonerName string
if val, ok := r.URL.Query()["name"]; ok {
if len(val[0]) == 0 {
s.log.Warnf("name parameter was empty in request")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
summonerName = val[0]
} else {
s.log.Warnf("There was no name parameter in request")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
summonerResponse, err := s.prepareSummonerResponse(summonerName, checkParamterForceUpdate(r.URL.Query()))
if err != nil {
s.log.Warnf("Error preparing Summoner Response: %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
}
out, err := json.Marshal(summonerResponse)
if err != nil {
s.log.Errorln(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
io.WriteString(w, string(out))
atomic.AddUint64(&s.stats.handledRequests, 1)
}