Files
oslstats/pkg/slapshotapi/slapid.go
2026-02-21 14:45:14 +11:00

54 lines
1005 B
Go

package slapshotapi
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
)
type endpointSteamID struct {
steamID string
}
func getEndpointSteamID(steamID string) *endpointSteamID {
return &endpointSteamID{
steamID: steamID,
}
}
func (ep *endpointSteamID) path() string {
return fmt.Sprintf("/api/public/players/steam/%s", ep.steamID)
}
func (ep *endpointSteamID) method() string {
return "GET"
}
type idresp struct {
ID uint32 `json:"id"`
}
var ErrNoSlapID error = errors.New("slapID not found")
// GetSlapID returns the slapshot ID of the steam user.
func (c *SlapAPI) GetSlapID(
ctx context.Context,
steamid string,
) (uint32, error) {
endpoint := getEndpointSteamID(steamid)
data, err := c.request(ctx, endpoint)
if err != nil {
if strings.Contains(err.Error(), "404") {
return 0, ErrNoSlapID
}
return 0, errors.Wrap(err, "c.request")
}
resp, err := unmarshal[idresp](data)
if err != nil {
return 0, errors.Wrap(err, "unmarshal")
}
return resp.ID, nil
}