commit
b296f56e2a
@ -2,9 +2,10 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/tbenz9/go-halo5-api/halo"
|
||||
"github.com/dmmcquay/go-halo5-api/halo"
|
||||
)
|
||||
|
||||
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 samplePlaylistID string = "2323b76a-db98-4e03-aa37-e171cfbdd1a4" //SWAT gametype 2016 Season
|
||||
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 sampleRequisitionPacksID string = "d10141cb-68a5-4c6b-af38-4e4935f973f7"
|
||||
var sampleRequisitionID string = "e4f549b2-90af-4dab-b2bc-11a46ea44103"
|
||||
@ -36,6 +38,21 @@ func getAPIKey() string {
|
||||
func main() {
|
||||
|
||||
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.FlexibleStats())
|
||||
//fmt.Println(h.GameBaseVariants())
|
||||
@ -56,7 +73,7 @@ func main() {
|
||||
//fmt.Println(h.Requisitions(sampleRequisitionID))
|
||||
//fmt.Println(h.MatchesForPlayer(sampleGamertag, "", 0, 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.
|
||||
|
||||
// Metadata
|
@ -3,19 +3,11 @@ package halo
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func checkErr(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
if err != nil {
|
||||
@ -23,7 +15,10 @@ func (h *Halo) metadataRequest(datatype, id string) ([]byte, error) {
|
||||
}
|
||||
q := url.Query()
|
||||
url.RawQuery = q.Encode()
|
||||
response, _ := h.sendRequest(url.String())
|
||||
response, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
@ -39,6 +34,11 @@ func (h *Halo) sendRequest(url string) ([]byte, error) {
|
||||
}
|
||||
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
|
||||
if url != response.Request.URL.String() {
|
||||
return []byte(response.Request.URL.String()), nil
|
||||
@ -53,39 +53,10 @@ func (h *Halo) sendRequest(url string) ([]byte, error) {
|
||||
return contents, nil
|
||||
}
|
||||
|
||||
func sendRequest(url string) []byte {
|
||||
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) {
|
||||
func verifyValidID(id string) error {
|
||||
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 {
|
||||
log.Fatal("%s is not a valid %s", ID, name)
|
||||
if !re.MatchString(id) {
|
||||
return fmt.Errorf("Not a Valid id: ", id)
|
||||
}
|
||||
return nil
|
||||
}
|
222
halo/metadata.go
222
halo/metadata.go
@ -3,266 +3,324 @@ package halo
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func (h *Halo) CampaignMissions() CampaignMissionsStruct {
|
||||
func (h *Halo) CampaignMissions() (CampaignMissionsStruct, error) {
|
||||
var j CampaignMissionsStruct
|
||||
jsonObject, err := h.metadataRequest("campaign-missions", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return CampaignMissionsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest campaign-missions Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("commendations", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return CommendationsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest commendations Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("csr-designations", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return CsrDesignationsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest csr-designations Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("enemies", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return EnemiesStruct{}, fmt.Errorf(
|
||||
"MetadataRequest enemies Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("flexible-stats", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return FlexibleStatsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest flexible-stats Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
if err != nil {
|
||||
fmt.Println("hjere")
|
||||
log.Fatal("Failure to unmarshal json: ", err)
|
||||
return FlexibleStatsStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
|
||||
}
|
||||
return j
|
||||
return j, nil
|
||||
}
|
||||
|
||||
func (h *Halo) GameBaseVariants() GameBaseVariantsStruct {
|
||||
func (h *Halo) GameBaseVariants() (GameBaseVariantsStruct, error) {
|
||||
var j GameBaseVariantsStruct
|
||||
jsonObject, err := h.metadataRequest("game-base-variants", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return GameBaseVariantsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest game-base-variants Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("game-variants", id)
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return GameVariantsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest game-variants Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("impulses", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return ImpulsesStruct{}, fmt.Errorf(
|
||||
"MetadataRequest impulses Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("map-variants", id)
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return MapVariantsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest map-variants Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("maps", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return MapsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest maps Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("medals", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return MedalsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest medals Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("playlists", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return PlaylistsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest playlists Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("requisition-packs", id)
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return RequisitionPacksStruct{}, fmt.Errorf(
|
||||
"MetadataRequest requisition-packs Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("requisitions", id)
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return RequisitionsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest requisitions Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("seasons", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return SeasonsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest seasons Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("skulls", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return SkullsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest skulls Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("spartan-ranks", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return SpartanRanksStruct{}, fmt.Errorf(
|
||||
"MetadataRequest spartan-ranks Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("team-colors", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return TeamColorsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest team-colors Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("vehicles", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return VehiclesStruct{}, fmt.Errorf(
|
||||
"MetadataRequest vehicles Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
jsonObject, err := h.metadataRequest("weapons", "")
|
||||
if err != nil {
|
||||
log.Fatal("MetadataRequest Failed: ", err)
|
||||
return WeaponsStruct{}, fmt.Errorf(
|
||||
"MetadataRequest weapons Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
}
|
||||
|
@ -3,32 +3,48 @@ package halo
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func EmblemImage(baseurl, title, player string, size int) []byte {
|
||||
url, err := url.Parse(fmt.Sprintf("%s/profile/%s/profiles/%s", baseurl, title, player))
|
||||
checkErr(err)
|
||||
func (h *Halo) EmblemImage(player string, size int) (string, error) {
|
||||
url, err := url.Parse(
|
||||
fmt.Sprintf("%s/profile/%s/profiles/%s/emblem",
|
||||
h.baseurl,
|
||||
h.title,
|
||||
player,
|
||||
))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
q := url.Query()
|
||||
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()
|
||||
response := sendRequest(url.String())
|
||||
return response
|
||||
response, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(response), nil
|
||||
}
|
||||
|
||||
func SpartanImage(baseurl, title, player string, size int, crop string) []byte {
|
||||
url, err := url.Parse(fmt.Sprintf("%s/profile/%s/profiles/%s/spartan", baseurl, title, player))
|
||||
checkErr(err)
|
||||
func (h *Halo) SpartanImage(player string, size int, crop string) (string, error) {
|
||||
url, err := url.Parse(fmt.Sprintf("%s/profile/%s/profiles/%s/spartan", h.baseurl, h.title, player))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
q := url.Query()
|
||||
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") {
|
||||
q.Set("crop", crop)
|
||||
}
|
||||
url.RawQuery = q.Encode()
|
||||
response := sendRequest(url.String())
|
||||
return response
|
||||
response, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(response), nil
|
||||
}
|
||||
|
160
halo/stats.go
160
halo/stats.go
@ -3,35 +3,37 @@ package halo
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (h *Halo) EventsForMatch(matchid string) EventsForMatchStruct {
|
||||
verifyValidID(matchid, "Match ID")
|
||||
func (h *Halo) EventsForMatch(matchid string) (EventsForMatchStruct, error) {
|
||||
err := verifyValidID(matchid)
|
||||
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)
|
||||
return EventsForMatchStruct{}, err
|
||||
}
|
||||
q := url.Query()
|
||||
url.RawQuery = q.Encode()
|
||||
jsonObject, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
log.Fatal("EventsForMatch Failed: ", err)
|
||||
return EventsForMatchStruct{}, fmt.Errorf(
|
||||
"EventsForMatch request Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
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)
|
||||
return MatchesForPlayerStruct{}, err
|
||||
}
|
||||
q := url.Query()
|
||||
|
||||
@ -47,22 +49,25 @@ func (h *Halo) MatchesForPlayer(player, modes string, start, count int) MatchesF
|
||||
url.RawQuery = q.Encode()
|
||||
jsonObject, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
log.Fatal("MatchesForPlayer Failed: ", err)
|
||||
return MatchesForPlayerStruct{}, fmt.Errorf(
|
||||
"MatchesForPlayer request Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
verifyValidID(playlistid, "Playlist ID")
|
||||
verifyValidID(seasonid, "Season ID")
|
||||
err := verifyValidID(playlistid)
|
||||
err = verifyValidID(seasonid)
|
||||
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/player-leaderboards/csr/%s/%s", h.baseurl, h.title, seasonid, playlistid))
|
||||
if err != nil {
|
||||
log.Fatal("PlayerLeaderboard URL Parse Failed: ", err)
|
||||
return PlayerLeaderboardStruct{}, err
|
||||
}
|
||||
q := url.Query()
|
||||
|
||||
@ -72,101 +77,116 @@ func (h *Halo) PlayerLeaderboard(seasonid, playlistid string, count int) PlayerL
|
||||
url.RawQuery = q.Encode()
|
||||
jsonObject, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
log.Fatal("PlayerLeaderboard Failed: ", err)
|
||||
return PlayerLeaderboardStruct{}, fmt.Errorf(
|
||||
"PlayerLeaderboard request Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
verifyValidID(matchid, "Match ID")
|
||||
err := verifyValidID(matchid)
|
||||
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/arena/matches/%s", h.baseurl, h.title, matchid))
|
||||
if err != nil {
|
||||
log.Fatal("CarnageReportArena URL Parse Failed: ", err)
|
||||
return CarnageReportArenaStruct{}, err
|
||||
}
|
||||
q := url.Query()
|
||||
url.RawQuery = q.Encode()
|
||||
jsonObject, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
log.Fatal("CarnageReportArena Failed: ", err)
|
||||
return CarnageReportArenaStruct{}, fmt.Errorf(
|
||||
"CarnageReportArena request Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
verifyValidID(matchid, "Match ID")
|
||||
err := verifyValidID(matchid)
|
||||
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/campaign/matches/%s", h.baseurl, h.title, matchid))
|
||||
if err != nil {
|
||||
log.Fatal("CarnageReportCampaign URL Parse Failed: ", err)
|
||||
return CarnageReportCampaignStruct{}, err
|
||||
}
|
||||
q := url.Query()
|
||||
url.RawQuery = q.Encode()
|
||||
jsonObject, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
log.Fatal("CarnageReportCampaign Failed: ", err)
|
||||
return CarnageReportCampaignStruct{}, fmt.Errorf(
|
||||
"CarnageReportCampaign request Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
verifyValidID(matchid, "Match ID")
|
||||
err := verifyValidID(matchid)
|
||||
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/custom/matches/%s", h.baseurl, h.title, matchid))
|
||||
if err != nil {
|
||||
log.Fatal("CarnageReportCustom URL Parse Failed: ", err)
|
||||
return CarnageReportCustomStruct{}, err
|
||||
}
|
||||
q := url.Query()
|
||||
url.RawQuery = q.Encode()
|
||||
jsonObject, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
log.Fatal("CarnageReportCustom Failed: ", err)
|
||||
return CarnageReportCustomStruct{}, fmt.Errorf(
|
||||
"CarnageReportCustom request Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
verifyValidID(matchid, "Match ID")
|
||||
err := verifyValidID(matchid)
|
||||
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/warzone/matches/%s", h.baseurl, h.title, matchid))
|
||||
if err != nil {
|
||||
log.Fatal("CarnageReportWarzone URL Parse Failed: ", err)
|
||||
return CarnageReportWarzoneStruct{}, err
|
||||
}
|
||||
q := url.Query()
|
||||
url.RawQuery = q.Encode()
|
||||
jsonObject, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
log.Fatal("CarnageReportWarzone Failed: ", err)
|
||||
return CarnageReportWarzoneStruct{}, fmt.Errorf(
|
||||
"CarnageReportWarzone request Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
verifyValidID(seasonid, "Season ID")
|
||||
err := verifyValidID(seasonid)
|
||||
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/arena", h.baseurl, h.title))
|
||||
if err != nil {
|
||||
log.Fatal("CarnageReportWarzone URL Parse Failed: ", err)
|
||||
return ServiceRecordArenaStruct{}, err
|
||||
}
|
||||
q := url.Query()
|
||||
q.Set("players", players)
|
||||
@ -176,71 +196,83 @@ func (h *Halo) ServiceRecordArena(players, seasonid string) ServiceRecordArenaSt
|
||||
url.RawQuery = q.Encode()
|
||||
jsonObject, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
log.Fatal("ServiceRecordArena Failed: ", err)
|
||||
return ServiceRecordArenaStruct{}, fmt.Errorf(
|
||||
"ServiceRecordArena request Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/campaign", h.baseurl, h.title))
|
||||
if err != nil {
|
||||
log.Fatal("ServiceRecordCampaign URL Parse Failed: ", err)
|
||||
return ServiceRecordCampaignStruct{}, err
|
||||
}
|
||||
q := url.Query()
|
||||
q.Set("players", players)
|
||||
url.RawQuery = q.Encode()
|
||||
jsonObject, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
log.Fatal("ServiceRecordCampaign Failed: ", err)
|
||||
return ServiceRecordCampaignStruct{}, fmt.Errorf(
|
||||
"ServiceRecordCampaign request Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/custom", h.baseurl, h.title))
|
||||
if err != nil {
|
||||
log.Fatal("ServiceRecordCampaign URL Parse Failed: ", err)
|
||||
return ServiceRecordCustomStruct{}, err
|
||||
}
|
||||
q := url.Query()
|
||||
q.Set("players", players)
|
||||
url.RawQuery = q.Encode()
|
||||
jsonObject, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
log.Fatal("ServiceRecordCustom Failed: ", err)
|
||||
return ServiceRecordCustomStruct{}, fmt.Errorf(
|
||||
"ServiceRecordCustom request Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/warzone", h.baseurl, h.title))
|
||||
if err != nil {
|
||||
log.Fatal("ServiceRecordCampaign URL Parse Failed: ", err)
|
||||
return ServiceRecordWarzoneStruct{}, err
|
||||
}
|
||||
q := url.Query()
|
||||
q.Set("players", players)
|
||||
url.RawQuery = q.Encode()
|
||||
jsonObject, err := h.sendRequest(url.String())
|
||||
if err != nil {
|
||||
log.Fatal("ServiceRecordWarzone Failed: ", err)
|
||||
return ServiceRecordWarzoneStruct{}, fmt.Errorf(
|
||||
"ServiceRecordWarzone request Failed: %v",
|
||||
err,
|
||||
)
|
||||
}
|
||||
err = json.Unmarshal(jsonObject, &j)
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user