Merge pull request #5 from dmmcquay/structs

Structs
This commit is contained in:
Thomas Bennett 2016-05-11 14:58:21 -07:00
commit b296f56e2a
5 changed files with 296 additions and 202 deletions

View File

@ -2,9 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"github.com/tbenz9/go-halo5-api/halo" "github.com/dmmcquay/go-halo5-api/halo"
) )
var baseurl string = "https://www.haloapi.com" var baseurl string = "https://www.haloapi.com"
@ -20,6 +21,7 @@ var sampleWarzoneMatchID string = "c35a35f8-f450-4836-a4c2-65100a7acb79"
var sampleSeasonID string = "b46c2095-4ca6-4f4b-a565-4702d7cfe586" //February 2016 Season var sampleSeasonID string = "b46c2095-4ca6-4f4b-a565-4702d7cfe586" //February 2016 Season
var samplePlaylistID string = "2323b76a-db98-4e03-aa37-e171cfbdd1a4" //SWAT gametype 2016 Season var samplePlaylistID string = "2323b76a-db98-4e03-aa37-e171cfbdd1a4" //SWAT gametype 2016 Season
var sampleGameVariantID string = "963ca478-369a-4a37-97e3-432fa13035e1" //Slayer var sampleGameVariantID string = "963ca478-369a-4a37-97e3-432fa13035e1" //Slayer
var badGameVariantID string = "9aaaaaaa-369a-4a37-97e3-432fa13035e1" //Slayer
var sampleMapVariantsID string = "a44373ee-9f63-4733-befd-5cd8fbb1b44a" //Truth var sampleMapVariantsID string = "a44373ee-9f63-4733-befd-5cd8fbb1b44a" //Truth
var sampleRequisitionPacksID string = "d10141cb-68a5-4c6b-af38-4e4935f973f7" var sampleRequisitionPacksID string = "d10141cb-68a5-4c6b-af38-4e4935f973f7"
var sampleRequisitionID string = "e4f549b2-90af-4dab-b2bc-11a46ea44103" var sampleRequisitionID string = "e4f549b2-90af-4dab-b2bc-11a46ea44103"
@ -36,6 +38,21 @@ func getAPIKey() string {
func main() { func main() {
h := halo.NewHalo(baseurl, title, getAPIKey()) h := halo.NewHalo(baseurl, title, getAPIKey())
a, err := h.EmblemImage("smoke721", 512)
if err != nil {
log.Fatal(err)
}
fmt.Println(a)
b, err := h.SpartanImage("smoke721", 512, "full")
if err != nil {
log.Fatal(err)
}
fmt.Println(b)
c, err := h.SpartanImage("thisplayerdoesnotexist", 512, "full")
if err != nil {
log.Fatal(err)
}
fmt.Println(c)
//fmt.Println(h.Enemies()) //fmt.Println(h.Enemies())
//fmt.Println(h.FlexibleStats()) //fmt.Println(h.FlexibleStats())
//fmt.Println(h.GameBaseVariants()) //fmt.Println(h.GameBaseVariants())
@ -56,7 +73,7 @@ func main() {
//fmt.Println(h.Requisitions(sampleRequisitionID)) //fmt.Println(h.Requisitions(sampleRequisitionID))
//fmt.Println(h.MatchesForPlayer(sampleGamertag, "", 0, 0)) //fmt.Println(h.MatchesForPlayer(sampleGamertag, "", 0, 0))
//fmt.Println(h.PlayerLeaderboard(sampleSeasonID, samplePlaylistID, 0)) //fmt.Println(h.PlayerLeaderboard(sampleSeasonID, samplePlaylistID, 0))
fmt.Println(h.CarnageReportArena(sampleArenaMatchID)) //fmt.Println(h.CarnageReportArena(sampleArenaMatchID))
// Uncomment any of the below for sample output. // Uncomment any of the below for sample output.
// Metadata // Metadata

View File

@ -3,19 +3,11 @@ package halo
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"net/url" "net/url"
"os"
"regexp" "regexp"
) )
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func (h *Halo) metadataRequest(datatype, id string) ([]byte, error) { func (h *Halo) metadataRequest(datatype, id string) ([]byte, error) {
url, err := url.Parse(fmt.Sprintf("%s/metadata/%s/metadata/%s/%s", h.baseurl, h.title, datatype, id)) url, err := url.Parse(fmt.Sprintf("%s/metadata/%s/metadata/%s/%s", h.baseurl, h.title, datatype, id))
if err != nil { if err != nil {
@ -23,7 +15,10 @@ func (h *Halo) metadataRequest(datatype, id string) ([]byte, error) {
} }
q := url.Query() q := url.Query()
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
response, _ := h.sendRequest(url.String()) response, err := h.sendRequest(url.String())
if err != nil {
return nil, err
}
return response, nil return response, nil
} }
@ -39,6 +34,11 @@ func (h *Halo) sendRequest(url string) ([]byte, error) {
} }
defer response.Body.Close() defer response.Body.Close()
//check for response code
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf(response.Status)
}
// Return the URL of the image for SpartanImage and EmblemImage // Return the URL of the image for SpartanImage and EmblemImage
if url != response.Request.URL.String() { if url != response.Request.URL.String() {
return []byte(response.Request.URL.String()), nil return []byte(response.Request.URL.String()), nil
@ -53,39 +53,10 @@ func (h *Halo) sendRequest(url string) ([]byte, error) {
return contents, nil return contents, nil
} }
func sendRequest(url string) []byte { func verifyValidID(id string) error {
request, err := http.NewRequest("GET", url, nil)
checkErr(err)
request.Header.Set("Ocp-Apim-Subscription-Key", getAPIKey())
response, err := http.DefaultClient.Do(request)
checkErr(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())
}
// If its not SpartanImage or EmblemImage return the body
contents, err := ioutil.ReadAll(response.Body)
checkErr(err)
return contents
}
func getAPIKey() string {
apikey := os.Getenv("HALO_API_KEY")
if len(apikey) != 32 {
fmt.Println("Invalid API Key")
}
return apikey
}
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}$") 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 !re.MatchString(id) {
if valid == false { return fmt.Errorf("Not a Valid id: ", id)
log.Fatal("%s is not a valid %s", ID, name)
} }
return nil
} }

View File

@ -3,266 +3,324 @@ package halo
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
) )
func (h *Halo) CampaignMissions() CampaignMissionsStruct { func (h *Halo) CampaignMissions() (CampaignMissionsStruct, error) {
var j CampaignMissionsStruct var j CampaignMissionsStruct
jsonObject, err := h.metadataRequest("campaign-missions", "") jsonObject, err := h.metadataRequest("campaign-missions", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return CampaignMissionsStruct{}, fmt.Errorf(
"MetadataRequest campaign-missions Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return CampaignMissionsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) Commendations() CommendationsStruct { func (h *Halo) Commendations() (CommendationsStruct, error) {
var j CommendationsStruct var j CommendationsStruct
jsonObject, err := h.metadataRequest("commendations", "") jsonObject, err := h.metadataRequest("commendations", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return CommendationsStruct{}, fmt.Errorf(
"MetadataRequest commendations Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return CommendationsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) CsrDesignations() CsrDesignationsStruct { func (h *Halo) CsrDesignations() (CsrDesignationsStruct, error) {
var j CsrDesignationsStruct var j CsrDesignationsStruct
jsonObject, err := h.metadataRequest("csr-designations", "") jsonObject, err := h.metadataRequest("csr-designations", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return CsrDesignationsStruct{}, fmt.Errorf(
"MetadataRequest csr-designations Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return CsrDesignationsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) Enemies() EnemiesStruct { func (h *Halo) Enemies() (EnemiesStruct, error) {
var j EnemiesStruct var j EnemiesStruct
jsonObject, err := h.metadataRequest("enemies", "") jsonObject, err := h.metadataRequest("enemies", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return EnemiesStruct{}, fmt.Errorf(
"MetadataRequest enemies Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return EnemiesStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) FlexibleStats() FlexibleStatsStruct { func (h *Halo) FlexibleStats() (FlexibleStatsStruct, error) {
var j FlexibleStatsStruct var j FlexibleStatsStruct
jsonObject, err := h.metadataRequest("flexible-stats", "") jsonObject, err := h.metadataRequest("flexible-stats", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return FlexibleStatsStruct{}, fmt.Errorf(
"MetadataRequest flexible-stats Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
fmt.Println("hjere") return FlexibleStatsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
log.Fatal("Failure to unmarshal json: ", err)
} }
return j return j, nil
} }
func (h *Halo) GameBaseVariants() GameBaseVariantsStruct { func (h *Halo) GameBaseVariants() (GameBaseVariantsStruct, error) {
var j GameBaseVariantsStruct var j GameBaseVariantsStruct
jsonObject, err := h.metadataRequest("game-base-variants", "") jsonObject, err := h.metadataRequest("game-base-variants", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return GameBaseVariantsStruct{}, fmt.Errorf(
"MetadataRequest game-base-variants Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return GameBaseVariantsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) GameVariants(id string) GameVariantsStruct { func (h *Halo) GameVariants(id string) (GameVariantsStruct, error) {
var j GameVariantsStruct var j GameVariantsStruct
jsonObject, err := h.metadataRequest("game-variants", id) jsonObject, err := h.metadataRequest("game-variants", id)
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return GameVariantsStruct{}, fmt.Errorf(
"MetadataRequest game-variants Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return GameVariantsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) Impulses() ImpulsesStruct { func (h *Halo) Impulses() (ImpulsesStruct, error) {
var j ImpulsesStruct var j ImpulsesStruct
jsonObject, err := h.metadataRequest("impulses", "") jsonObject, err := h.metadataRequest("impulses", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return ImpulsesStruct{}, fmt.Errorf(
"MetadataRequest impulses Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return ImpulsesStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) MapVariants(id string) MapVariantsStruct { func (h *Halo) MapVariants(id string) (MapVariantsStruct, error) {
var j MapVariantsStruct var j MapVariantsStruct
jsonObject, err := h.metadataRequest("map-variants", id) jsonObject, err := h.metadataRequest("map-variants", id)
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return MapVariantsStruct{}, fmt.Errorf(
"MetadataRequest map-variants Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return MapVariantsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) Maps() MapsStruct { func (h *Halo) Maps() (MapsStruct, error) {
var j MapsStruct var j MapsStruct
jsonObject, err := h.metadataRequest("maps", "") jsonObject, err := h.metadataRequest("maps", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return MapsStruct{}, fmt.Errorf(
"MetadataRequest maps Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return MapsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) Medals() MedalsStruct { func (h *Halo) Medals() (MedalsStruct, error) {
var j MedalsStruct var j MedalsStruct
jsonObject, err := h.metadataRequest("medals", "") jsonObject, err := h.metadataRequest("medals", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return MedalsStruct{}, fmt.Errorf(
"MetadataRequest medals Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return MedalsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) Playlists() PlaylistsStruct { func (h *Halo) Playlists() (PlaylistsStruct, error) {
var j PlaylistsStruct var j PlaylistsStruct
jsonObject, err := h.metadataRequest("playlists", "") jsonObject, err := h.metadataRequest("playlists", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return PlaylistsStruct{}, fmt.Errorf(
"MetadataRequest playlists Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return PlaylistsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) RequisitionPacks(id string) RequisitionPacksStruct { func (h *Halo) RequisitionPacks(id string) (RequisitionPacksStruct, error) {
var j RequisitionPacksStruct var j RequisitionPacksStruct
jsonObject, err := h.metadataRequest("requisition-packs", id) jsonObject, err := h.metadataRequest("requisition-packs", id)
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return RequisitionPacksStruct{}, fmt.Errorf(
"MetadataRequest requisition-packs Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return RequisitionPacksStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) Requisitions(id string) RequisitionsStruct { func (h *Halo) Requisitions(id string) (RequisitionsStruct, error) {
var j RequisitionsStruct var j RequisitionsStruct
jsonObject, err := h.metadataRequest("requisitions", id) jsonObject, err := h.metadataRequest("requisitions", id)
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return RequisitionsStruct{}, fmt.Errorf(
"MetadataRequest requisitions Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return RequisitionsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) Seasons() SeasonsStruct { func (h *Halo) Seasons() (SeasonsStruct, error) {
var j SeasonsStruct var j SeasonsStruct
jsonObject, err := h.metadataRequest("seasons", "") jsonObject, err := h.metadataRequest("seasons", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return SeasonsStruct{}, fmt.Errorf(
"MetadataRequest seasons Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return SeasonsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) Skulls() SkullsStruct { func (h *Halo) Skulls() (SkullsStruct, error) {
var j SkullsStruct var j SkullsStruct
jsonObject, err := h.metadataRequest("skulls", "") jsonObject, err := h.metadataRequest("skulls", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return SkullsStruct{}, fmt.Errorf(
"MetadataRequest skulls Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return SkullsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) SpartanRanks() SpartanRanksStruct { func (h *Halo) SpartanRanks() (SpartanRanksStruct, error) {
var j SpartanRanksStruct var j SpartanRanksStruct
jsonObject, err := h.metadataRequest("spartan-ranks", "") jsonObject, err := h.metadataRequest("spartan-ranks", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return SpartanRanksStruct{}, fmt.Errorf(
"MetadataRequest spartan-ranks Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return SpartanRanksStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) TeamColors() TeamColorsStruct { func (h *Halo) TeamColors() (TeamColorsStruct, error) {
var j TeamColorsStruct var j TeamColorsStruct
jsonObject, err := h.metadataRequest("team-colors", "") jsonObject, err := h.metadataRequest("team-colors", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return TeamColorsStruct{}, fmt.Errorf(
"MetadataRequest team-colors Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return TeamColorsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) Vehicles() VehiclesStruct { func (h *Halo) Vehicles() (VehiclesStruct, error) {
var j VehiclesStruct var j VehiclesStruct
jsonObject, err := h.metadataRequest("vehicles", "") jsonObject, err := h.metadataRequest("vehicles", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return VehiclesStruct{}, fmt.Errorf(
"MetadataRequest vehicles Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return VehiclesStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) Weapons() WeaponsStruct { func (h *Halo) Weapons() (WeaponsStruct, error) {
var j WeaponsStruct var j WeaponsStruct
jsonObject, err := h.metadataRequest("weapons", "") jsonObject, err := h.metadataRequest("weapons", "")
if err != nil { if err != nil {
log.Fatal("MetadataRequest Failed: ", err) return WeaponsStruct{}, fmt.Errorf(
"MetadataRequest weapons Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return WeaponsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }

View File

@ -3,32 +3,48 @@ package halo
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"strconv"
"strings" "strings"
) )
func EmblemImage(baseurl, title, player string, size int) []byte { func (h *Halo) EmblemImage(player string, size int) (string, error) {
url, err := url.Parse(fmt.Sprintf("%s/profile/%s/profiles/%s", baseurl, title, player)) url, err := url.Parse(
checkErr(err) fmt.Sprintf("%s/profile/%s/profiles/%s/emblem",
h.baseurl,
h.title,
player,
))
if err != nil {
return "", err
}
q := url.Query() q := url.Query()
if (size == 95) || (size == 128) || (size == 190) || (size == 256) || (size == 512) { if (size == 95) || (size == 128) || (size == 190) || (size == 256) || (size == 512) {
q.Set("size", string(size)) q.Set("size", strconv.Itoa(size))
} }
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
response := sendRequest(url.String()) response, err := h.sendRequest(url.String())
return response if err != nil {
return "", err
}
return string(response), nil
} }
func SpartanImage(baseurl, title, player string, size int, crop string) []byte { func (h *Halo) SpartanImage(player string, size int, crop string) (string, error) {
url, err := url.Parse(fmt.Sprintf("%s/profile/%s/profiles/%s/spartan", baseurl, title, player)) url, err := url.Parse(fmt.Sprintf("%s/profile/%s/profiles/%s/spartan", h.baseurl, h.title, player))
checkErr(err) if err != nil {
return "", err
}
q := url.Query() q := url.Query()
if (size == 95) || (size == 128) || (size == 190) || (size == 256) || (size == 512) { if (size == 95) || (size == 128) || (size == 190) || (size == 256) || (size == 512) {
q.Set("size", string(size)) q.Set("size", strconv.Itoa(size))
} }
if (strings.ToLower(crop) == "full") || (strings.ToLower(crop) == "portrait") { if (strings.ToLower(crop) == "full") || (strings.ToLower(crop) == "portrait") {
q.Set("crop", crop) q.Set("crop", crop)
} }
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
response := sendRequest(url.String()) response, err := h.sendRequest(url.String())
return response if err != nil {
return "", err
}
return string(response), nil
} }

View File

@ -3,35 +3,37 @@ package halo
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/url" "net/url"
) )
func (h *Halo) EventsForMatch(matchid string) EventsForMatchStruct { func (h *Halo) EventsForMatch(matchid string) (EventsForMatchStruct, error) {
verifyValidID(matchid, "Match ID") err := verifyValidID(matchid)
var j EventsForMatchStruct var j EventsForMatchStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/matches/%s/events", h.baseurl, h.title, matchid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/matches/%s/events", h.baseurl, h.title, matchid))
if err != nil { if err != nil {
log.Fatal("EventsForMatch URL Parse Failed: ", err) return EventsForMatchStruct{}, err
} }
q := url.Query() q := url.Query()
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
jsonObject, err := h.sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
if err != nil { if err != nil {
log.Fatal("EventsForMatch Failed: ", err) return EventsForMatchStruct{}, fmt.Errorf(
"EventsForMatch request Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return EventsForMatchStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) MatchesForPlayer(player, modes string, start, count int) MatchesForPlayerStruct { func (h *Halo) MatchesForPlayer(player, modes string, start, count int) (MatchesForPlayerStruct, error) {
var j MatchesForPlayerStruct var j MatchesForPlayerStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/players/%s/matches", h.baseurl, h.title, player)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/players/%s/matches", h.baseurl, h.title, player))
if err != nil { if err != nil {
log.Fatal("MatchesForPlayer URL Parse Failed: ", err) return MatchesForPlayerStruct{}, err
} }
q := url.Query() q := url.Query()
@ -47,22 +49,25 @@ func (h *Halo) MatchesForPlayer(player, modes string, start, count int) MatchesF
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
jsonObject, err := h.sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
if err != nil { if err != nil {
log.Fatal("MatchesForPlayer Failed: ", err) return MatchesForPlayerStruct{}, fmt.Errorf(
"MatchesForPlayer request Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return MatchesForPlayerStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) PlayerLeaderboard(seasonid, playlistid string, count int) PlayerLeaderboardStruct { func (h *Halo) PlayerLeaderboard(seasonid, playlistid string, count int) (PlayerLeaderboardStruct, error) {
var j PlayerLeaderboardStruct var j PlayerLeaderboardStruct
verifyValidID(playlistid, "Playlist ID") err := verifyValidID(playlistid)
verifyValidID(seasonid, "Season ID") err = verifyValidID(seasonid)
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/player-leaderboards/csr/%s/%s", h.baseurl, h.title, seasonid, playlistid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/player-leaderboards/csr/%s/%s", h.baseurl, h.title, seasonid, playlistid))
if err != nil { if err != nil {
log.Fatal("PlayerLeaderboard URL Parse Failed: ", err) return PlayerLeaderboardStruct{}, err
} }
q := url.Query() q := url.Query()
@ -72,101 +77,116 @@ func (h *Halo) PlayerLeaderboard(seasonid, playlistid string, count int) PlayerL
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
jsonObject, err := h.sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
if err != nil { if err != nil {
log.Fatal("PlayerLeaderboard Failed: ", err) return PlayerLeaderboardStruct{}, fmt.Errorf(
"PlayerLeaderboard request Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return PlayerLeaderboardStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) CarnageReportArena(matchid string) CarnageReportArenaStruct { func (h *Halo) CarnageReportArena(matchid string) (CarnageReportArenaStruct, error) {
var j CarnageReportArenaStruct var j CarnageReportArenaStruct
verifyValidID(matchid, "Match ID") err := verifyValidID(matchid)
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/arena/matches/%s", h.baseurl, h.title, matchid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/arena/matches/%s", h.baseurl, h.title, matchid))
if err != nil { if err != nil {
log.Fatal("CarnageReportArena URL Parse Failed: ", err) return CarnageReportArenaStruct{}, err
} }
q := url.Query() q := url.Query()
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
jsonObject, err := h.sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
if err != nil { if err != nil {
log.Fatal("CarnageReportArena Failed: ", err) return CarnageReportArenaStruct{}, fmt.Errorf(
"CarnageReportArena request Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return CarnageReportArenaStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) CarnageReportCampaign(matchid string) CarnageReportCampaignStruct { func (h *Halo) CarnageReportCampaign(matchid string) (CarnageReportCampaignStruct, error) {
var j CarnageReportCampaignStruct var j CarnageReportCampaignStruct
verifyValidID(matchid, "Match ID") err := verifyValidID(matchid)
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/campaign/matches/%s", h.baseurl, h.title, matchid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/campaign/matches/%s", h.baseurl, h.title, matchid))
if err != nil { if err != nil {
log.Fatal("CarnageReportCampaign URL Parse Failed: ", err) return CarnageReportCampaignStruct{}, err
} }
q := url.Query() q := url.Query()
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
jsonObject, err := h.sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
if err != nil { if err != nil {
log.Fatal("CarnageReportCampaign Failed: ", err) return CarnageReportCampaignStruct{}, fmt.Errorf(
"CarnageReportCampaign request Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return CarnageReportCampaignStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) CarnageReportCustom(matchid string) CarnageReportCustomStruct { func (h *Halo) CarnageReportCustom(matchid string) (CarnageReportCustomStruct, error) {
var j CarnageReportCustomStruct var j CarnageReportCustomStruct
verifyValidID(matchid, "Match ID") err := verifyValidID(matchid)
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/custom/matches/%s", h.baseurl, h.title, matchid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/custom/matches/%s", h.baseurl, h.title, matchid))
if err != nil { if err != nil {
log.Fatal("CarnageReportCustom URL Parse Failed: ", err) return CarnageReportCustomStruct{}, err
} }
q := url.Query() q := url.Query()
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
jsonObject, err := h.sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
if err != nil { if err != nil {
log.Fatal("CarnageReportCustom Failed: ", err) return CarnageReportCustomStruct{}, fmt.Errorf(
"CarnageReportCustom request Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return CarnageReportCustomStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) CarnageReportWarzone(matchid string) CarnageReportWarzoneStruct { func (h *Halo) CarnageReportWarzone(matchid string) (CarnageReportWarzoneStruct, error) {
var j CarnageReportWarzoneStruct var j CarnageReportWarzoneStruct
verifyValidID(matchid, "Match ID") err := verifyValidID(matchid)
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/warzone/matches/%s", h.baseurl, h.title, matchid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/warzone/matches/%s", h.baseurl, h.title, matchid))
if err != nil { if err != nil {
log.Fatal("CarnageReportWarzone URL Parse Failed: ", err) return CarnageReportWarzoneStruct{}, err
} }
q := url.Query() q := url.Query()
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
jsonObject, err := h.sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
if err != nil { if err != nil {
log.Fatal("CarnageReportWarzone Failed: ", err) return CarnageReportWarzoneStruct{}, fmt.Errorf(
"CarnageReportWarzone request Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return CarnageReportWarzoneStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) ServiceRecordArena(players, seasonid string) ServiceRecordArenaStruct { func (h *Halo) ServiceRecordArena(players, seasonid string) (ServiceRecordArenaStruct, error) {
var j ServiceRecordArenaStruct var j ServiceRecordArenaStruct
verifyValidID(seasonid, "Season ID") err := verifyValidID(seasonid)
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/arena", h.baseurl, h.title)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/arena", h.baseurl, h.title))
if err != nil { if err != nil {
log.Fatal("CarnageReportWarzone URL Parse Failed: ", err) return ServiceRecordArenaStruct{}, err
} }
q := url.Query() q := url.Query()
q.Set("players", players) q.Set("players", players)
@ -176,71 +196,83 @@ func (h *Halo) ServiceRecordArena(players, seasonid string) ServiceRecordArenaSt
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
jsonObject, err := h.sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
if err != nil { if err != nil {
log.Fatal("ServiceRecordArena Failed: ", err) return ServiceRecordArenaStruct{}, fmt.Errorf(
"ServiceRecordArena request Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return ServiceRecordArenaStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) ServiceRecordCampaign(players string) ServiceRecordCampaignStruct { func (h *Halo) ServiceRecordCampaign(players string) (ServiceRecordCampaignStruct, error) {
var j ServiceRecordCampaignStruct var j ServiceRecordCampaignStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/campaign", h.baseurl, h.title)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/campaign", h.baseurl, h.title))
if err != nil { if err != nil {
log.Fatal("ServiceRecordCampaign URL Parse Failed: ", err) return ServiceRecordCampaignStruct{}, err
} }
q := url.Query() q := url.Query()
q.Set("players", players) q.Set("players", players)
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
jsonObject, err := h.sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
if err != nil { if err != nil {
log.Fatal("ServiceRecordCampaign Failed: ", err) return ServiceRecordCampaignStruct{}, fmt.Errorf(
"ServiceRecordCampaign request Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return ServiceRecordCampaignStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) ServiceRecordCustom(players string) ServiceRecordCustomStruct { func (h *Halo) ServiceRecordCustom(players string) (ServiceRecordCustomStruct, error) {
var j ServiceRecordCustomStruct var j ServiceRecordCustomStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/custom", h.baseurl, h.title)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/custom", h.baseurl, h.title))
if err != nil { if err != nil {
log.Fatal("ServiceRecordCampaign URL Parse Failed: ", err) return ServiceRecordCustomStruct{}, err
} }
q := url.Query() q := url.Query()
q.Set("players", players) q.Set("players", players)
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
jsonObject, err := h.sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
if err != nil { if err != nil {
log.Fatal("ServiceRecordCustom Failed: ", err) return ServiceRecordCustomStruct{}, fmt.Errorf(
"ServiceRecordCustom request Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return ServiceRecordCustomStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }
func (h *Halo) ServiceRecordWarzone(players string) ServiceRecordWarzoneStruct { func (h *Halo) ServiceRecordWarzone(players string) (ServiceRecordWarzoneStruct, error) {
var j ServiceRecordWarzoneStruct var j ServiceRecordWarzoneStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/warzone", h.baseurl, h.title)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/warzone", h.baseurl, h.title))
if err != nil { if err != nil {
log.Fatal("ServiceRecordCampaign URL Parse Failed: ", err) return ServiceRecordWarzoneStruct{}, err
} }
q := url.Query() q := url.Query()
q.Set("players", players) q.Set("players", players)
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
jsonObject, err := h.sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
if err != nil { if err != nil {
log.Fatal("ServiceRecordWarzone Failed: ", err) return ServiceRecordWarzoneStruct{}, fmt.Errorf(
"ServiceRecordWarzone request Failed: %v",
err,
)
} }
err = json.Unmarshal(jsonObject, &j) err = json.Unmarshal(jsonObject, &j)
if err != nil { if err != nil {
log.Fatal("Failure to unmarshal json: ", err) return ServiceRecordWarzoneStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
} }
return j return j, nil
} }