Compare commits

..

8 Commits

Author SHA1 Message Date
Thomas Bennett
38718dc289 fixed broken structs 2016-05-16 16:40:29 -07:00
Thomas Bennett
f7ac04ea6a fixed string conversion to Itoa 2016-05-15 11:47:50 -07:00
Thomas Bennett
d4eba2ba44 updated regex to be more concise 2016-05-15 11:47:27 -07:00
Thomas Bennett
b296f56e2a Merge pull request #5 from dmmcquay/structs
Structs
2016-05-11 14:58:21 -07:00
017d100810 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
2016-05-11 14:52:37 -07:00
4550ba9d30 moved main.go in to a seperate cmd dir 2016-05-11 10:48:44 -07:00
914869dfed updating profile.go to use methods
also changed some things in go-halo5-api.go.  Still need to rethink how
we are passing up errors and failures of trying to get the results
2016-05-11 10:48:00 -07:00
Thomas Bennett
76531a8dcb finished converting functions to methods in the stats file 2016-05-11 09:31:42 -07:00
6 changed files with 1654 additions and 1463 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())
@ -54,7 +71,9 @@ func main() {
//fmt.Println(h.GameVariants(sampleGameVariantID)) //fmt.Println(h.GameVariants(sampleGameVariantID))
//fmt.Println(h.MapVariants(sampleMapVariantsID)) //fmt.Println(h.MapVariants(sampleMapVariantsID))
//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.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) re, _ := regexp.Compile("^\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}$")
checkErr(err) if !re.MatchString(id) {
request.Header.Set("Ocp-Apim-Subscription-Key", getAPIKey()) return fmt.Errorf("Not a Valid id: ", id)
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}$")
valid := re.MatchString(ID)
if valid == false {
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,38 @@ package halo
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/url" "net/url"
"strconv"
) )
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()
@ -39,118 +42,238 @@ func (h *Halo) MatchesForPlayer(player, modes string, start, count int) MatchesF
q.Set("modes", modes) q.Set("modes", modes)
} }
if start != 0 { if start != 0 {
q.Set("start", string(start)) q.Set("start", strconv.Itoa(start))
} }
if count != 0 { if count != 0 {
q.Set("count", string(count)) q.Set("count", strconv.Itoa(count))
} }
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 PlayerLeaderboard(baseurl, title, seasonid, playlistid string, count int) []byte { func (h *Halo) PlayerLeaderboard(seasonid, playlistid string, count int) (PlayerLeaderboardStruct, error) {
verifyValidID(playlistid, "Playlist ID") var j PlayerLeaderboardStruct
verifyValidID(seasonid, "Season ID") err := verifyValidID(playlistid)
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/player-leaderboards/csr/%s/%s", baseurl, title, seasonid, playlistid)) err = verifyValidID(seasonid)
checkErr(err) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/player-leaderboards/csr/%s/%s", h.baseurl, h.title, seasonid, playlistid))
if err != nil {
return PlayerLeaderboardStruct{}, err
}
q := url.Query() q := url.Query()
if count != 0 { if count != 0 {
q.Set("count", string(count)) q.Set("count", strconv.Itoa(count))
} }
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
response := sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
return response if err != nil {
return PlayerLeaderboardStruct{}, fmt.Errorf(
"PlayerLeaderboard request Failed: %v",
err,
)
}
err = json.Unmarshal(jsonObject, &j)
if err != nil {
return PlayerLeaderboardStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
}
return j, nil
} }
func CarnageReportArena(baseurl, title, matchid string) []byte { func (h *Halo) CarnageReportArena(matchid string) (CarnageReportArenaStruct, error) {
verifyValidID(matchid, "Match ID") var j CarnageReportArenaStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/arena/matches/%s", baseurl, title, matchid)) err := verifyValidID(matchid)
checkErr(err) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/arena/matches/%s", h.baseurl, h.title, matchid))
if err != nil {
return CarnageReportArenaStruct{}, err
}
q := url.Query() q := url.Query()
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
response := sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
return response if err != nil {
return CarnageReportArenaStruct{}, fmt.Errorf(
"CarnageReportArena request Failed: %v",
err,
)
}
err = json.Unmarshal(jsonObject, &j)
if err != nil {
return CarnageReportArenaStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
}
return j, nil
} }
func CarnageReportCampaign(baseurl, title, matchid string) []byte { func (h *Halo) CarnageReportCampaign(matchid string) (CarnageReportCampaignStruct, error) {
verifyValidID(matchid, "Match ID") var j CarnageReportCampaignStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/campaign/matches/%s", baseurl, title, matchid)) err := verifyValidID(matchid)
checkErr(err) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/campaign/matches/%s", h.baseurl, h.title, matchid))
if err != nil {
return CarnageReportCampaignStruct{}, err
}
q := url.Query() q := url.Query()
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
response := sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
return response if err != nil {
return CarnageReportCampaignStruct{}, fmt.Errorf(
"CarnageReportCampaign request Failed: %v",
err,
)
}
err = json.Unmarshal(jsonObject, &j)
if err != nil {
return CarnageReportCampaignStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
}
return j, nil
} }
func CarnageReportCustom(baseurl, title, matchid string) []byte { func (h *Halo) CarnageReportCustom(matchid string) (CarnageReportCustomStruct, error) {
verifyValidID(matchid, "Match ID") var j CarnageReportCustomStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/custom/matches/%s", baseurl, title, matchid)) err := verifyValidID(matchid)
checkErr(err) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/custom/matches/%s", h.baseurl, h.title, matchid))
if err != nil {
return CarnageReportCustomStruct{}, err
}
q := url.Query() q := url.Query()
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
response := sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
return response if err != nil {
return CarnageReportCustomStruct{}, fmt.Errorf(
"CarnageReportCustom request Failed: %v",
err,
)
}
err = json.Unmarshal(jsonObject, &j)
if err != nil {
return CarnageReportCustomStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
}
return j, nil
} }
func CarnageReportWarzone(baseurl, title, matchid string) []byte { func (h *Halo) CarnageReportWarzone(matchid string) (CarnageReportWarzoneStruct, error) {
verifyValidID(matchid, "Match ID") var j CarnageReportWarzoneStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/warzone/matches/%s", baseurl, title, matchid)) err := verifyValidID(matchid)
checkErr(err) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/warzone/matches/%s", h.baseurl, h.title, matchid))
if err != nil {
return CarnageReportWarzoneStruct{}, err
}
q := url.Query() q := url.Query()
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
response := sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
return response if err != nil {
return CarnageReportWarzoneStruct{}, fmt.Errorf(
"CarnageReportWarzone request Failed: %v",
err,
)
}
err = json.Unmarshal(jsonObject, &j)
if err != nil {
return CarnageReportWarzoneStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
}
return j, nil
} }
func ServiceRecordArena(baseurl, title, players, seasonid string) []byte { func (h *Halo) ServiceRecordArena(players, seasonid string) (ServiceRecordArenaStruct, error) {
verifyValidID(seasonid, "Season ID") var j ServiceRecordArenaStruct
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/arena", baseurl, title)) err := verifyValidID(seasonid)
checkErr(err) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/arena", h.baseurl, h.title))
if err != nil {
return ServiceRecordArenaStruct{}, err
}
q := url.Query() q := url.Query()
q.Set("players", players) q.Set("players", players)
if seasonid != "" { if seasonid != "" {
q.Set("seasonId", seasonid) q.Set("seasonId", seasonid)
} }
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
response := sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
return response if err != nil {
return ServiceRecordArenaStruct{}, fmt.Errorf(
"ServiceRecordArena request Failed: %v",
err,
)
}
err = json.Unmarshal(jsonObject, &j)
if err != nil {
return ServiceRecordArenaStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
}
return j, nil
} }
func ServiceRecordCampaign(baseurl, title, players string) []byte { func (h *Halo) ServiceRecordCampaign(players string) (ServiceRecordCampaignStruct, error) {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/campaign", baseurl, title)) var j ServiceRecordCampaignStruct
checkErr(err) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/campaign", h.baseurl, h.title))
if err != nil {
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()
response := sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
return response if err != nil {
return ServiceRecordCampaignStruct{}, fmt.Errorf(
"ServiceRecordCampaign request Failed: %v",
err,
)
}
err = json.Unmarshal(jsonObject, &j)
if err != nil {
return ServiceRecordCampaignStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
}
return j, nil
} }
func ServiceRecordCustom(baseurl, title, players string) []byte { func (h *Halo) ServiceRecordCustom(players string) (ServiceRecordCustomStruct, error) {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/custom", baseurl, title)) var j ServiceRecordCustomStruct
checkErr(err) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/custom", h.baseurl, h.title))
if err != nil {
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()
response := sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
return response if err != nil {
return ServiceRecordCustomStruct{}, fmt.Errorf(
"ServiceRecordCustom request Failed: %v",
err,
)
}
err = json.Unmarshal(jsonObject, &j)
if err != nil {
return ServiceRecordCustomStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
}
return j, nil
} }
func ServiceRecordWarzone(baseurl, title, players string) []byte { func (h *Halo) ServiceRecordWarzone(players string) (ServiceRecordWarzoneStruct, error) {
url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/warzone", baseurl, title)) var j ServiceRecordWarzoneStruct
checkErr(err) url, err := url.Parse(fmt.Sprintf("%s/stats/%s/servicerecords/warzone", h.baseurl, h.title))
if err != nil {
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()
response := sendRequest(url.String()) jsonObject, err := h.sendRequest(url.String())
return response if err != nil {
return ServiceRecordWarzoneStruct{}, fmt.Errorf(
"ServiceRecordWarzone request Failed: %v",
err,
)
}
err = json.Unmarshal(jsonObject, &j)
if err != nil {
return ServiceRecordWarzoneStruct{}, fmt.Errorf("Failure to unmarshal json: %v", err)
}
return j, nil
} }

File diff suppressed because it is too large Load Diff