Skip to content

Instantly share code, notes, and snippets.

@1lann
Created August 9, 2015 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1lann/a3539c1669750bf2eba0 to your computer and use it in GitHub Desktop.
Save 1lann/a3539c1669750bf2eba0 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type MasteryPageContainer struct {
Pages []MasteryPage `"json:pages"`
}
type MasteryPage struct {
Masteries []Mastery `"json:masteries"`
Id int64 `"json:id"`
Name string `"json:name"`
Current bool `"json:current"`
}
type Mastery struct {
Id int `"json:id"`
Rank int `"json:rank"`
}
const region = "oce"
const summonerId = "3190144"
const apiKey = "your key here"
func main() {
// Make the HTTP get request
resp, err := http.Get("https://" + region + ".api.pvp.net/api/lol/" + region +
"/v1.4/summoner/" + summonerId + "/masteries?api_key=" + apiKey)
if err != nil {
// You should handle the error here
panic(err)
}
// Close the body reader when finished
defer resp.Body.Close()
// Handle non 200 status codes
if resp.StatusCode != 200 {
fmt.Println("Status code is not 200!")
fmt.Println("Instead it is: " + resp.Status)
return
}
// Read all the contents
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
// You should handle the error here
panic(err)
}
// Store the results in a map as you will need to
// resolve the summoner id index
results := make(map[string]MasteryPageContainer)
// Unmarshal (decode) the JSON
err = json.Unmarshal(contents, &results)
if err != nil {
// You should handle the error here
panic(err)
}
pageContainer, found := results[summonerId]
// Check that the value at the index exists
if !found {
fmt.Println("Could not find mastery pages of summoner from response!")
return
}
// Print out all the names of the mastery pages
fmt.Println("Here's a list of mastery pages for " + summonerId + ":")
for _, mastery := range pageContainer.Pages {
fmt.Println("- " + mastery.Name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment