Try to fetch steam game count if requested and cache it
Build, Test & Push Core Service / quality-check (push) Successful in 44s
Build, Test & Push Core Service / docker (push) Successful in 1m48s

This commit is contained in:
Andreas Dahm committed 2026-04-28 15:43:02 +02:00
1 parent 2e3896b00d
commit 697fdcb208
9 files changed
+125 -10

No files matched your search

@@ -0,0 +1,24 @@
package core_service.configuration;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager("steamGameCount");
cacheManager.setCaffeine(Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.maximumSize(1000));
return cacheManager;
}
}
@@ -47,10 +47,11 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
try {
final String userEmail = jwtService.extractEmail(jwt);
final String userId = jwtService.extractUserId(jwt);
final Long steamId = jwtService.extractSteamId(jwt);
if (userEmail != null && userId != null && SecurityContextHolder.getContext().getAuthentication() == null) {
UserPrincipal principal = new UserPrincipal(userId, userEmail);
UserPrincipal principal = new UserPrincipal(userId, userEmail, steamId);
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
principal,
null,
@@ -1,6 +1,5 @@
package core_service.configuration.security;
import core_service.shared.security.UserPrincipal;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.stereotype.Service;
@@ -35,6 +34,15 @@ public class JwtService {
.get("userId", String.class);
}
public Long extractSteamId(String token) {
return Jwts.parser()
.verifyWith(getSigningKey())
.build()
.parseSignedClaims(token)
.getPayload()
.get("steamId", Long.class);
}
public boolean isTokenValid(String token) {
try {
Jwts.parser()
@@ -0,0 +1,30 @@
package core_service.modules.steam_adapter.api;
import core_service.modules.steam_adapter.service.SteamApiService;
import core_service.shared.security.UserPrincipal;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/api/steam")
@RequiredArgsConstructor
public class SteamController {
private final SteamApiService steamApiService;
@GetMapping("/games-count")
public ResponseEntity<?> getGamesCount(@AuthenticationPrincipal UserPrincipal principal) {
if (principal.steamId() == null) {
return ResponseEntity.ok(Map.of("count", 0, "hasSteamId", false));
}
int count = steamApiService.getOwnedGamesCount(String.valueOf(principal.steamId()));
return ResponseEntity.ok(Map.of("count", count, "hasSteamId", true));
}
}
@@ -6,7 +6,7 @@ import java.util.List;
public record SteamOwnedGamesResponse(OwnedGamesResponse response) {
public record OwnedGamesResponse(
@JsonProperty("game_count") int gameCount,
@JsonProperty("game_count") Integer gameCount,
List<Game> games
) {}
@@ -6,6 +6,7 @@ import core_service.modules.steam_adapter.api.dto.response.SteamProfileAndGamesR
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
@@ -23,6 +24,35 @@ public class SteamApiService {
@Value("${app.steam.api-key}")
private String apiKey;
@Cacheable(value = "steamGameCount", key = "#steamId")
public int getOwnedGamesCount(String steamId) {
try {
log.info("Fetching owned games count for SteamID: {}", steamId);
SteamOwnedGamesResponse response = steamRestClient.get()
.uri(uriBuilder -> uriBuilder
.path("/IPlayerService/GetOwnedGames/v0001/")
.queryParam("key", apiKey)
.queryParam("steamid", steamId)
.queryParam("include_appinfo", 0)
.queryParam("include_played_free_games", 1)
.build())
.retrieve()
.body(SteamOwnedGamesResponse.class);
log.info("SteamOwnedGamesResponse: {}", response);
if (response != null && response.response() != null) {
return response.response().gameCount() != null ? response.response().gameCount() : 0;
}
} catch (Exception e) {
log.error("Error communicating with Steam API for game count of SteamID: {}", steamId, e);
}
return 0;
}
public Optional<SteamProfileAndGamesResponse> getFullPlayerProfile(String steamId) {
var playerOpt = getPlayerSummary(steamId);
if (playerOpt.isEmpty()) {
@@ -47,7 +77,7 @@ public class SteamApiService {
.body(SteamPlayerSummaryResponse.class);
if (response != null && response.response() != null && !response.response().players().isEmpty()) {
return Optional.of(response.response().players().get(0));
return Optional.of(response.response().players().getFirst());
}
} catch (Exception e) {
@@ -4,7 +4,8 @@ import java.security.Principal;
public record UserPrincipal(
String id,
String email
String email,
Long steamId
) implements Principal {
@Override