-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
79 lines (65 loc) · 1.6 KB
/
base.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
package goBungieNet
import (
"encoding/json"
"net/http"
"errors"
"io"
// "fmt"
)
const serverBase string = "https://bungie.net"
const apiBase string = serverBase + "/Platform"
type Response struct {
Response interface{}
ErrorCode errorCode
ThrottleSeconds int
ErrorStatus string
Message string
MessageData map[string]string
}
type PrivacySetting int
const (
PrivacyNone PrivacySetting = 0
PrivacyPublic = 1
PrivacyPrivate = 2
)
type DestinyVersion int
const (
VersionNone DestinyVersion = 0
VersionDestiny2 = 1
)
var (
ApiKey string
)
func init() {
ApiKey = ""
}
func get(endpoint string) (*Response, error) {
var request *http.Request
var err error
var response *http.Response
var parsedResponse *Response
request, err = setupClient("GET", endpoint, nil)
if err != nil { return nil, err }
response, err = http.DefaultClient.Do(request)
if (err != nil) { return nil, err }
parsedResponse, err = getResponse(response.Body)
if (err != nil) { return nil, err }
return parsedResponse, nil
}
func setupClient(method string, endpoint string, body io.Reader) (*http.Request, error) {
if ApiKey == "" { return nil, errors.New("No Api Key provided") }
request, err := http.NewRequest(method, apiBase + endpoint, body)
if (err == nil) {
request.Header.Add("X-API-Key", ApiKey)
}
return request, err
}
func getResponse(reader io.Reader) (*Response, error) {
decoder := json.NewDecoder(reader)
var response Response
err := decoder.Decode(&response)
if (err != nil) {
return nil, errors.New("Decoder Error: " + err.Error())
}
return &response, nil
}