commit 4f42e1e21e807cb9cd8d8e9f72dfb939607e283f Author: Jordan Orelli Date: Fri Jul 31 14:41:50 2015 +0200 hey diff --git a/client.go b/client.go new file mode 100644 index 0000000..5dfe61e --- /dev/null +++ b/client.go @@ -0,0 +1,20 @@ +package steam + +import ( + "fmt" + "net/http" +) + +// a steam API client, not tied to any particular game +type Client struct { + key string +} + +func NewClient(key string) *Client { + return &Client{key: key} +} + +func (c *Client) Get(iface, method, version string) (*http.Response, error) { + url := fmt.Sprintf("https://api.steampowered.com/%s/%s/%s/?key=%s", iface, method, version, c.key) + return http.Get(url) +} diff --git a/cmd/steam/commands.go b/cmd/steam/commands.go new file mode 100644 index 0000000..2b8da3f --- /dev/null +++ b/cmd/steam/commands.go @@ -0,0 +1,27 @@ +package main + +import ( + "github.com/jordanorelli/steam" + "io" + "net/http" + "os" +) + +var commands = map[string]command{ + "api-list": command{ + handler: func(c *steam.Client) { + dump(c.Get("ISteamWebAPIUtil", "GetSupportedAPIList", "v0001")) + }, + }, +} + +type command struct { + handler func(*steam.Client) +} + +func dump(r *http.Response, e error) { + if e != nil { + bail(1, e.Error()) + } + io.Copy(os.Stdout, r.Body) +} diff --git a/cmd/steam/main.go b/cmd/steam/main.go new file mode 100644 index 0000000..72dce71 --- /dev/null +++ b/cmd/steam/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "github.com/jordanorelli/steam" + "io" + "os" +) + +func bail(code int, t string, args ...interface{}) { + var out io.Writer + if code == 0 { + out = os.Stdout + } else { + out = os.Stderr + } + fmt.Fprintf(out, t+"\n", args...) + os.Exit(code) +} + +func main() { + key := os.Getenv("STEAM_KEY") + if key == "" { + bail(1, "no steam key provided. use the environment variable STEAM_KEY to provide an api key") + } + + client := steam.NewClient(key) + if len(os.Args) < 2 { + bail(1, "supply a subcommand pl0x") + } + cmd, ok := commands[os.Args[1]] + if !ok { + bail(1, "no such subcommand %s", os.Args[1]) + } + cmd.handler(client) +}