Add WoW Connected Realm APIs and update Examples
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package example
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -7,38 +7,19 @@ import (
|
||||
|
||||
"battlenetapi/battlenet"
|
||||
"battlenetapi/wow/gamedata"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Load the .env file
|
||||
err := godotenv.Load(".env")
|
||||
if err != nil {
|
||||
fmt.Println("Error loading .env file")
|
||||
}
|
||||
|
||||
// Authenticate with the BattleNet API
|
||||
clientID := os.Getenv("CLIENT_ID")
|
||||
clientSecret := os.Getenv("CLIENT_SECRET")
|
||||
credentials := battlenet.GetAccessToken(clientID, clientSecret)
|
||||
|
||||
func GetLeaderboard(params battlenet.BattleNetAPIParams, bracket string) {
|
||||
// Get the current PVP season
|
||||
params := battlenet.BattleNetAPIParams{
|
||||
UrlOrEndpoint: gamedata.PvpSeasonIndexEndpoint,
|
||||
Namespace: battlenet.DYNAMIC,
|
||||
Region: battlenet.US,
|
||||
Token: credentials.AccessToken,
|
||||
}
|
||||
response := battlenet.BattleNetAPI(params)
|
||||
response := battlenet.BattleNetAPI(params, nil)
|
||||
var pvpIndex gamedata.PvpSeasonIndexAPI
|
||||
json.Unmarshal(response, &pvpIndex)
|
||||
|
||||
// Get the leaderboards for the current PVP season and Bracket
|
||||
shuffleOrBlitz := fmt.Sprintf("blitz-%s", gamedata.ClassDemonHunterHavoc)
|
||||
params.UrlOrEndpoint = fmt.Sprintf(gamedata.PvpLeaderboardEndpoint, pvpIndex.CurrentSeason.ID, shuffleOrBlitz)
|
||||
response = battlenet.BattleNetAPI(params)
|
||||
file, err := os.Create(fmt.Sprintf("pvp_season_%d_leaderboard-bracket_%s.json", pvpIndex.CurrentSeason.ID, shuffleOrBlitz))
|
||||
|
||||
params.UrlOrEndpoint = fmt.Sprintf(gamedata.PvpLeaderboardEndpoint, pvpIndex.CurrentSeason.ID, bracket)
|
||||
response = battlenet.BattleNetAPI(params, nil)
|
||||
file, err := os.Create(fmt.Sprintf("pvp_season_%d_leaderboard-bracket_%s.json", pvpIndex.CurrentSeason.ID, bracket))
|
||||
if err != nil {
|
||||
fmt.Println("Error creating file:", err)
|
||||
return
|
||||
|
||||
74
example/getRealmStatus.go
Normal file
74
example/getRealmStatus.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"battlenetapi/battlenet"
|
||||
"battlenetapi/wow/gamedata"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func GetRealmStatus(params battlenet.BattleNetAPIParams, formatter battlenet.URLFormatter) {
|
||||
// Get the realm status
|
||||
response := battlenet.BattleNetAPI(params, formatter)
|
||||
var searchResults gamedata.ConnectedRealmSearchAPI
|
||||
var data []gamedata.RealmSearchResult
|
||||
err := json.Unmarshal(response, &searchResults)
|
||||
if err != nil {
|
||||
fmt.Println("Unable to parse ConnectedRealmSearchAPI")
|
||||
return
|
||||
}
|
||||
|
||||
// Handle more than one page of data
|
||||
currentPage := params.Options.(gamedata.RealmStatusParams).Page
|
||||
if currentPage > 1 {
|
||||
for currentPage <= searchResults.PageCount {
|
||||
currentPage += 1
|
||||
|
||||
// Update page to current page
|
||||
realmStatusParams := params.Options.(*gamedata.RealmStatusParams)
|
||||
realmStatusParams.Page = currentPage
|
||||
params.Options = realmStatusParams
|
||||
|
||||
// Call next page
|
||||
response := battlenet.BattleNetAPI(params, formatter)
|
||||
json.Unmarshal(response, &searchResults)
|
||||
data = append(data, searchResults.Results...)
|
||||
}
|
||||
} else {
|
||||
data = searchResults.Results
|
||||
}
|
||||
|
||||
file, err := os.Create("realm_status.json")
|
||||
if err != nil {
|
||||
fmt.Println("Error creating file:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Print out select information from the data
|
||||
for _, realm := range data {
|
||||
info := realm.Data.Realms[0]
|
||||
fmt.Printf(
|
||||
"%s-%s (%s - %s): %s (%s) \n",
|
||||
info.Name.US,
|
||||
info.Type.Name.US,
|
||||
info.Category.US,
|
||||
info.Timezone,
|
||||
realm.Data.Status.Name.US,
|
||||
realm.Data.Population.Name.US,
|
||||
)
|
||||
}
|
||||
|
||||
// Save complete data
|
||||
searchResults.Results = data
|
||||
results, err := json.Marshal(searchResults)
|
||||
if err != nil {
|
||||
fmt.Println("Unable to convert search results to a seralized object")
|
||||
}
|
||||
_, err = file.Write(results)
|
||||
if err != nil {
|
||||
fmt.Println("Error writing data to file:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user