From 017d10081064424b4540869c71dfcf8cd6e60dce Mon Sep 17 00:00:00 2001 From: derek mcquay Date: Wed, 11 May 2016 14:52:37 -0700 Subject: [PATCH] Restructured code to pass up and handle errors Pretty big change. Things now pass up errors that can be handled by the caller. This brought about a lot of change. Errors attempt to be descriptive of where they came from and notify user of the api what potentailly caused the error. Also renamed go-halo5-api.go to a more suitable name, http.go. This might change in the future if we implement the http.Client interface --- cmd/gohalo/main.go | 19 ++- halo/{go-halo5-api.go => http.go} | 10 +- halo/metadata.go | 222 +++++++++++++++++++----------- halo/profile.go | 26 ++-- halo/stats.go | 160 ++++++++++++--------- 5 files changed, 279 insertions(+), 158 deletions(-) rename halo/{go-halo5-api.go => http.go} (86%) diff --git a/cmd/gohalo/main.go b/cmd/gohalo/main.go index e0d8c47..204c50a 100644 --- a/cmd/gohalo/main.go +++ b/cmd/gohalo/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log" "os" "github.com/dmmcquay/go-halo5-api/halo" @@ -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,7 +38,22 @@ func getAPIKey() string { func main() { h := halo.NewHalo(baseurl, title, getAPIKey()) - fmt.Println(h.Enemies()) + 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()) //fmt.Println(h.Impulses()) diff --git a/halo/go-halo5-api.go b/halo/http.go similarity index 86% rename from halo/go-halo5-api.go rename to halo/http.go index e051ab0..a3238cf 100644 --- a/halo/go-halo5-api.go +++ b/halo/http.go @@ -15,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 } @@ -31,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 diff --git a/halo/metadata.go b/halo/metadata.go index dfc37dd..9c5bba3 100644 --- a/halo/metadata.go +++ b/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 } diff --git a/halo/profile.go b/halo/profile.go index 82d92e4..748bafa 100644 --- a/halo/profile.go +++ b/halo/profile.go @@ -2,43 +2,49 @@ package halo import ( "fmt" - "log" "net/url" + "strconv" "strings" ) -func (h *Halo) EmblemImage(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", + fmt.Sprintf("%s/profile/%s/profiles/%s/emblem", h.baseurl, h.title, player, )) if err != nil { - log.Fatal("Error parsing URL: ", err) + 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, err := h.sendRequest(url.String()) - return response + if err != nil { + return "", err + } + return string(response), nil } -func (h *Halo) SpartanImage(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", h.baseurl, h.title, player)) if err != nil { - log.Fatal("Error parsing URL: ", err) + 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, err := h.sendRequest(url.String()) - return response + if err != nil { + return "", err + } + return string(response), nil } diff --git a/halo/stats.go b/halo/stats.go index 0c204f8..9c50d04 100644 --- a/halo/stats.go +++ b/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 }