From 914869dfed1c7a50c5ec0eed2080d006f2eb48c4 Mon Sep 17 00:00:00 2001 From: derek mcquay Date: Wed, 11 May 2016 10:48:00 -0700 Subject: [PATCH] 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 --- halo/go-halo5-api.go | 45 ++++---------------------------------------- halo/profile.go | 26 +++++++++++++++++-------- 2 files changed, 22 insertions(+), 49 deletions(-) diff --git a/halo/go-halo5-api.go b/halo/go-halo5-api.go index d2c6bfa..e051ab0 100644 --- a/halo/go-halo5-api.go +++ b/halo/go-halo5-api.go @@ -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 { @@ -53,39 +45,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 } diff --git a/halo/profile.go b/halo/profile.go index 8647e9c..82d92e4 100644 --- a/halo/profile.go +++ b/halo/profile.go @@ -2,25 +2,35 @@ package halo import ( "fmt" + "log" "net/url" "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) []byte { + url, err := url.Parse( + fmt.Sprintf("%s/profile/%s/profiles/%s", + h.baseurl, + h.title, + player, + )) + if err != nil { + log.Fatal("Error parsing URL: ", err) + } q := url.Query() if (size == 95) || (size == 128) || (size == 190) || (size == 256) || (size == 512) { q.Set("size", string(size)) } url.RawQuery = q.Encode() - response := sendRequest(url.String()) + response, err := h.sendRequest(url.String()) return response } -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) []byte { + 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) + } q := url.Query() if (size == 95) || (size == 128) || (size == 190) || (size == 256) || (size == 512) { q.Set("size", string(size)) @@ -29,6 +39,6 @@ func SpartanImage(baseurl, title, player string, size int, crop string) []byte { q.Set("crop", crop) } url.RawQuery = q.Encode() - response := sendRequest(url.String()) + response, err := h.sendRequest(url.String()) return response }