-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_player.go
More file actions
54 lines (48 loc) · 1.22 KB
/
api_player.go
File metadata and controls
54 lines (48 loc) · 1.22 KB
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
package sova
import (
"context"
"errors"
"fmt"
"strconv"
"unicode/utf8"
)
type ErrCannotFindPlayer struct {
AsID bool
Player string
}
func (e ErrCannotFindPlayer) Error() string {
if e.Player == "" {
return "cannot find player"
}
return fmt.Sprintf("cannot find player: %s", e.Player)
}
// Player ...
//
// /api/player/{id}
func (api *API) Player(ctx context.Context, id int) (resp *PlayerResponse, err error) {
resp, err = getAndUnmarshal[*PlayerResponse](api, ctx, f("player/%d", id))
if err != nil {
return nil, err
}
if resp == nil {
return nil, ErrCannotFindPlayer{AsID: true, Player: strconv.Itoa(id)}
}
return resp, nil
}
var ErrNicknameMustBeTwoChars = errors.New("nickname must be at least 2 characters long")
// PlayerSearch searches for a player with a specific query.
//
// /api/player/search/{query}
func (api *API) PlayerSearch(ctx context.Context, query string) (resp PlayerSearchResponse, err error) {
if utf8.RuneCountInString(query) < 2 {
return nil, ErrNicknameMustBeTwoChars
}
resp, err = getAndUnmarshal[PlayerSearchResponse](api, ctx, f("player/search/%s", query))
if err != nil {
return nil, err
}
if len(resp) == 0 {
return nil, ErrCannotFindPlayer{Player: query}
}
return resp, nil
}