Added ability to import HALO_API_KEY environment variable.

The secret API key is now imported from the environment instead of being hard coded into the package.  The gamertag variable has been renamed to sampleGamertag.
This commit is contained in:
Thomas Bennett 2016-05-05 13:17:12 -07:00
parent 205714cad4
commit 5f0288e32c
2 changed files with 9 additions and 4 deletions

View File

@ -1 +1,6 @@
# go-halo5-api # go-halo5-api
go-halo5-api is a wrapper around the Halo5 API as documented at https://developer.haloapi.com.
# Usage
Set your API key as an environment variable
```export HALO_API_KEY="123456789abcdefghi"```

View File

@ -6,13 +6,12 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os"
"strings" "strings"
) )
var baseurl string = "https://www.haloapi.com" var baseurl string = "https://www.haloapi.com"
var gamertag string = "motta13"
var title string = "h5" var title string = "h5"
var apikey string = ""
func checkErr(err error) { func checkErr(err error) {
if err != nil { if err != nil {
@ -164,7 +163,6 @@ func spartan_image(baseurl, title, player string, size int, crop string) string
q.Set("crop", crop) q.Set("crop", crop)
} }
url.RawQuery = q.Encode() url.RawQuery = q.Encode()
fmt.Println(url.String())
response := send_request(url.String()) response := send_request(url.String())
return response return response
} }
@ -262,6 +260,7 @@ func metadata_request(baseurl, title, datatype, id string) string {
func send_request(url string) string { func send_request(url string) string {
request, err := http.NewRequest("GET", url, nil) request, err := http.NewRequest("GET", url, nil)
checkErr(err) checkErr(err)
apikey := os.Getenv("HALO_API_KEY")
request.Header.Set("Ocp-Apim-Subscription-Key", apikey) request.Header.Set("Ocp-Apim-Subscription-Key", apikey)
response, err := http.DefaultClient.Do(request) response, err := http.DefaultClient.Do(request)
@ -274,6 +273,7 @@ func send_request(url string) string {
} }
// Sample values for testing // Sample values for testing
var sampleGamertag string = "motta13"
var sampleMode string = "warzone" var sampleMode string = "warzone"
var sampleMatchID string = "c35a35f8-f450-4836-a4c2-65100a7acb79" var sampleMatchID 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
@ -282,7 +282,7 @@ 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(matches_for_player(baseurl, title, gamertag, "warzone", 0, 0)) //fmt.Println(matches_for_player(baseurl, title, SampleGamertag, "warzone", 0, 0))
//fmt.Println(events_for_match(baseurl, title, sampleMatchID)) //fmt.Println(events_for_match(baseurl, title, sampleMatchID))
//fmt.Println(player_leaderboard(baseurl, title, sampleSeasonID, samplePlaylistID, 0)) //fmt.Println(player_leaderboard(baseurl, title, sampleSeasonID, samplePlaylistID, 0))
//fmt.Println(service_record_arena(baseurl, title, samplePlayers, sampleSeasonID)) //fmt.Println(service_record_arena(baseurl, title, samplePlayers, sampleSeasonID))