Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ import (
"bytes"
"encoding/json"
"io"
"fmt"
"strings"
"io/ioutil"
"net/http"
"net/url"
)


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

// 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.
Expand All @@ -24,6 +32,42 @@ 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,
}
}

// 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
2 changes: 1 addition & 1 deletion profile/account.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package profile

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

// AccountService has account-related APIs. See Client.
Expand Down
2 changes: 1 addition & 1 deletion profile/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"net/http"
"net/url"
"github.com/mitchellh/go-bnet"
"github.com/nmccrory/go-bnet"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion profile/profile.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package profile

import (
"github.com/nmccrory/go-bnet"
"github.com/mitchellh/go-bnet"
"github.com/nmccrory/go-bnet/sc2"
"github.com/nmccrory/go-bnet/wow"
)
Expand Down
5 changes: 5 additions & 0 deletions sc2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ func NewClient(region string, c *http.Client) *Client {
},
}
}

// Profile is the hook to a Starcraft 2 Profile service.
func (c *Client) Profile() *ProfileService {
return &ProfileService{client: c}
}
27 changes: 27 additions & 0 deletions sc2/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package sc2

import (
"fmt"
"github.com/nmccrory/go-bnet"
profile "github.com/nmccrory/go-bnet/profile"
)

type ProfileService struct {
client *Client
}

func (s *ProfileService) Profile(id int, region int, name string) (*profile.SC2Profile, *bnet.Response, error) {
url := fmt.Sprintf("profile/%d/%d/%s", id, region, name)
req, err := s.client.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, err
}

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

return &profile, resp, nil
}
114 changes: 114 additions & 0 deletions sc2/profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package sc2

import (
"fmt"
"net/http"
"testing"
"reflect"
)

const sc2ProfileResp = `{ "characters":
[{
"id": 1234567,
"realm": 1,
"displayName": "foobar",
"clanName": "foobar",
"clanTag": "foobar",
"profilePath": "/profile/1234567/1/foobar/",
"portrait": {
"x": -10,
"y": -10,
"w": 10,
"h": 10,
"offset": 10,
"url": "http://media.blizzard.com/sc2/portraits/dummy.jpg"
},
"career": {
"primaryRace": "PROTOSS",
"terranWins": 0,
"protossWins": 0,
"zergWins": 0,
"highest1v1Rank": "DIAMOND",
"seasonTotalGames": 0,
"careerTotalGames": 100
},
"swarmLevels": {
"level": 10,
"terran": {
"level": 1,
"totalLevelXP": 1000,
"currentLevelXP": 0
},
"zerg": {
"level": 2,
"totalLevelXP": 1000,
"currentLevelXP": 0
},
"protoss": {
"level": 3,
"totalLevelXP": 1000,
"currentLevelXP": 0
}
},
"campaign": {},
"season": {
"seasonId": 123,
"seasonNumber": 1,
"seasonYear": 2017,
"totalGamesThisSeason": 0
},
"rewards": {
"selected": [12345678, 12345678],
"earned": [12345678, 12345678]
},
"achievements": {
"points": {
"totalPoints": 1234,
"categoryPoints": {}
},
"achievements": [{
"achievementId": 123456789,
"completionDate": 123456789
}]
}
}]
}`

func TestProfileService_Profile(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/sc2/1234567/1/foobar", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, sc2ProfileResp)
})
actual, _, err := client.Profile().Profile(1234567,1, "foobar" )
if err != nil {
t.Fatalf("err: %s", err)
}
if actual.Characters == nil {
t.Fatal("err: This user has no Starcraft 2 profile.")
}
want := SC2Character{
ID: 1234567,
Realm: 1,
DisplayName: "foobar",
ClanName: "foobar",
ClanTag: "foobar",
ProfilePath: "/profile/1234567/1/foobar/",
Portrait: CharacterImage{-10, -10, 10, 10, 10,
"http://media.blizzard.com/sc2/portraits/dummy.jpg"},
Career: Career{"PROTOSS", 0, 0, 0,
"DIAMOND", 0, 100},
SwarmLevels: SwarmLevels{10,
Level{1, 1000, 0},
Level{2, 1000, 0},
Level{3, 1000, 0}},
Season: Season{123, 1, 2017, 0},
Rewards: Rewards{[]int{12345678, 12345678}, []int{12345678, 12345678}},
Achievements: Achievements{Points{1234},
[]Achievement{Achievement{123456789, 123456789}}},
}
if !reflect.DeepEqual(actual.Characters[0], want) {
t.Fatalf("returned %+v, want %+v", actual.Characters[0], want)
}
}