-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsteamapi.go
50 lines (41 loc) · 1.25 KB
/
steamapi.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
// Package steamapi provides an interface to the
// Steam Web API methods.
package steamapi
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
)
// BaseSteamAPIURLProduction is the steam url used to do requests in prod
const BaseSteamAPIURLProduction = "https://api.steampowered.com"
// BaseSteamAPIURL is the url used to do requests, defaulted to prod
var BaseSteamAPIURL = BaseSteamAPIURLProduction
// A SteamMethod represents a Steam Web API method.
type SteamMethod string
// NewSteamMethod creates a new SteamMethod.
func NewSteamMethod(interf, method string, version int) SteamMethod {
m := fmt.Sprintf("%v/%v/%v/v%v/", BaseSteamAPIURL, interf, method, strconv.Itoa(version))
return SteamMethod(m)
}
// Request makes a request to the Steam Web API with the given
// url values and stores the result in v.
//
// Returns an error if the return status code was not 200.
func (s SteamMethod) Request(data url.Values, v interface{}) error {
url := string(s)
if data != nil {
url += "?" + data.Encode()
}
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("steamapi %s Status code %d", s, resp.StatusCode)
}
d := json.NewDecoder(resp.Body)
return d.Decode(&v)
}