Skip to content

Commit

Permalink
feat: convert steam API over to use generic request handler
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Dec 1, 2024
1 parent eb24644 commit 5c06654
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
3 changes: 2 additions & 1 deletion internal/apis/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ func SendRequest[T any](req *http.Request) (T, error) {
}
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf(
"status code of %d returned from API. Code of 200 expected",
"status code of %d returned from API. Code of 200 expected from %s",
resp.StatusCode,
req.URL.String(),
)
if resp.StatusCode == http.StatusBadGateway ||
resp.StatusCode == http.StatusGatewayTimeout ||
Expand Down
30 changes: 6 additions & 24 deletions internal/apis/steam/games.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package steam

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"sort"
"time"

"github.com/gleich/lcp-v2/internal/apis"
"github.com/gleich/lcp-v2/internal/secrets"
"github.com/gleich/lumber/v3"
)
Expand Down Expand Up @@ -47,33 +46,16 @@ func fetchRecentlyPlayedGames() ([]game, error) {
"include_appinfo": {"true"},
"format": {"json"},
}
resp, err := http.Get(
"https://api.steampowered.com/IPlayerService/GetOwnedGames/v1?" + params.Encode(),
req, err := http.NewRequest("GET",
"https://api.steampowered.com/IPlayerService/GetOwnedGames/v1?"+params.Encode(), nil,
)
if err != nil {
lumber.Error(err, "sending request for owned games failed")
lumber.Error(err, "failed to create request for steam API owned games")
return nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
ownedGames, err := apis.SendRequest[ownedGamesResponse](req)
if err != nil {
lumber.Error(err, "reading response body for owned games failed")
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
"%d status code when trying to get owned games: %s\n",
resp.StatusCode,
string(body),
)
}

var ownedGames ownedGamesResponse
err = json.Unmarshal(body, &ownedGames)
if err != nil {
lumber.Error(err, "failed to parse json for owned games")
lumber.Debug("body:", string(body))
lumber.Error(err, "sending request for owned games failed")
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/apis/strava/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func fetchActivities(minioClient minio.Client, tokens tokens) ([]activity, error
)
if err != nil {
lumber.Error(err, "failed to send request to Strava API to get activities")
return []activity{}, err
return nil, err
}

var activities []activity
Expand Down

0 comments on commit 5c06654

Please sign in to comment.