-
Notifications
You must be signed in to change notification settings - Fork 0
/
challonge.go
147 lines (137 loc) · 3.19 KB
/
challonge.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
package challonge
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const BASE_URL = "api.challonge.com/v1/"
type ChallongeClient struct {
BaseUrl string
Username string
ApiKey string
Client *http.Client
}
func NewChallongeClient(u, k string) *ChallongeClient {
return &ChallongeClient{
BaseUrl: BASE_URL,
Username: u,
ApiKey: k,
Client: &http.Client{},
}
}
func NewChallongeTestClient(b, u, k string) *ChallongeClient {
return &ChallongeClient{
BaseUrl: b,
Username: u,
ApiKey: k,
Client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
},
}
}
// When unmarshalling the response body, if the supplied body is an error message,
// return either a helpful error message or just the entire raw body for better debugging
// instead of an unhelpful error returned when trying to unmarshal a bad response payload.
func checkForChallongeError(b string) error {
if b == "HTTP Basic: Access denied." {
return errors.New("challonge-go: invalid credentials")
}
if strings.Contains(b, "errors") {
return errors.New(fmt.Sprintf("challonge-go: errors present: %s", b))
}
return nil
}
func (c *ChallongeClient) Get(uri string, p interface{}) error {
url := fmt.Sprintf("https://%s:%s@%s%s.json", c.Username, c.ApiKey, c.BaseUrl, uri)
resp, err := c.Client.Get(url)
if err != nil {
return err
}
i, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Printf("%v\n", string(i))
err = checkForChallongeError(string(i))
if err != nil {
return err
}
err = json.Unmarshal(i, p)
if err != nil {
return err
}
return nil
}
func (c *ChallongeClient) Post(uri string, p interface{}, r interface{}) error {
url := fmt.Sprintf("https://%s:%s@%s%s.json", c.Username, c.ApiKey, c.BaseUrl, uri)
d, err := json.Marshal(p)
if err != nil {
return err
}
resp, err := c.Client.Post(url, "application/json", bytes.NewBuffer(d))
if err != nil {
return err
}
i, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = checkForChallongeError(string(i))
if err != nil {
return err
}
err = json.Unmarshal(i, r)
if err != nil {
return err
}
return nil
}
func (c *ChallongeClient) Put(uri string, p interface{}, r interface{}) error {
url := fmt.Sprintf("https://%s:%s@%s%s.json", c.Username, c.ApiKey, c.BaseUrl, uri)
d, err := json.Marshal(p)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(d))
resp, err := c.Client.Do(req)
i, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = checkForChallongeError(string(i))
if err != nil {
return err
}
err = json.Unmarshal(i, r)
if err != nil {
return err
}
return nil
}
func (c *ChallongeClient) Delete(uri string, r interface{}) error {
url := fmt.Sprintf("https://%s:%s@%s%s.json", c.Username, c.ApiKey, c.BaseUrl, uri)
req, err := http.NewRequest("DELETE", url, nil)
resp, err := c.Client.Do(req)
i, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = checkForChallongeError(string(i))
if err != nil {
return err
}
err = json.Unmarshal(i, r)
if err != nil {
return err
}
return nil
}