Skip to content
Closed
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ client, this package provides OAuth endpoints.

**Build Status:** [![Build Status](https://travis-ci.org/mitchellh/go-bnet.svg?branch=master)](https://travis-ci.org/mitchellh/go-bnet)

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

Expand Down Expand Up @@ -40,15 +40,15 @@ oauthCfg := &oauth2.Config{
}
```

Once you have access to the OAuth client, you can initilize the Battle.net
API client:
Once you have access to the OAuth client, you can import and initilize one of the Battle.net
API clients:

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

// Initialize the client
client := bnet.NewClient(oauthClient)
// Initialize the Battle.net Profile API client
client := profile.NewClient("us", authClient)

// ... API calls
```
Expand Down
51 changes: 2 additions & 49 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import (
"strings"
)

const (
libraryVersion = "0.1"
userAgent = "go-bnet/" + libraryVersion
)

// Client is the API client for Battle.net. Create this using NewClient.
// Client is the API client for Battle.net.
// Create this using one of the Battle.net libary's NewClient functions.
// This can also be constructed manually but it isn't recommended.
type Client struct {
// Client is the HTTP client to use for communication.
Expand All @@ -30,49 +26,6 @@ type Client struct {
UserAgent string
}

// 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 NewClient(region string, c *http.Client) *Client {
region = strings.ToLower(region)

if c == nil {
c = http.DefaultClient
}

// Determine the API base URL based on the region
baseURLStr := fmt.Sprintf("https://%s.api.battle.net/", region)
if region == "cn" {
baseURLStr = "https://api.battlenet.com.cn/"
}

baseURL, err := url.Parse(baseURLStr)
if err != nil {
// We panic because we manually construct it above so it should
// never really fail unless the user gives us a REALLY bad region.
panic(err)
}

return &Client{
Client: c,
BaseURL: baseURL,
UserAgent: userAgent,
}
}

func (c *Client) Account() *AccountService {
return &AccountService{client: c}
}

func (c *Client) Profile() *ProfileService {
return &ProfileService{client: c}
}

// 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
Expand Down
28 changes: 0 additions & 28 deletions profile.go

This file was deleted.

9 changes: 7 additions & 2 deletions account.go → profile/account.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package bnet
package profile

import (
"github.com/mitchellh/go-bnet"
)

// AccountService has account-related APIs. See Client.
type AccountService struct {
Expand All @@ -12,7 +16,7 @@ type User struct {
}

// User calls the /account/user endpoint. See Battle.net docs.
func (s *AccountService) User() (*User, *Response, error) {
func (s *AccountService) User() (*User, *bnet.Response, error) {
req, err := s.client.NewRequest("GET", "account/user", nil)
if err != nil {
return nil, nil, err
Expand All @@ -26,3 +30,4 @@ func (s *AccountService) User() (*User, *Response, error) {

return &user, resp, nil
}

2 changes: 1 addition & 1 deletion account_test.go → profile/account_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bnet
package profile

import (
"fmt"
Expand Down
67 changes: 67 additions & 0 deletions profile/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package profile

import (
"fmt"
"strings"
"net/http"
"net/url"
"github.com/mitchellh/go-bnet"
)

const (
libraryVersion = "0.1"
userAgent = "go-bnet/" + libraryVersion
)

// Client is an extension of the github.com/mitchellh/go-bnet API client for Battle.net.
// Create this using NewClient.
// This can also be constructed manually but it isn't recommended.
type Client struct {
bnet.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 NewClient(region string, c *http.Client) *Client {
region = strings.ToLower(region)

if c == nil {
c = http.DefaultClient
}

// Determine the API base URL based on the region
baseURLStr := fmt.Sprintf("https://%s.api.battle.net/", region)
if region == "cn" {
baseURLStr = "https://api.battlenet.com.cn/"
}

baseURL, err := url.Parse(baseURLStr)
if err != nil {
// We panic because we manually construct it above so it should
// never really fail unless the user gives us a REALLY bad region.
panic(err)
}
return &Client{
Client: bnet.Client{
Client: c,
BaseURL: baseURL,
UserAgent: userAgent,
},
}
}

// Hook to Account service.
func (c *Client) Account() *AccountService {
return &AccountService{client: c}
}

// Hook to Profile service.
func (c *Client) Profile() *ProfileService {
return &ProfileService{client: c}
}
44 changes: 44 additions & 0 deletions profile/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package profile

import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)

var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux

// client is the Battle.net client being tested.
client *Client

// server is a test HTTP server used to provide mock API responses.
server *httptest.Server
)

// setup sets up a test HTTP server along with a bnet.Client that is
// configured to talk to that test server. Tests should register handlers on
// mux which provide mock responses for the API method being tested.
func setup() {
// test server
mux = http.NewServeMux()
server = httptest.NewServer(mux)

// bnet client configured to use test server
client = NewClient("us", nil)
url, _ := url.Parse(server.URL)
client.BaseURL = url
}

// teardown closes the test HTTP server.
func teardown() {
server.Close()
}

func testMethod(t *testing.T, r *http.Request, want string) {
if got := r.Method; got != want {
t.Errorf("Request method: %v, want %v", got, want)
}
}
57 changes: 57 additions & 0 deletions profile/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package profile

import (
"github.com/mitchellh/go-bnet"
"github.com/mitchellh/go-bnet/sc2"
"github.com/mitchellh/go-bnet/wow"
)

// ProfileService has OAuth Profile APIs. See Client.
type ProfileService struct {
client *Client
}

// SC2Profile represents the profile information for a user's Starcraft 2 profile.
type SC2Profile struct {
Characters []sc2.SC2Character `json:"characters"`
}

// WoWProfile is a collection of a user's World of Warcraft characters.
type WoWProfile struct {
Characters []wow.WoWCharacter `json:"characters"`
}

// SC2() calls the /sc2/profile/user endpoint. This endpoint uses OAuth2
// to retrieve a user's Starcraft 2 profile. See Battle.net docs.
func (s *ProfileService) SC2() (*SC2Profile, *bnet.Response, error) {
req, err := s.client.NewRequest("GET", "sc2/profile/user", nil)
if err != nil {
return nil, nil, err
}

var profile SC2Profile
resp, err := s.client.Do(req, &profile)
if err != nil {
return nil, resp, err
}

return &profile, resp, nil
}

// WoW() calls the /wow/user/characters endpoint. This endpoint uses OAuth2
// to retrieve a user's World of Warcraft character list. See Battle.net docs.
func (s *ProfileService) WoW() (*WoWProfile, *bnet.Response, error) {
req, err := s.client.NewRequest("GET", "wow/user/characters", nil)
if err != nil {
return nil, nil, err
}

var profile WoWProfile
resp, err := s.client.Do(req, &profile)
if err != nil {
return nil, resp, err
}

return &profile, resp, nil
}

Loading