Fixed bug where SpartanImage and EmblemImage were not returning

Fixed formatting in the main.go file, and updated the SpartanImage and EmblemImage functions to return the URL of the image rather than the byte code.  All the other functions now return byte code which is cast to a string in the main.go file.
This commit is contained in:
Thomas Bennett 2016-05-05 16:41:09 -07:00
parent 0f96521408
commit 1a1b7af289
2 changed files with 67 additions and 54 deletions

View File

@ -16,7 +16,7 @@ func checkErr(err error) {
} }
} }
func EventsForMatch(baseurl, title, matchid string) string { func EventsForMatch(baseurl, title, matchid string) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/matches/%s/events", baseurl, title, matchid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/matches/%s/events", baseurl, title, matchid))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
@ -25,7 +25,7 @@ func EventsForMatch(baseurl, title, matchid string) string {
return response return response
} }
func MatchesForPlayer(baseurl, title, player, modes string, start, count int) string { func MatchesForPlayer(baseurl, title, player, modes string, start, count int) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/players/%s/matches", baseurl, title, player)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/players/%s/matches", baseurl, title, player))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
@ -44,7 +44,7 @@ func MatchesForPlayer(baseurl, title, player, modes string, start, count int) st
return response return response
} }
func PlayerLeaderboard(baseurl, title, seasonid, playlistid string, count int) string { func PlayerLeaderboard(baseurl, title, seasonid, playlistid string, count int) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/player-leaderboards/csr/%s/%s", baseurl, title, seasonid, playlistid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/player-leaderboards/csr/%s/%s", baseurl, title, seasonid, playlistid))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
@ -57,7 +57,7 @@ func PlayerLeaderboard(baseurl, title, seasonid, playlistid string, count int) s
return response return response
} }
func CarnageReportArena(baseurl, title, matchid string) string { func CarnageReportArena(baseurl, title, matchid string) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/arena/matches/%s", baseurl, title, matchid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/arena/matches/%s", baseurl, title, matchid))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
@ -66,7 +66,7 @@ func CarnageReportArena(baseurl, title, matchid string) string {
return response return response
} }
func CarnageReportCampaign(baseurl, title, matchid string) string { func CarnageReportCampaign(baseurl, title, matchid string) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/campaign/matches/%s", baseurl, title, matchid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/campaign/matches/%s", baseurl, title, matchid))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
@ -75,7 +75,7 @@ func CarnageReportCampaign(baseurl, title, matchid string) string {
return response return response
} }
func CarnageReportCustom(baseurl, title, matchid string) string { func CarnageReportCustom(baseurl, title, matchid string) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/custom/matches/%s", baseurl, title, matchid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/custom/matches/%s", baseurl, title, matchid))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
@ -84,7 +84,7 @@ func CarnageReportCustom(baseurl, title, matchid string) string {
return response return response
} }
func CarnageReportWarzone(baseurl, title, matchid string) string { func CarnageReportWarzone(baseurl, title, matchid string) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/warzone/matches/%s", baseurl, title, matchid)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/warzone/matches/%s", baseurl, title, matchid))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
@ -93,8 +93,8 @@ func CarnageReportWarzone(baseurl, title, matchid string) string {
return response return response
} }
func ServiceRecordArena(baseurl, title, players, seasonid string) string { func ServiceRecordArena(baseurl, title, players, seasonid string) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/arena/", baseurl, title)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/arena", baseurl, title))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
q.Set("players", players) q.Set("players", players)
@ -106,8 +106,8 @@ func ServiceRecordArena(baseurl, title, players, seasonid string) string {
return response return response
} }
func ServiceRecordCampaign(baseurl, title, players string) string { func ServiceRecordCampaign(baseurl, title, players string) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/campaign/", baseurl, title)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/campaign", baseurl, title))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
q.Set("players", players) q.Set("players", players)
@ -116,8 +116,8 @@ func ServiceRecordCampaign(baseurl, title, players string) string {
return response return response
} }
func ServiceRecordCustom(baseurl, title, players string) string { func ServiceRecordCustom(baseurl, title, players string) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/custom/", baseurl, title)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/custom", baseurl, title))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
q.Set("players", players) q.Set("players", players)
@ -126,8 +126,8 @@ func ServiceRecordCustom(baseurl, title, players string) string {
return response return response
} }
func ServiceRecordWarzone(baseurl, title, players string) string { func ServiceRecordWarzone(baseurl, title, players string) []byte {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/warzone/", baseurl, title)) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/warzone", baseurl, title))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
q.Set("players", players) q.Set("players", players)
@ -137,8 +137,8 @@ func ServiceRecordWarzone(baseurl, title, players string) string {
} }
// Begin profile section // Begin profile section
func EmblemImage(baseurl, title, player string, size int) string { func EmblemImage(baseurl, title, player string, size int) []byte {
url, err := url.Parse(fmt.Sprintf("%s/profile/%s/profiles/%s/", baseurl, title, player)) url, err := url.Parse(fmt.Sprintf("%s/profile/%s/profiles/%s", baseurl, title, player))
checkErr(err) checkErr(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) {
@ -149,8 +149,8 @@ func EmblemImage(baseurl, title, player string, size int) string {
return response return response
} }
func SpartanImage(baseurl, title, player string, size int, crop string) string { 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)) url, err := url.Parse(fmt.Sprintf("%s/profile/%s/profiles/%s/spartan", baseurl, title, player))
checkErr(err) checkErr(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) {
@ -165,87 +165,87 @@ func SpartanImage(baseurl, title, player string, size int, crop string) string {
} }
// Begin metadata section // Begin metadata section
func CampaignMissions(baseurl, title string) string { func CampaignMissions(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "campaign-missions", "") return metadataRequest(baseurl, title, "campaign-missions", "")
} }
func Commendations(baseurl, title string) string { func Commendations(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "commendations", "") return metadataRequest(baseurl, title, "commendations", "")
} }
func CsrDesignations(baseurl, title string) string { func CsrDesignations(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "csr-designations", "") return metadataRequest(baseurl, title, "csr-designations", "")
} }
func Enemies(baseurl, title string) string { func Enemies(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "enemies", "") return metadataRequest(baseurl, title, "enemies", "")
} }
func FlexibleStats(baseurl, title string) string { func FlexibleStats(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "flexible-stats", "") return metadataRequest(baseurl, title, "flexible-stats", "")
} }
func GameBaseVariants(baseurl, title string) string { func GameBaseVariants(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "game-base-variants", "") return metadataRequest(baseurl, title, "game-base-variants", "")
} }
func GameVariants(baseurl, title, id string) string { func GameVariants(baseurl, title, id string) []byte {
return metadataRequest(baseurl, title, "game-variants", id) return metadataRequest(baseurl, title, "game-variants", id)
} }
func Impulses(baseurl, title string) string { func Impulses(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "impulses", "") return metadataRequest(baseurl, title, "impulses", "")
} }
func MapVariants(baseurl, title, id string) string { func MapVariants(baseurl, title, id string) []byte {
return metadataRequest(baseurl, title, "map-variants", id) return metadataRequest(baseurl, title, "map-variants", id)
} }
func Maps(baseurl, title string) string { func Maps(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "maps", "") return metadataRequest(baseurl, title, "maps", "")
} }
func Medals(baseurl, title string) string { func Medals(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "medals", "") return metadataRequest(baseurl, title, "medals", "")
} }
func Playlists(baseurl, title string) string { func Playlists(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "playlists", "") return metadataRequest(baseurl, title, "playlists", "")
} }
func RequisitionPacks(baseurl, title, id string) string { func RequisitionPacks(baseurl, title, id string) []byte {
return metadataRequest(baseurl, title, "requisition-packs", id) return metadataRequest(baseurl, title, "requisition-packs", id)
} }
func Requisitions(baseurl, title, id string) string { func Requisitions(baseurl, title, id string) []byte {
return metadataRequest(baseurl, title, "requisitions", id) return metadataRequest(baseurl, title, "requisitions", id)
} }
func Seasons(baseurl, title string) string { func Seasons(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "seasons", "") return metadataRequest(baseurl, title, "seasons", "")
} }
func Skulls(baseurl, title string) string { func Skulls(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "skulls", "") return metadataRequest(baseurl, title, "skulls", "")
} }
func SpartanRanks(baseurl, title string) string { func SpartanRanks(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "spartan-ranks", "") return metadataRequest(baseurl, title, "spartan-ranks", "")
} }
func TeamColors(baseurl, title string) string { func TeamColors(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "team-colors", "") return metadataRequest(baseurl, title, "team-colors", "")
} }
func Vehicles(baseurl, title string) string { func Vehicles(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "vehicles", "") return metadataRequest(baseurl, title, "vehicles", "")
} }
func Weapons(baseurl, title string) string { func Weapons(baseurl, title string) []byte {
return metadataRequest(baseurl, title, "weapons", "") return metadataRequest(baseurl, title, "weapons", "")
} }
func metadataRequest(baseurl, title, datatype, id string) string { func metadataRequest(baseurl, title, datatype, id string) []byte {
url, err := url.Parse(fmt.Sprintf("%s/metadata/%s/metadata/%s/%s", baseurl, title, datatype, id)) url, err := url.Parse(fmt.Sprintf("%s/metadata/%s/metadata/%s/%s", baseurl, title, datatype, id))
checkErr(err) checkErr(err)
q := url.Query() q := url.Query()
@ -254,7 +254,7 @@ func metadataRequest(baseurl, title, datatype, id string) string {
return response return response
} }
func sendRequest(url string) string { func sendRequest(url string) []byte {
request, err := http.NewRequest("GET", url, nil) request, err := http.NewRequest("GET", url, nil)
checkErr(err) checkErr(err)
request.Header.Set("Ocp-Apim-Subscription-Key", getAPIKey()) request.Header.Set("Ocp-Apim-Subscription-Key", getAPIKey())
@ -263,9 +263,16 @@ func sendRequest(url string) string {
checkErr(err) checkErr(err)
defer response.Body.Close() 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) contents, err := ioutil.ReadAll(response.Body)
checkErr(err) checkErr(err)
return string(contents)
return contents
} }
func getAPIKey() string { func getAPIKey() string {

30
main.go
View File

@ -1,11 +1,5 @@
package main package main
import (
"fmt"
"github.com/tbenz9/go-halo5-api/halo"
)
var baseurl string = "https://www.haloapi.com" var baseurl string = "https://www.haloapi.com"
var title string = "h5" var title string = "h5"
@ -19,10 +13,22 @@ var samplePlayers string = "motta13,smoke721"
func main() { func main() {
// Uncomment any of the below for sample output. // Uncomment any of the below for sample output.
fmt.Println(halo.MatchesForPlayer(baseurl, title, sampleGamertag, "warzone", 0, 0))
//fmt.Println(events_for_match(baseurl, title, sampleMatchID)) // Matches For Player
//fmt.Println(player_leaderboard(baseurl, title, sampleSeasonID, samplePlaylistID, 0)) //fmt.Println(string(halo.MatchesForPlayer(baseurl, title, sampleGamertag, "warzone", 0, 0)))
//fmt.Println(service_record_arena(baseurl, title, samplePlayers, sampleSeasonID))
//fmt.Println(vehicles(baseurl, title)) // Events For Match
//fmt.Println(spartan_image(baseurl, title, gamertag, 0, "")) //fmt.Println(string(halo.EventsForMatch(baseurl, title, sampleMatchID)))
// Player Leaderboards
//fmt.Println(string(halo.PlayerLeaderboard(baseurl, title, sampleSeasonID, samplePlaylistID, 0)))
// Service Record: Arena
//fmt.Println(string(halo.ServiceRecordArena(baseurl, title, samplePlayers, sampleSeasonID)))
// Vehicles
//fmt.Println(string(halo.Vehicles(baseurl, title)))
// Spartan Image
//fmt.Println(string(halo.SpartanImage(baseurl, title, sampleGamertag, 0, "")))
} }