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:
parent
76531a8dcb
commit
914869dfed
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user