-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdatasource.go
201 lines (157 loc) · 5.21 KB
/
datasource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"github.com/leighmacdonald/steamid/v4/steamid"
"github.com/leighmacdonald/steamweb/v2"
)
type DataSource interface {
summaries(ctx context.Context, steamIDs steamid.Collection) ([]steamweb.PlayerSummary, error)
Bans(ctx context.Context, steamIDs steamid.Collection) ([]steamweb.PlayerBanState, error)
friends(ctx context.Context, steamIDs steamid.Collection) (FriendMap, error)
sourceBans(ctx context.Context, steamIDs steamid.Collection) (SourcebansMap, error)
}
// LocalDataSource implements a local only data source that can be used for people who do not want to use the bd-api
// service, or if it is otherwise down.
type LocalDataSource struct{}
func (n LocalDataSource) summaries(ctx context.Context, steamIDs steamid.Collection) ([]steamweb.PlayerSummary, error) {
summaries, errSummaries := steamweb.PlayerSummaries(ctx, steamIDs)
if errSummaries != nil {
return nil, errors.Join(errSummaries, errFetchSummaries)
}
return summaries, nil
}
func (n LocalDataSource) Bans(ctx context.Context, steamIDs steamid.Collection) ([]steamweb.PlayerBanState, error) {
bans, errBans := steamweb.GetPlayerBans(ctx, steamIDs)
if errBans != nil {
return nil, errors.Join(errBans, errFetchBans)
}
return bans, nil
}
func (n LocalDataSource) friends(ctx context.Context, steamIDs steamid.Collection) (FriendMap, error) {
var (
resp = FriendMap{}
waitGroup = &sync.WaitGroup{}
mutex = &sync.RWMutex{}
)
for _, steamID := range steamIDs {
waitGroup.Add(1)
go func(sid steamid.SteamID) {
defer waitGroup.Done()
var (
friends []steamweb.Friend
errSummaries error
)
friends, errSummaries = steamweb.GetFriendList(ctx, sid)
if errSummaries != nil {
friends = []steamweb.Friend{}
}
mutex.Lock()
defer mutex.Unlock()
if friends == nil {
resp[sid.String()] = []steamweb.Friend{}
} else {
resp[sid.String()] = friends
}
}(steamID)
}
waitGroup.Wait()
return resp, nil
}
func (n LocalDataSource) sourceBans(_ context.Context, steamIDs steamid.Collection) (SourcebansMap, error) {
dummy := SourcebansMap{}
for _, sid := range steamIDs {
dummy[sid.String()] = []SbBanRecord{}
}
return dummy, nil
}
func createLocalDataSource(key string) (*LocalDataSource, error) {
if errKey := steamweb.SetKey(key); errKey != nil {
return nil, errors.Join(errKey, errAPIKey)
}
return &LocalDataSource{}, nil
}
func newDataSource(userSettings userSettings) (DataSource, error) { //nolint:ireturn
if userSettings.BdApiEnabled {
return createAPIDataSource(userSettings.BdApiAddress)
}
return createLocalDataSource(userSettings.ApiKey)
}
// steamIDStringList transforms a steamid.Collection into a comma separated list of SID64 strings.
func steamIDStringList(collection steamid.Collection) string {
ids := make([]string, len(collection))
for index, steamID := range collection {
ids[index] = steamID.String()
}
return strings.Join(ids, ",")
}
const APIDataSourceDefaultAddress = "https://bd-api.roto.lol"
// APIDataSource implements a client for the remote bd-api service.
type APIDataSource struct {
baseURL string
client *http.Client
}
func createAPIDataSource(sourceURL string) (*APIDataSource, error) {
if sourceURL == "" {
sourceURL = APIDataSourceDefaultAddress
}
_, errParse := url.Parse(sourceURL)
if errParse != nil {
return nil, errors.Join(errParse, errDataSourceAPIAddr)
}
return &APIDataSource{baseURL: sourceURL, client: &http.Client{}}, nil
}
func (n APIDataSource) url(path string, collection steamid.Collection) string {
return fmt.Sprintf("%s%s?steamids=%s", n.baseURL, path, steamIDStringList(collection))
}
func (n APIDataSource) get(ctx context.Context, path string, results any) error {
req, errReq := http.NewRequestWithContext(ctx, http.MethodGet, path, nil)
if errReq != nil {
return errors.Join(errReq, errCreateRequest)
}
resp, errResp := n.client.Do(req)
if errResp != nil {
return errors.Join(errResp, errPerformRequest)
}
defer func() {
_ = resp.Body.Close()
}()
if errJSON := json.NewDecoder(resp.Body).Decode(&results); errJSON != nil {
return errors.Join(errJSON, errDecodeResponse)
}
return nil
}
func (n APIDataSource) summaries(ctx context.Context, steamIDs steamid.Collection) ([]steamweb.PlayerSummary, error) {
var out []steamweb.PlayerSummary
if errGet := n.get(ctx, n.url("/summary", steamIDs), &out); errGet != nil {
return nil, errGet
}
return out, nil
}
func (n APIDataSource) Bans(ctx context.Context, steamIDs steamid.Collection) ([]steamweb.PlayerBanState, error) {
var out []steamweb.PlayerBanState
if errGet := n.get(ctx, n.url("/bans", steamIDs), &out); errGet != nil {
return nil, errGet
}
return out, nil
}
func (n APIDataSource) friends(ctx context.Context, steamIDs steamid.Collection) (FriendMap, error) {
var out FriendMap
if errGet := n.get(ctx, n.url("/friends", steamIDs), &out); errGet != nil {
return nil, errGet
}
return out, nil
}
func (n APIDataSource) sourceBans(ctx context.Context, steamIDs steamid.Collection) (SourcebansMap, error) {
var out SourcebansMap
if errGet := n.get(ctx, n.url("/sourcebans", steamIDs), &out); errGet != nil {
return nil, errGet
}
return out, nil
}