-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatches.go
149 lines (126 loc) · 3.9 KB
/
matches.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
package tba
import (
"encoding/json"
"github.com/akrantz01/go-tba/responses"
"github.com/mitchellh/mapstructure"
"net/http"
"strconv"
)
// Get information about a match
func Match(key, apiKey string, opts *RequestOptions) (*responses.Match, int, error) {
// Generate the request
req := newRequest("/match/"+key, apiKey, opts)
// Send the request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, 0, err
}
// Check the response type
if resp.StatusCode == http.StatusUnauthorized {
return nil, resp.StatusCode, ErrInvalidToken
} else if resp.StatusCode == http.StatusNotModified {
return nil, resp.StatusCode, nil
} else if resp.StatusCode != 200 {
return nil, resp.StatusCode, err
}
// Decode body
var match responses.Match
if err := json.NewDecoder(resp.Body).Decode(&match); err != nil {
return nil, resp.StatusCode, err
}
// Close the body
if err := resp.Body.Close(); err != nil {
return nil, resp.StatusCode, err
}
// Coerce score breakdown type
year, _ := strconv.ParseInt(match.Key[:4], 10, 64)
switch year {
case 2020:
var score responses.ScoringBreakdown2020
if err := mapstructure.Decode(match.ScoreBreakdown, &score); err != nil {
return nil, resp.StatusCode, err
}
match.ScoreBreakdown = score
case 2019:
var score responses.ScoringBreakdown2019
if err := mapstructure.Decode(match.ScoreBreakdown, &score); err != nil {
return nil, resp.StatusCode, err
}
match.ScoreBreakdown = score
case 2018:
var score responses.ScoringBreakdown2018
if err := mapstructure.Decode(match.ScoreBreakdown, &score); err != nil {
return nil, resp.StatusCode, err
}
match.ScoreBreakdown = score
case 2017:
var score responses.ScoringBreakdown2017
if err := mapstructure.Decode(match.ScoreBreakdown, &score); err != nil {
return nil, resp.StatusCode, err
}
match.ScoreBreakdown = score
case 2016:
var score responses.ScoringBreakdown2016
if err := mapstructure.Decode(match.ScoreBreakdown, &score); err != nil {
return nil, resp.StatusCode, err
}
match.ScoreBreakdown = score
}
return &match, resp.StatusCode, nil
}
// Get a simplified information about a match
func MatchSimple(key, apiKey string, opts *RequestOptions) (*responses.MatchSimple, int, error) {
// Generate the request
req := newRequest("/match/"+key+"/simple", apiKey, opts)
// Send the request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, 0, err
}
// Check the response type
if resp.StatusCode == http.StatusUnauthorized {
return nil, resp.StatusCode, ErrInvalidToken
} else if resp.StatusCode == http.StatusNotModified {
return nil, resp.StatusCode, nil
} else if resp.StatusCode != 200 {
return nil, resp.StatusCode, err
}
// Decode body
var match responses.MatchSimple
if err := json.NewDecoder(resp.Body).Decode(&match); err != nil {
return nil, resp.StatusCode, err
}
// Close the body
if err := resp.Body.Close(); err != nil {
return nil, resp.StatusCode, err
}
return &match, resp.StatusCode, nil
}
// Get timeseries information about a match
func MatchTimeseries(key, apiKey string, opts *RequestOptions) (*responses.Timeseries2018, int, error) {
// Generate the request
req := newRequest("/match/"+key+"/timeseries", apiKey, opts)
// Send the request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, 0, err
}
// Check the response type
if resp.StatusCode == http.StatusUnauthorized {
return nil, resp.StatusCode, ErrInvalidToken
} else if resp.StatusCode == http.StatusNotModified {
return nil, resp.StatusCode, nil
} else if resp.StatusCode != 200 {
return nil, resp.StatusCode, err
}
// Decode body
var timeseries responses.Timeseries2018
if err := json.NewDecoder(resp.Body).Decode(×eries); err != nil {
return nil, resp.StatusCode, err
}
// Close the body
if err := resp.Body.Close(); err != nil {
return nil, resp.StatusCode, err
}
return ×eries, resp.StatusCode, nil
}