56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package slapshotapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"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 := idresp{}
|
|
err = json.Unmarshal(data, &resp)
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, "json.Unmarshal")
|
|
}
|
|
return resp.ID, nil
|
|
}
|