forked from joelhill/mysportsfeeds-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
current_season.go
107 lines (88 loc) · 2.95 KB
/
current_season.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
package msf
import (
"context"
"errors"
"fmt"
"time"
blaster "github.com/joelhill/go-rest-http-blaster"
logrus "github.com/sirupsen/logrus"
)
// CurrentSeasonOptions - Are the options to hit the current season endpoint
type CurrentSeasonOptions struct {
// URL Parts
URL string
Version string
Sport string
Format string
// Optional URL Params
Date string
Force string
}
// NewCurrentSeasonOptions - Returns the options with most url parts already set to hit the current season endpoint
func (s *Service) NewCurrentSeasonOptions() *CurrentSeasonOptions {
return &CurrentSeasonOptions{
URL: s.Config.BaseURL,
Version: s.Config.Version,
Sport: s.Config.Sport,
Format: s.Config.Format,
}
}
// CurrentSeason - hits the https://api.mysportsfeeds.com/{version}/pull/{sport}/current_season.{format} endpoint
func (s *Service) CurrentSeason(c context.Context, options *CurrentSeasonOptions) (CurrentSeasonIO, int, error) {
errorPayload := make(map[string]interface{})
mapping := CurrentSeasonIO{}
// make sure we have all the required elements to build the full required url string.
err := validateCurrentSeasonURI(options)
if err != nil {
return mapping, 0, err
}
t := time.Now()
cacheBuster := t.Format("20060102150405")
uri := fmt.Sprintf("%s/%s/pull/%s/current_season.%s?cachebuster=%s", options.URL, options.Version, options.Sport, options.Format, cacheBuster)
if len(options.Date) > 0 {
uri = fmt.Sprintf("%s&date=%s", uri, options.Date)
}
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("CurrentSeason API Call")
// make you a client
client, err := blaster.NewClient(uri)
if err != nil {
s.Logger.Errorf("failed to create a http client: %s", err.Error())
return mapping, 0, err
}
client.SetHeader("Accept-Encoding", CompressionHeaderGzip)
client.SetHeader("Authorization", s.Config.Authorization)
client.WillSaturateOnError(&errorPayload)
client.WillSaturate(&mapping)
ctx := context.Background()
statusCode, err := client.Get(ctx)
if err != nil {
s.Logger.Errorf("something went wrong making the get request for CurrentSeason: %s", err.Error())
return mapping, statusCode, err
}
s.Logger.Infof("CurrentSeason Status Code: %d", statusCode)
if client.StatusCodeIsError() {
s.Logger.Errorf("CurrentSeason retuned an unsuccessful status code. Error: %+v", errorPayload)
}
return mapping, statusCode, nil
}
func validateCurrentSeasonURI(options *CurrentSeasonOptions) 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.Format) == 0 {
return errors.New("missing required option to build the url: Format")
}
return nil
}