/blizzard

Go client library for Blizzard API data

Primary LanguageGoMIT LicenseMIT

blizzard

Go Reference Go Report Card Build Status

This is a Go client library for gathering Blizzard API reference data

Table of Contents

Getting Started

First, download the Blizzard library:

go get github.com/FuzzyStatic/blizzard/v2

Start using the library by initiating a new Blizzard config structure for your desired region and locale (client_id and client_secret can be acquired through your developer account at https://develop.battle.net/) and requesting an access token:

blizz := blizzard.NewClient("client_id", "client_secret", blizzard.US, blizzard.EnUS)

err := blizz.AccessTokenRequest(ctx)
if err != nil {
  fmt.Println(err)
}

Fetching Diablo 3 Data

You can use the functions prefixed with "D3" to acquire Diablo 3 information. For example, you can get information about the current D3 hardcore necromancer leaderboards:

dat, _, err := blizz.D3SeasonLeaderboardHardcoreNecromancer(ctx, 15)
if err != nil {
  fmt.Println(err)
}

fmt.Printf("%+v\n", dat)

Fetching Hearthstone Data

You can use the functions prefixed with "HS" to acquire Hearthstone information. For example, you can get information about all the Hearthstone cards:

dat, _, err := blizz.HSCardsAll(ctx)
if err != nil {
  fmt.Println(err)
}

fmt.Printf("%+v\n", dat)

Fetching StarCraft 2 Data

You can use the functions prefixed with "SC2" to acquire StarCraft 2 information. For example, you can get information about the current SC2 grandmaster ladder:

dat, _, err := blizz.SC2LadderGrandmaster(ctx, blizzard.EU)
if err != nil {
  fmt.Println(err)
}

fmt.Printf("%+v\n", dat)

Fetching World of Warcraft Data

You can use the functions prefixed with "WoW" to acquire World of Warcraft information. For example, you can get information about your WoW character profile:

dat, _, err := blizz.WoWCharacterProfileSummary(ctx, "illidan", "wildz")
if err != nil {
  fmt.Println(err)
}

fmt.Printf("%+v\n", dat)

or get information about specific spells:

dat, _, err := blizz.WoWSpell(ctx, 17086)
if err != nil {
  fmt.Println(err)
}

fmt.Printf("%+v\n", dat)

or the PvP leaderboards:

dat, _, err := blizz.WoWCharacterPvPBracketStatistics(ctx, wowp.Bracket3v3)
if err != nil {
  fmt.Println(err)
}

fmt.Printf("%+v\n", dat)

Fetching World of Warcraft Classic Data

You can use the functions prefixed with "ClassicWoW" to acquire World of Warcraft Classic information. For example, you can get information about WoW Classic creature data:

dat, _, err := blizz.ClassicWoWCreature(ctx, 30)
if err != nil {
  fmt.Println(err)
}

fmt.Printf("%+v\n", dat)

Authorization for User Data

To use the UserInfoHeader or WoWUserCharacters functions to acquire data about other users (and not your own), you must use the OAuth2 redirect method to get an authorized token. This is useful for building websites that display more personal or individualized data. The following code snippet is an example on how to acquire authorized tokens for other users. A working example can be found in the examples/authCodeFlow directory. Before the redirect URI will work, you will have to add it to your client settings at https://develop.battle.net/access:

package main

import (
  "context"
  "encoding/json"
  "fmt"
  "log"
  "net/http"

  "github.com/FuzzyStatic/blizzard"
  "github.com/FuzzyStatic/blizzard/oauth"
  "golang.org/x/oauth2"
)

var (
  cfg   oauth2.Config
  blizz *blizzard.Client
)

// Homepage
func HomePage(w http.ResponseWriter, r *http.Request) {
  fmt.Println("Homepage Hit!")
  u := cfg.AuthCodeURL("my_random_state")
  http.Redirect(w, r, u, http.StatusFound)
}

// Authorize
func Authorize(w http.ResponseWriter, r *http.Request) {
  err := r.ParseForm()
  if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
  }

  state := r.Form.Get("state")
  if state != "my_random_state" {
    http.Error(w, "State invalid", http.StatusBadRequest)
    return
  }

  code := r.Form.Get("code")
  if code == "" {
    http.Error(w, "Code not found", http.StatusBadRequest)
    return
  }

  token, err := cfg.Exchange(context.Background(), code)
  if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
  }

  e := json.NewEncoder(w)
  e.SetIndent("", "  ")
  err = e.Encode(*token)
  if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
  }

  dat1, _, err := blizz.UserInfoHeader(token)
  if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
  }

  fmt.Printf("%+v\n", dat1)

  dat2, _, err := blizz.WoWUserCharacters(token)
  if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
  }

  fmt.Printf("%+v\n", dat2)
}

func main() {
  blizz = blizzard.NewClient("client_id", "client_secret", blizzard.US, blizzard.EnUS)
  cfg = blizz.AuthorizeConfig("http://<mydomain>:9094/oauth2", oauth.ProfileD3, oauth.ProfileSC2, oauth.ProfileWoW)

  http.HandleFunc("/", HomePage)
  http.HandleFunc("/oauth2", Authorize)

  // We start up our Client on port 9094
  log.Println("Client is running at 9094 port.")
  log.Fatal(http.ListenAndServe(":9094", nil))
}

Fetching OAuth Data

Now you can validate those tokens with the OAuth API:

dat, _, err := blizz.TokenValidation(ctx, token)
if err != nil {
  fmt.Println(err)
}

fmt.Printf("%+v\n", dat)

Documentation

See the Blizzard API reference and the Go reference for all the different datasets that can be acquired.

Special Thanks

Thanks to JSON-to-Go for making JSON to Go structure creation simple.

Thanks to all who contribute to keep this package current.