-
Notifications
You must be signed in to change notification settings - Fork 2
/
seasonal_player_stats.go
157 lines (128 loc) · 4.27 KB
/
seasonal_player_stats.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package msf
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
retryablehttp "github.com/hashicorp/go-retryablehttp"
logrus "github.com/sirupsen/logrus"
)
// SeasonalPlayerStatsOptions - Are the options to hit the seasonal player stats endpoint
type SeasonalPlayerStatsOptions struct {
// URL Parts
URL string
Version string
Sport string
Season string
Format string
// Optional URL Params
Player string
Position string
Country string
Team string
Date string
Stats string
Sort string
Offset string
Limit string
Force string
}
// NewSeasonalPlayerStatsOptions - Returns the options with most url parts already set to hit the seasonal player stats endpoint
func (s *Service) NewSeasonalPlayerStatsOptions() *SeasonalPlayerStatsOptions {
return &SeasonalPlayerStatsOptions{
URL: s.Config.BaseURL,
Version: s.Config.Version,
Sport: s.Config.Sport,
Format: s.Config.Format,
Season: s.Config.Season,
}
}
// SeasonalPlayerStats - hits the https://api.mysportsfeeds.com/{version}/pull/{sport}/{season}/player_stats_totals.{format} endoint
func (s *Service) SeasonalPlayerStats(options *SeasonalPlayerStatsOptions) (PlayerStatsTotalsIO, int, error) {
mapping := PlayerStatsTotalsIO{}
// make sure we have all the required elements to build the full required url string.
err := validateSeasonalPlayerStatsURI(options)
if err != nil {
return mapping, 0, err
}
t := time.Now()
cacheBuster := t.Format("20060102150405")
uri := fmt.Sprintf("%s/%s/pull/%s/%s/player_stats_totals.%s?cachebuster=%s", options.URL, options.Version, options.Sport, options.Season, options.Format, cacheBuster)
if len(options.Player) > 0 {
uri = fmt.Sprintf("%s&player=%s", uri, options.Player)
}
if len(options.Position) > 0 {
uri = fmt.Sprintf("%s&position=%s", uri, options.Position)
}
if len(options.Country) > 0 {
uri = fmt.Sprintf("%s&country=%s", uri, options.Country)
}
if len(options.Team) > 0 {
uri = fmt.Sprintf("%s&team=%s", uri, options.Team)
}
if len(options.Date) > 0 {
uri = fmt.Sprintf("%s&date=%s", uri, options.Date)
}
if len(options.Stats) > 0 {
uri = fmt.Sprintf("%s&stats=%s", uri, options.Stats)
}
if len(options.Sort) > 0 {
uri = fmt.Sprintf("%s&sort=%s", uri, options.Sort)
}
if len(options.Offset) > 0 {
uri = fmt.Sprintf("%s&offset=%s", uri, options.Offset)
}
if len(options.Limit) > 0 {
uri = fmt.Sprintf("%s&limit=%s", uri, options.Limit)
}
if len(options.Force) > 0 {
uri = fmt.Sprintf("%s&force=%s", uri, options.Force)
}
s.Logger = s.Logger.WithFields(logrus.Fields{
"URI": uri,
})
s.Logger.Debug("SeasonalPlayerStats API Call")
// make you a client
client := retryablehttp.NewClient()
req, err := retryablehttp.NewRequest(http.MethodGet, uri, nil)
if err != nil {
s.Logger.Errorf("client: could not create request: %s", err.Error())
return mapping, 0, err
}
req.Header.Add("Authorization", CompressionHeaderGzip)
req.Header.Add("Authorization", s.Config.Authorization)
response, err := client.Do(req)
if err != nil {
s.Logger.Errorf("client: error making http request: %s", err.Error())
return mapping, 0, err
}
if response.StatusCode < 200 || response.StatusCode > 300 {
s.Logger.Errorf("client: something went wrong making the get request for SeasonalPlayerStats: %s", err.Error())
return mapping, response.StatusCode, err
}
s.Logger.Infof("SeasonalPlayerStats Status Code: %d", response.StatusCode)
if jErr := json.NewDecoder(response.Body).Decode(&mapping); jErr != nil {
s.Logger.Errorf("client: error decoding response for SeasonalPlayerStats: %s", err.Error())
return mapping, response.StatusCode, err
}
return mapping, response.StatusCode, nil
}
func validateSeasonalPlayerStatsURI(options *SeasonalPlayerStatsOptions) error {
if len(options.URL) == 0 {
return errors.New("missing required option to build the url: URL")
}
if len(options.Version) == 0 {
return errors.New("missing required option to build the url: Version")
}
if len(options.Sport) == 0 {
return errors.New("missing required option to build the url: Sport")
}
if len(options.Season) == 0 {
return errors.New("missing required option to build the url: Season")
}
if len(options.Format) == 0 {
return errors.New("missing required option to build the url: Format")
}
return nil
}