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
This commit is contained in:
Derek McQuay 2016-05-11 10:48:00 -07:00
parent 76531a8dcb
commit 914869dfed
2 changed files with 22 additions and 49 deletions

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 {
@ -53,39 +45,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)
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) {
re, _ := regexp.Compile("^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$") 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 !re.MatchString(id) {
if valid == false { return fmt.Errorf("Not a Valid id: ", id)
log.Fatal("%s is not a valid %s", ID, name)
} }
return nil
} }

View File

@ -2,25 +2,35 @@ package halo
import ( import (
"fmt" "fmt"
"log"
"net/url" "net/url"
"strings" "strings"
) )
func EmblemImage(baseurl, title, player string, size int) []byte { func (h *Halo) EmblemImage(player string, size int) []byte {
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",
h.baseurl,
h.title,
player,
))
if err != nil {
log.Fatal("Error parsing URL: ", 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", string(size))
} }
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
response := sendRequest(url.String()) response, err := h.sendRequest(url.String())
return response return response
} }
func SpartanImage(baseurl, title, player string, size int, crop string) []byte { func (h *Halo) SpartanImage(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", h.baseurl, h.title, player))
checkErr(err) if err != nil {
log.Fatal("Error parsing URL: ", 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", string(size))
@ -29,6 +39,6 @@ func SpartanImage(baseurl, title, player string, size int, crop string) []byte {
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 return response
} }