Skip to content

Commit 7c4d91f

Browse files
authored
Merge pull request #15 from nmccrory/dev
Created a close to working service for profiles in the sc2 package. A…
2 parents 5c13bc2 + 4e6f3f8 commit 7c4d91f

File tree

7 files changed

+193
-3
lines changed

7 files changed

+193
-3
lines changed

client.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ import (
44
"bytes"
55
"encoding/json"
66
"io"
7+
"fmt"
8+
"strings"
79
"io/ioutil"
810
"net/http"
911
"net/url"
1012
)
1113

14+
15+
const (
16+
libraryVersion = "0.1"
17+
userAgent = "go-bnet/" + libraryVersion
18+
)
19+
1220
// Client is the API client for Battle.net.
1321
// Create this using one of the Battle.net libary's NewClient functions.
1422
// This can also be constructed manually but it isn't recommended.
@@ -24,6 +32,42 @@ type Client struct {
2432
UserAgent string
2533
}
2634

35+
// NewClient creates a new Battle.net client.
36+
//
37+
// region must be a valid Battle.net region. This will not validate it
38+
// is valid.
39+
//
40+
// The http.Client argument should usually be retrieved via the
41+
// oauth2 Go library NewClient function. It must be a client that
42+
// automatically injects authentication details into requests.
43+
func NewClient(region string, c *http.Client) *Client {
44+
region = strings.ToLower(region)
45+
46+
if c == nil {
47+
c = http.DefaultClient
48+
}
49+
50+
// Determine the API base URL based on the region
51+
baseURLStr := fmt.Sprintf("https://%s.api.battle.net/", region)
52+
if region == "cn" {
53+
baseURLStr = "https://api.battlenet.com.cn/"
54+
}
55+
56+
baseURL, err := url.Parse(baseURLStr)
57+
if err != nil {
58+
// We panic because we manually construct it above so it should
59+
// never really fail unless the user gives us a REALLY bad region.
60+
panic(err)
61+
}
62+
return &Client{
63+
Client: c,
64+
65+
BaseURL: baseURL,
66+
67+
UserAgent: userAgent,
68+
}
69+
}
70+
2771
// NewRequest creates an API request. A relative URL can be provided in urlStr,
2872
// in which case it is resolved relative to the BaseURL of the Client.
2973
// Relative URLs should always be specified without a preceding slash. If

profile/account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package profile
22

33
import (
4-
"github.com/mitchellh/go-bnet"
4+
"github.com/nmccrory/go-bnet"
55
)
66

77
// AccountService has account-related APIs. See Client.

profile/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"strings"
66
"net/http"
77
"net/url"
8-
"github.com/mitchellh/go-bnet"
8+
"github.com/nmccrory/go-bnet"
99
)
1010

1111
const (

profile/profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package profile
22

33
import (
4-
"github.com/nmccrory/go-bnet"
4+
"github.com/mitchellh/go-bnet"
55
"github.com/nmccrory/go-bnet/sc2"
66
"github.com/nmccrory/go-bnet/wow"
77
)

sc2/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ func NewClient(region string, c *http.Client) *Client {
5454
},
5555
}
5656
}
57+
58+
// Profile is the hook to a Starcraft 2 Profile service.
59+
func (c *Client) Profile() *ProfileService {
60+
return &ProfileService{client: c}
61+
}

sc2/profile.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package sc2
2+
3+
import (
4+
"fmt"
5+
"github.com/nmccrory/go-bnet"
6+
profile "github.com/nmccrory/go-bnet/profile"
7+
)
8+
9+
type ProfileService struct {
10+
client *Client
11+
}
12+
13+
func (s *ProfileService) Profile(id int, region int, name string) (*profile.SC2Profile, *bnet.Response, error) {
14+
url := fmt.Sprintf("profile/%d/%d/%s", id, region, name)
15+
req, err := s.client.NewRequest("GET", url, nil)
16+
if err != nil {
17+
return nil, nil, err
18+
}
19+
20+
var profile profile.SC2Profile
21+
resp, err := s.client.Do(req, &profile)
22+
if err != nil {
23+
return nil, resp, err
24+
}
25+
26+
return &profile, resp, nil
27+
}

sc2/profile_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package sc2
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
"reflect"
8+
)
9+
10+
const sc2ProfileResp = `{ "characters":
11+
[{
12+
"id": 1234567,
13+
"realm": 1,
14+
"displayName": "foobar",
15+
"clanName": "foobar",
16+
"clanTag": "foobar",
17+
"profilePath": "/profile/1234567/1/foobar/",
18+
"portrait": {
19+
"x": -10,
20+
"y": -10,
21+
"w": 10,
22+
"h": 10,
23+
"offset": 10,
24+
"url": "http://media.blizzard.com/sc2/portraits/dummy.jpg"
25+
},
26+
"career": {
27+
"primaryRace": "PROTOSS",
28+
"terranWins": 0,
29+
"protossWins": 0,
30+
"zergWins": 0,
31+
"highest1v1Rank": "DIAMOND",
32+
"seasonTotalGames": 0,
33+
"careerTotalGames": 100
34+
},
35+
"swarmLevels": {
36+
"level": 10,
37+
"terran": {
38+
"level": 1,
39+
"totalLevelXP": 1000,
40+
"currentLevelXP": 0
41+
},
42+
"zerg": {
43+
"level": 2,
44+
"totalLevelXP": 1000,
45+
"currentLevelXP": 0
46+
},
47+
"protoss": {
48+
"level": 3,
49+
"totalLevelXP": 1000,
50+
"currentLevelXP": 0
51+
}
52+
},
53+
"campaign": {},
54+
"season": {
55+
"seasonId": 123,
56+
"seasonNumber": 1,
57+
"seasonYear": 2017,
58+
"totalGamesThisSeason": 0
59+
},
60+
"rewards": {
61+
"selected": [12345678, 12345678],
62+
"earned": [12345678, 12345678]
63+
},
64+
"achievements": {
65+
"points": {
66+
"totalPoints": 1234,
67+
"categoryPoints": {}
68+
},
69+
"achievements": [{
70+
"achievementId": 123456789,
71+
"completionDate": 123456789
72+
}]
73+
}
74+
}]
75+
}`
76+
77+
func TestProfileService_Profile(t *testing.T) {
78+
setup()
79+
defer teardown()
80+
mux.HandleFunc("/sc2/1234567/1/foobar", func(w http.ResponseWriter, r *http.Request) {
81+
testMethod(t, r, "GET")
82+
fmt.Fprint(w, sc2ProfileResp)
83+
})
84+
actual, _, err := client.Profile().Profile(1234567,1, "foobar" )
85+
if err != nil {
86+
t.Fatalf("err: %s", err)
87+
}
88+
if actual.Characters == nil {
89+
t.Fatal("err: This user has no Starcraft 2 profile.")
90+
}
91+
want := SC2Character{
92+
ID: 1234567,
93+
Realm: 1,
94+
DisplayName: "foobar",
95+
ClanName: "foobar",
96+
ClanTag: "foobar",
97+
ProfilePath: "/profile/1234567/1/foobar/",
98+
Portrait: CharacterImage{-10, -10, 10, 10, 10,
99+
"http://media.blizzard.com/sc2/portraits/dummy.jpg"},
100+
Career: Career{"PROTOSS", 0, 0, 0,
101+
"DIAMOND", 0, 100},
102+
SwarmLevels: SwarmLevels{10,
103+
Level{1, 1000, 0},
104+
Level{2, 1000, 0},
105+
Level{3, 1000, 0}},
106+
Season: Season{123, 1, 2017, 0},
107+
Rewards: Rewards{[]int{12345678, 12345678}, []int{12345678, 12345678}},
108+
Achievements: Achievements{Points{1234},
109+
[]Achievement{Achievement{123456789, 123456789}}},
110+
}
111+
if !reflect.DeepEqual(actual.Characters[0], want) {
112+
t.Fatalf("returned %+v, want %+v", actual.Characters[0], want)
113+
}
114+
}

0 commit comments

Comments
 (0)