-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
157 lines (131 loc) · 3.66 KB
/
query.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 yfquery
import (
"bytes"
"fmt"
"net/http"
"strings"
"github.com/antchfx/xmlquery"
"github.com/famendola1/yfquery/schema"
)
const (
endpoint = "https://fantasysports.yahooapis.com/fantasy/v2"
)
type query struct {
base string
resource string
isCollection bool
keys []string
outs []string
params []string
payload string
}
// ToString generates the endpoint string for the query
func (q *query) ToString() string {
var uri string
if q.base != "" {
uri = q.base + "/"
}
if q.isCollection {
uri += q.resource + "s"
if len(q.keys) != 0 {
uri += fmt.Sprintf(";%s_keys=%s", q.resource, strings.Join(q.keys, ","))
}
} else {
uri += q.resource
if len(q.keys) != 0 {
uri += ("/" + q.keys[0])
}
}
if len(q.outs) == 1 {
uri += fmt.Sprintf("/%s", q.outs[0])
}
if len(q.outs) > 1 {
uri += fmt.Sprintf(";out=%s", strings.Join(q.outs, ","))
}
if len(q.params) != 0 {
uri += (";" + strings.Join(q.params, ";"))
}
if uri[0] != '/' {
uri = "/" + uri
}
return uri
}
// Get sends a "GET" request to the Yahoo Fantasy endpoint that the query would
// generate using the provided client.
func (q *query) Get(client *http.Client) (*schema.FantasyContent, error) {
return sendRequest(client, "GET", q.ToString(), "")
}
// Post sends a "POST" request to the Yahoo Fantasy endpoint that the query would
// generate using the provided client along with the payload.
func (q *query) Post(client *http.Client) (*schema.FantasyContent, error) {
return sendRequest(client, "POST", q.ToString(), q.payload)
}
// Put sends a "PUT" request to the Yahoo Fantasy endpoint that the query would
// generate using the provided client along with the payload.
func (q *query) Put(client *http.Client) (*schema.FantasyContent, error) {
return sendRequest(client, "PUT", q.ToString(), q.payload)
}
// Delete sends a "DELETE" request to the Yahoo Fantasy endpoint that the query would
// generate using the provided client along with the payload.
func (q *query) Delete(client *http.Client) (*schema.FantasyContent, error) {
return sendRequest(client, "DELETE", q.ToString(), q.payload)
}
func (q *query) Reset() {
q.keys = []string{}
q.outs = []string{}
q.params = []string{}
q.payload = ""
}
func sendRequest(client *http.Client, method, uri, payload string) (*schema.FantasyContent, error) {
var fc schema.FantasyContent
var err error
var resp *http.Response
switch method {
case "GET":
resp, err = client.Get((endpoint + uri))
break
case "POST":
resp, err = client.Post((endpoint + uri), "application/xml", strings.NewReader(payload))
break
case "PUT":
req, err2 := http.NewRequest("PUT", (endpoint + uri), strings.NewReader(payload))
if err2 != nil {
return nil, err2
}
req.Header.Add("content-type", "application/xml")
resp, err = client.Do(req)
break
case "DELETE":
req, err2 := http.NewRequest("DELETE", (endpoint + uri), strings.NewReader(payload))
if err2 != nil {
return nil, err2
}
req.Header.Add("content-type", "application/xml")
resp, err = client.Do(req)
break
}
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, handleError(resp)
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
if err = parse(buf.String(), "//fantasy_content", &fc); err != nil {
return nil, err
}
return &fc, nil
}
// handleError returns an error containing the error message in the response.
func handleError(resp *http.Response) error {
doc, err := xmlquery.Parse(resp.Body)
if err != nil {
return err
}
node, err := xmlquery.Query(doc, "//description")
if err != nil {
return err
}
return fmt.Errorf("%s: %s", resp.Status, node.InnerText())
}