-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
executable file
·151 lines (131 loc) · 3.38 KB
/
api.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
package golink
import (
"bytes"
"code.google.com/p/go-etree"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"sort"
"time"
)
type APIFetcher interface {
Get(path string, params url.Values, c *APICredentials) (etree.Element, error)
}
type API struct {
BaseURL string
Cache APICache
lastCachedUntil, lastCurrentTime time.Time
Client URLFetcher
}
type CredentialedAPI struct {
api APIFetcher
credentials APICredentials
}
// Returns a new API object, filling in defaults as needed.
func NewAPI(base string, c APICache, uf URLFetcher) *API {
if base == "" {
base = "api.eveonline.com"
}
if c == nil {
c = make(InMemoryAPICache)
}
if uf == nil {
uf = http.PostForm
}
return &API{BaseURL: base, Cache: c, Client: uf}
}
func NewCredentialedAPI(a APIFetcher, c APICredentials) *CredentialedAPI {
return &CredentialedAPI{api: a, credentials: c}
}
func (a *CredentialedAPI) Get(path string, params url.Values) (etree.Element, error) {
return a.api.Get(path, params, &a.credentials)
}
type APICredentials struct {
KeyID, VCode string
}
type URLFetcher func(path string, params url.Values) (result *http.Response, err error)
type APICache interface {
Get(k string) []byte
Put(k string, v []byte, duration time.Duration)
}
type InMemoryCacheValue struct {
V []byte
Expiration time.Time
}
type InMemoryAPICache map[string]*InMemoryCacheValue
func (c InMemoryAPICache) Get(k string) []byte {
r := c[k]
if r == nil {
return nil
}
if time.Now().After(r.Expiration) {
c[k] = nil
return nil
}
return r.V
}
func (c InMemoryAPICache) Put(k string, v []byte, duration time.Duration) {
c[k] = &InMemoryCacheValue{V: v, Expiration: time.Now().Add(duration)}
}
//Request a specific path from the EVE API.
func (a *API) Get(path string, params url.Values, c *APICredentials) (etree.Element, error) {
if c != nil {
params["keyID"] = []string{c.KeyID}
params["vCode"] = []string{c.VCode}
}
cacheKey := genCacheKey(path, params)
response := a.Cache.Get(cacheKey)
cached := response != nil
if !cached {
r, err := a.Client(fmt.Sprintf("https://%v/%v.xml.aspx", a.BaseURL, path), params)
if err != nil {
return nil, err
}
defer r.Body.Close()
response, err = ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
}
tree, err := etree.Parse(bytes.NewBuffer(response))
if err != nil {
return nil, err
}
tree = tree.Find("eveapi")
elem := tree.Find("currentTime")
if elem == nil {
return nil, fmt.Errorf("Unable to parse currentTime.")
}
currentTime, err := parseEveTs(elem.Text())
if err != nil {
return nil, err
}
a.lastCurrentTime = currentTime
elem = tree.Find("cachedUntil")
if elem == nil {
return nil, fmt.Errorf("Unable to parse cachedUntil.")
}
expiresTime, err := parseEveTs(elem.Text())
if err != nil {
return nil, err
}
a.lastCachedUntil = expiresTime
if !cached {
a.Cache.Put(cacheKey, response, expiresTime.Sub(currentTime))
}
xmlErr := tree.Find("error")
if xmlErr != nil {
code, _ := xmlErr.Get("code")
return nil, fmt.Errorf("API reported error with code %v and message \"%v\"", code, xmlErr.Text())
}
return tree.Find("result"), nil
}
func genCacheKey(path string, params url.Values) string {
ks := make([]string, 0)
for k, v := range params {
ks = append(ks, fmt.Sprint(k, v))
}
sort.StringSlice(ks).Sort()
return fmt.Sprintf("%v#%v", path, ks)
}