Skip to content
Closed
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
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (c *Client) Profile() *ProfileService {
return &ProfileService{client: c}
}

func (c *Client) SC2() *SC2Service {
return &SC2Service{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
22 changes: 22 additions & 0 deletions profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ type SC2Profile struct {
Characters []SC2Character `json:"characters"`
}

// WoWProfile is a collection of a user's World of Warcraft characters.
type WoWProfile struct {
Characters []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, *Response, error) {
Expand All @@ -26,3 +31,20 @@ func (s *ProfileService) SC2() (*SC2Profile, *Response, error) {

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, *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
}
62 changes: 61 additions & 1 deletion profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import(
"net/http"
"testing"
"reflect"
"github.com/nmccrory/go-bnet/sc2"
"github.com/nmccrory/go-bnet/wow"
)
const sc2ProfileResp = `{ "characters":
[{
Expand Down Expand Up @@ -72,6 +74,30 @@ const sc2ProfileResp = `{ "characters":
}
}]
}`
const wowCharactersResp = `{ "characters":
[{
"name": "foobar",
"realm": "foobar",
"battleGroup": "Foo",
"class": 1,
"race": 1,
"gender": 0,
"level": 99,
"achievementPoints": 1234,
"thumbnail": "foobar/123/avatar.jpg",
"spec": {
"name": "foobar",
"role": "foobar",
"backgroundImage": "foo-bar",
"icon": "foo_bar",
"description": "Quick brown fox jumps over the lazy dog.",
"order": 1
},
"guild": "Foo",
"guildRealm": "foobar",
"lastModified": 1234567
}]
}`

func TestProfileService_SC2(t *testing.T) {
setup()
Expand All @@ -85,7 +111,7 @@ func TestProfileService_SC2(t *testing.T) {
t.Fatalf("err: %s", err)
}
if actual.Characters == nil {
t.Fatal("err: Profile is empty -> &SC2Profile{Characters: []SC2Character{}(nil)}")
t.Fatal("err: This user has no Starcraft 2 profile.")
}
want := SC2Character{ID: 1234567,
Realm: 1,
Expand All @@ -110,3 +136,37 @@ func TestProfileService_SC2(t *testing.T) {
t.Fatalf("returned %+v, want %+v", actual.Characters[0], want)
}
}

func TestProfileService_WoW(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/wow/user/characters", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, wowCharactersResp)
})
actual, _, err := client.Profile().WoW()
if err != nil {
t.Fatalf("err: %s", err)
}
if actual.Characters == nil {
t.Fatal("err: This user has no World of Warcraft characters.")
}
want := WoWCharacter{
Name: "foobar",
Realm: "foobar",
BattleGroup: "Foo",
Class: 1,
Race: 1,
Gender: 0,
Level: 99,
AchievementPoints: 1234,
Thumbnail: "foobar/123/avatar.jpg",
Spec: wow.Spec{"foobar", "foobar", "foo-bar", "foo_bar",
"Quick brown fox jumps over the lazy dog.", 1},
Guild: "Foo",
GuildRealm: "foobar",
LastModified: 1234567 }
if !reflect.DeepEqual(actual.Characters[0], want) {
t.Fatalf("returned %+v, want %+v", actual.Characters[0], want)
}
}
4 changes: 3 additions & 1 deletion sc2.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bnet

// SC2Service has Starcraft2-related APIs. See Client.
// SC2Service has Startcraft 2 related APIs.
type SC2Service struct {
client *Client
}
Expand Down Expand Up @@ -92,3 +92,5 @@ type SC2Character struct {
Achievements Achievements `json:"achievements"`
}



25 changes: 25 additions & 0 deletions sc2/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sc2

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

// Profile() calls the /sc2/profile/:id/:region/:name endpoint to
// retrieve a user's Starcraft 2 profile. See Battle.net docs.
func (s *SC2Service) Profile(id int, region int, name string) (*bnet.SC2Profile, *bnet.Response, error) {
endpoint := fmt.Sprintf("sc2/profile/%d/%d/%s", id, region, name)
req, err := s.client.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, nil, err
}

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

return &profile, resp, nil
}

117 changes: 117 additions & 0 deletions sc2/profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package sc2

import (
"fmt"
"net/http"
"reflect"
"testing"
"github.com/gorilla/mux

"github.com/nmccrory/go-bnet/sc2"
"github.com/aws/aws-sdk-go/aws/client"
)

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 TestSC2Service_Profile(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/account/user", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, sc2ProfileResp)
})

actual, _, err := client.Account().User()
if err != nil {
t.Fatalf("err: %s", err)
}

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, want) {
t.Fatalf("returned %+v, want %+v", actual, want)
}
}
29 changes: 29 additions & 0 deletions wow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package bnet

// Spec represents a character's class spec
// (e.g. Frost Mage, Marksmanship Hunter, etc.)
type Spec struct {
Name string `json:"name"`
Role string `json:"role"`
BackgroundImage string `json:"backgroundImage"`
Icon string `json:"icon"`
Description string `json:"description"`
Order int `json:"order"`
}

// WoWCharacter represents a single World of Warcraft character.
type WoWCharacter struct {
Name string `json:"name"`
Realm string `json:"realm"`
BattleGroup string `json:"battlegroup"`
Class int `json:"class"`
Race int `json:"race"`
Gender int `json:"gender"`
Level int `json:"level"`
AchievementPoints int `json:"achievementPoints"`
Thumbnail string `json:"thumbnail"`
Spec Spec `json:"spec"`
Guild string `json:"guild"`
GuildRealm string `json:"guildRealm"`
LastModified int `json:"lastModified"`
}