This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
71 lines (64 loc) · 1.42 KB
/
client.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
// Package eveapi implements access to EVE Onlines XML APi
package eveapi
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
"os"
)
const (
Tranquility = "https://api.eveonline.com"
Singularity = "https://api.testeveonline.com"
dateFormat = "2006-01-02 15:04:05"
defaultUserAgent = "Go API Wrapper"
)
type Key struct {
ID string
VCode string
}
type API struct {
Server string
APIKey Key
UserAgent string
Debug bool
}
func Simple(key Key) *API {
return &API{Tranquility, key, defaultUserAgent, false}
}
type APIResult struct {
Version int `xml:"version,attr"`
CurrentTime eveTime `xml:"currentTime"`
Error *APIError `xml:"error,omitempty"`
CachedUntil eveTime `xml:"cachedUntil"`
}
type APIError struct {
Code int `xml:"code,attr"`
Message string `xml:",chardata"`
}
func (e APIError) Error() string {
return fmt.Sprintf("Error! %v (code:%v)", e.Message, e.Code)
}
func (api API) Call(path string, args url.Values, output interface{}) error {
uri := api.Server + path
if args == nil {
args = url.Values{}
}
args.Set("keyID", api.APIKey.ID)
args.Set("vCode", api.APIKey.VCode)
resp, err := http.PostForm(uri, args)
if err != nil {
return err
}
defer resp.Body.Close()
if api.Debug {
io.Copy(os.Stdout, resp.Body)
}
//TODO: LimitReader if it explodes?
err = xml.NewDecoder(resp.Body).Decode(&output)
if err != nil {
return err
}
return nil
}