-
Notifications
You must be signed in to change notification settings - Fork 11
/
internal.go
68 lines (59 loc) · 1.65 KB
/
internal.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
package gw2api
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
)
//Timeout solution adapted from Volker on stackoverflow
func (gw2 *GW2Api) dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, gw2.timeout)
}
func (gw2 *GW2Api) fetchRawEndpoint(url string) (io io.ReadCloser, err error) {
var resp *http.Response
if resp, err = gw2.client.Get(url); err != nil {
return
}
return resp.Body, nil
}
func (gw2 *GW2Api) fetchEndpoint(ver, tag string, params url.Values, result interface{}) (err error) {
var endpoint *url.URL
endpoint, _ = url.Parse("https://api.guildwars2.com")
endpoint.Path += "/" + ver + "/" + tag
if params != nil {
endpoint.RawQuery = params.Encode()
}
var resp *http.Response
if resp, err = gw2.client.Get(endpoint.String()); err != nil {
return err
}
var data []byte
if data, err = ioutil.ReadAll(resp.Body); err != nil {
return err
}
defer resp.Body.Close()
if err = json.Unmarshal(data, &result); err != nil {
var gwerr APIError
if err = json.Unmarshal(data, &gwerr); err != nil {
return err
}
return fmt.Errorf("Endpoint returned error: %v", gwerr)
}
return
}
func (gw2 *GW2Api) fetchAuthenticatedEndpoint(ver, tag string, perm Permission, params url.Values, result interface{}) (err error) {
if len(gw2.auth) < 1 {
return fmt.Errorf("API requires authentication")
}
if perm >= PermAccount && !flagGet(gw2.authFlags, uint(perm)) {
return fmt.Errorf("Missing permissions for authenticated Endpoint")
}
if params == nil {
params = url.Values{}
}
params.Add("access_token", gw2.auth)
return gw2.fetchEndpoint(ver, tag, params, result)
}