bnet

package module
v0.0.0-...-bdcec58 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2017 License: MIT Imports: 12 Imported by: 3

README

go-bnet

go-bnet is a Go client library for accessing the Battle.net API. In addition to providing an API client, this package provides OAuth endpoints.

Documentation: GoDoc

Build Status: Build Status

API Coverage: Currently only the account information API is implemented. However, the base helpers are there to easily and quickly implement any other APIs such as the WoW or SC2 data.

Usage

import "github.com/mitchellh/go-bnet"
Authentication

Authenticate using the Go OAuth2 library. Endpoints are provided via the Endpoint function. A guide to using OAuth2 to authenticate is available in this blog post. The blog post uses GitHub as an example but it is almost identical for Battle.net and this library.

Battle.net endpoints are region-specific, so specify the region to the Endpoint function and use the resulting value. Example:

oauthCfg := &oauth2.Config{
    // Get from dev.battle.net
    ClientID:     "",
    ClientSecret: "",

    // Endpoint from this library
    Endpoint: bnet.Endpoint("us"),
}

Once you have access to the OAuth client, you can initilize the Battle.net API client:

// Token from prior auth
authClient := oauthCfg.Client(oauth2.NoContext, token)

// Initialize the client
client := bnet.NewClient(oauthClient)

// ... API calls
API Calls

Once a client is made, basic API calls can easliy be made:

user, resp, err := client.Account().User()
fmt.Printf("User: %#v", user)

All API calls return a *Response value in addition to a richer type and error. The response contains the http.Response as well as metadata such as quotas, QPS, etc. from Battle.net

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckError

func CheckError(r *http.Response) error

CheckError checks for an error in the given response.

func Endpoint

func Endpoint(region string) oauth2.Endpoint

Endpoint returns the endpoint for the given region. This doesn't validate the region name so you must use one that is valid from Battle.net.

Types

type AccountService

type AccountService struct {
	// contains filtered or unexported fields
}

AccountService has account-related APIs. See Client.

func (*AccountService) User

func (s *AccountService) User() (*User, *Response, error)

User calls the /account/user endpoint. See Battle.net docs.

type Achievement

type Achievement struct {
	ID             int `json:"achievementId"`
	CompletionDate int `json:"completionDate"`
}

Achievement represents a single Starcraft 2 achievement.

type Achievements

type Achievements struct {
	Points       Points        `json:"points"`
	Achievements []Achievement `json:"achievements"`
}

Achievements represents achievement information for a Starcraft 2 profile.

type Career

type Career struct {
	PrimaryRace      string `json:"primaryRace"`
	TerranWins       int    `json:"terranWins"`
	ProtossWins      int    `json:"protossWins"`
	ZergWins         int    `json:"zergWins"`
	Highest1v1Rank   string `json:"highest1v1Rank"`
	SeasonTotalGames int    `json:"seasonTotalGames"`
	CareerTotalGames int    `json:"careerTotalGames"`
}

Career represents game statistics for a character's Battle.net career.

type CharacterImage

type CharacterImage struct {
	X      int    `json:"x"`
	Y      int    `json:"y"`
	W      int    `json:"w"`
	H      int    `json:"h"`
	Offset int    `json:"offset"`
	Url    string `json:"url"`
}

CharacterImage is a character's portrait or avatar.

type Client

type Client struct {
	// Client is the HTTP client to use for communication.
	Client *http.Client

	// BaseURL is the base URL for API requests. This should match
	// the region with the auth region used for Client.
	BaseURL *url.URL

	// UserAgent is the user agent to set on API requests.
	UserAgent string
}

Client is the API client for Battle.net. Create this using NewClient. This can also be constructed manually but it isn't recommended.

func NewClient

func NewClient(region string, c *http.Client) *Client

NewClient creates a new Battle.net client.

region must be a valid Battle.net region. This will not validate it is valid.

The http.Client argument should usually be retrieved via the oauth2 Go library NewClient function. It must be a client that automatically injects authentication details into requests.

func (*Client) Account

func (c *Client) Account() *AccountService

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}) (*Response, error)

Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface, the raw response body will be written to v, without attempting to first decode it.

func (*Client) NewRequest

func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error)

NewRequest creates an API request. A relative URL can be provided in urlStr, in which case it is resolved relative to the BaseURL of the Client. Relative URLs should always be specified without a preceding slash. If specified, the value pointed to by body is JSON encoded and included as the request body.

func (*Client) Profile

func (c *Client) Profile() *ProfileService

type ErrorResponse

type ErrorResponse struct {
	Response *http.Response

	Code        string `json:"error"`
	Description string `json:"error_description"`
	Scope       string `json:"scope"`
}

ErrorResponse is the error response structure from the Battle.net API

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

type Level

type Level struct {
	Level          int `json:"level"`
	TotalLevelXP   int `json:"totalLevelXP"`
	CurrentLevelXP int `json:"currentLevelXP"`
}

Level is the current level and XP a character has earned.

type Points

type Points struct {
	Total int `json:"totalPoints"`
}

Points holds a character's total achievement points.

type ProfileService

type ProfileService struct {
	// contains filtered or unexported fields
}

ProfileService has OAuth Profile APIs. See Client.

func (*ProfileService) SC2

func (s *ProfileService) SC2() (*SC2Profile, *Response, error)

SC2() calls the /sc2/profile/user endpoint. This endpoint uses OAuth2 to retrieve a user's Starcraft 2 profile. See Battle.net docs.

type Response

type Response struct {
	*http.Response

	QPSCurrent    int
	QPSAllotted   int
	QuotaCurrent  int
	QuotaAllotted int
	QuotaReset    time.Time
}

Reponse is a Battle.net API response. This wraps the standard http.Response and provides convenient access to some of the metadata returned.

type Rewards

type Rewards struct {
	Selected []int `json:"selected"`
	Earned   []int `json:"earned"`
}

Rewards represents selected and earned rewards for a profile.

type SC2Character

type SC2Character struct {
	ID           int            `json:"id"`
	Realm        int            `json:"realm"`
	Name         string         `json:"name"`
	DisplayName  string         `json:"displayName"`
	ClanName     string         `json:"clanName"`
	ClanTag      string         `json:"clanTag"`
	ProfilePath  string         `json:"profilePath"`
	Portrait     CharacterImage `json:"portrait"`
	Avatar       CharacterImage `json:"avatar"`
	Career       Career         `json:"career"`
	SwarmLevels  SwarmLevels    `json:"swarmLevels"`
	Season       Season         `json:"season"`
	Rewards      Rewards        `json:"rewards"`
	Achievements Achievements   `json:"achievements"`
}

SC2Character represents a character in a user's Starcraft 2 profile.

type SC2Profile

type SC2Profile struct {
	Characters []SC2Character `json:"characters"`
}

SC2Profile represents the profile information for a user's Starcraft 2 profile.

type SC2Service

type SC2Service struct {
	// contains filtered or unexported fields
}

SC2Service has Starcraft2-related APIs. See Client.

type Season

type Season struct {
	ID         int `json:"seasonId"`
	Number     int `json:"seasonNumber"`
	Year       int `json:"seasonYear"`
	TotalGames int `json:"totalGamesThisSeason"`
}

Season is the current Starcraft 2 online multiplayer season.

type SwarmLevels

type SwarmLevels struct {
	Level   int   `json:"level"`
	Terran  Level `json:"terran"`
	Zerg    Level `json:"zerg"`
	Protoss Level `json:"protoss"`
}

SwarmLevels represents a character's level for each swarm (race) as well as their overall level.

type User

type User struct {
	ID        int    `json:"id"`
	BattleTag string `json:"battletag"`
}

User represents the user information for a Battle.net account

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL