Files
games-wishlist/steam.go
2026-02-15 21:48:01 +03:00

59 lines
1.4 KiB
Go

package main
import (
"encoding/json"
"fmt"
"game-wishlist/model"
"net/http"
"net/url"
"strconv"
)
type SteamAppDetails = map[int]struct {
Success bool `json:"success"`
Data struct {
Type string `json:"type"`
Name string `json:"name"`
SteamAppId int `json:"steam_appid"`
HeaderImage string `json:"header_image"`
ReleaseDate struct {
Date string `json:"date"`
} `json:"release_date"`
} `json:"data"`
}
func getSteamGameDetails(appId int) (*model.Game, error) {
req, err := http.NewRequest("GET", "https://store.steampowered.com/api/appdetails", nil)
if err != nil {
return nil, fmt.Errorf("failed to construct request for game details: %v", err)
}
q := make(url.Values)
q.Set("appids", strconv.Itoa(appId))
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch game details: %v", err)
}
defer resp.Body.Close()
appDetails := SteamAppDetails{}
if err := json.NewDecoder(resp.Body).Decode(&appDetails); err != nil {
return nil, fmt.Errorf("failed to decode game details response: %v", err)
}
gameDetails, ok := appDetails[appId]
if !ok {
return nil, fmt.Errorf("game not found")
}
return &model.Game{
Name: gameDetails.Data.Name,
SteamAppID: gameDetails.Data.SteamAppId,
Image: gameDetails.Data.HeaderImage,
ReleaseDate: gameDetails.Data.ReleaseDate.Date,
}, nil
}