updated EventsForMatch and MatchesForPlayer to use the new methods

This commit is contained in:
Thomas Bennett 2016-05-10 17:01:56 -07:00 committed by derek mcquay
parent 736272dfea
commit 5400089684
4 changed files with 47 additions and 22 deletions

View File

@ -23,29 +23,34 @@ func (h *Halo) metadataRequest(datatype, id string) ([]byte, error) {
}
q := url.Query()
url.RawQuery = q.Encode()
response := h.sendRequest(url.String())
response, _ := h.sendRequest(url.String())
return response, nil
}
func (h *Halo) sendRequest(url string) []byte {
func (h *Halo) sendRequest(url string) ([]byte, error) {
request, err := http.NewRequest("GET", url, nil)
checkErr(err)
if err != nil {
return nil, err
}
request.Header.Set("Ocp-Apim-Subscription-Key", h.apikey)
response, err := http.DefaultClient.Do(request)
checkErr(err)
if err != nil {
return nil, err
}
defer response.Body.Close()
// Return the URL of the image for SpartanImage and EmblemImage
if url != response.Request.URL.String() {
return []byte(response.Request.URL.String())
return []byte(response.Request.URL.String()), nil
}
// If its not SpartanImage or EmblemImage return the body
contents, err := ioutil.ReadAll(response.Body)
checkErr(err)
if err != nil {
return nil, err
}
return contents
return contents, nil
}
func sendRequest(url string) []byte {
@ -81,6 +86,6 @@ func verifyValidID(ID, name string) {
re, _ := regexp.Compile("^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$")
valid := re.MatchString(ID)
if valid == false {
fmt.Printf("%s is not a valid %s\n", ID, name)
log.Fatal("%s is not a valid %s", ID, name)
}
}

View File

@ -1,23 +1,38 @@
package halo
import (
"encoding/json"
"fmt"
"log"
"net/url"
)
func EventsForMatch(baseurl, title, matchid string) []byte {
func (h *Halo) EventsForMatch(matchid string) EventsForMatchStruct {
verifyValidID(matchid, "Match ID")
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/matches/%s/events", baseurl, title, matchid))
checkErr(err)
var j EventsForMatchStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/matches/%s/events", h.baseurl, h.title, matchid))
if err != nil {
log.Fatal("EventsForMatch URL Parse Failed: ", err)
}
q := url.Query()
url.RawQuery = q.Encode()
response := sendRequest(url.String())
return response
jsonObject, err := h.sendRequest(url.String())
if err != nil {
log.Fatal("EventsForMatch Failed: ", err)
}
err = json.Unmarshal(jsonObject, &j)
if err != nil {
log.Fatal("Failure to unmarshal json: ", err)
}
return j
}
func MatchesForPlayer(baseurl, title, player, modes string, start, count int) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/players/%s/matches", baseurl, title, player))
checkErr(err)
func (h *Halo) MatchesForPlayer(player, modes string, start, count int) MatchesForPlayerStruct {
var j MatchesForPlayerStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/players/%s/matches", h.baseurl, h.title, player))
if err != nil {
log.Fatal("MatchesForPlayer URL Parse Failed: ", err)
}
q := url.Query()
if modes != "" {
@ -30,8 +45,15 @@ func MatchesForPlayer(baseurl, title, player, modes string, start, count int) []
q.Set("count", string(count))
}
url.RawQuery = q.Encode()
response := sendRequest(url.String())
return response
jsonObject, err := h.sendRequest(url.String())
if err != nil {
log.Fatal("MatchesForPlayer Failed: ", err)
}
err = json.Unmarshal(jsonObject, &j)
if err != nil {
log.Fatal("Failure to unmarshal json: ", err)
}
return j
}
func PlayerLeaderboard(baseurl, title, seasonid, playlistid string, count int) []byte {

View File

@ -12,7 +12,6 @@ type CampaignMissionsStruct []struct {
ContentID string `json:"contentId"`
}
// This one works!
type CommendationsStruct []struct {
Type string `json:"type"`
Name string `json:"name"`
@ -59,7 +58,6 @@ type CommendationsStruct []struct {
ContentID string `json:"contentId"`
}
// This one works!
type CsrDesignationsStruct []struct {
Name string `json:"name"`
BannerImageURL string `json:"bannerImageUrl"`

View File

@ -54,7 +54,7 @@ func main() {
//fmt.Println(h.GameVariants(sampleGameVariantID))
//fmt.Println(h.MapVariants(sampleMapVariantsID))
//fmt.Println(h.Requisitions(sampleRequisitionID))
fmt.Println(h.RequisitionPacks(sampleRequisitionPacksID))
fmt.Println(h.MatchesForPlayer(sampleGamertag, "", 0, 0))
// Uncomment any of the below for sample output.
// Metadata