Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c9b54b4
Added OAuth Profile endpoint for World of Warcraft.
nmccrory Jun 12, 2017
9503e33
Created test for Profile().WoW()
nmccrory Jun 12, 2017
ec245e4
moved concerns for SC2 and WoW to their own directories
nmccrory Jun 12, 2017
fbd2cdb
profile and profile_test now contain import statements for sc2 and wo…
nmccrory Jun 12, 2017
dd18d61
Moved sc2.go and wow.go to main bnet package.
nmccrory Jun 13, 2017
4af676b
Moved SC2Service into the main go-bnet package as part of sc2.go
nmccrory Jun 13, 2017
414a0b1
Added WoW profile retrieval.
nmccrory Jun 21, 2017
6e7d87e
Merge pull request #11 from nmccrory/dev
nmccrory Jun 21, 2017
0fb58c9
Profile, SC2, and WoW have all been moved into their own packages.
nmccrory Jun 22, 2017
3bd761d
Merge pull request #12 from nmccrory/dev
nmccrory Jun 22, 2017
8b23e01
removed unused imports inside of client.go
nmccrory Jun 25, 2017
d3c71ca
Merge pull request #13 from nmccrory/dev
nmccrory Jun 25, 2017
96141c9
Cleaning up workspace to begin development from nmccrory/go-bnet fork
nmccrory Jun 25, 2017
5c13bc2
Merge pull request #14 from nmccrory/dev
nmccrory Jun 25, 2017
4e6f3f8
Created a close to working service for profiles in the sc2 package. A…
nmccrory Jul 7, 2017
7c4d91f
Merge pull request #15 from nmccrory/dev
nmccrory Jul 7, 2017
4cb8b48
The packaging hierarchy now completely avoids a infinite loop. Create…
nmccrory Jul 7, 2017
0f18d66
Lining up all dependencies to point to my personal fork
nmccrory Jul 7, 2017
80d6776
updating travis ci
nmccrory Jul 7, 2017
77634ed
Replaced all imports of github.com/nmccrory/go-bnet/.. with imports f…
nmccrory Jul 7, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: go

go:
- 1.6
- 1.8
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
21 changes: 8 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ package bnet
import (
"bytes"
"encoding/json"
"fmt"
"io"
"fmt"
"strings"
"io/ioutil"
"net/http"
"net/url"
"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 Down Expand Up @@ -57,22 +59,15 @@ func NewClient(region string, c *http.Client) *Client {
// 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 Expand Up @@ -130,7 +125,7 @@ func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
if err := CheckError(resp); err != nil {
return response, err
}

fmt.Print(response)
if v != nil {
if w, ok := v.(io.Writer); ok {
io.Copy(w, resp.Body)
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.Character `json:"characters"`
}

// WoWProfile is a collection of a user's World of Warcraft characters.
type WoWProfile struct {
Characters []wow.Character `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