Skip to content

Commit 3c63afc

Browse files
committed
Breaking NBA API changes
1 parent 9b2dc07 commit 3c63afc

File tree

12 files changed

+9047
-1957
lines changed

12 files changed

+9047
-1957
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ npm-debug.log.*
2525

2626
### IntelliJ IDEA ###
2727
.idea
28+
.run
2829
*.iws
2930
*.iml
3031
*.ipr
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package fr.hippo.nbastats.infrastructure.secondary.api;
2+
3+
class ApiCard {
4+
5+
private ApiScoreboardGame cardData;
6+
7+
public ApiScoreboardGame getCardData() {
8+
return cardData;
9+
}
10+
11+
public void setCardData(ApiScoreboardGame cardData) {
12+
this.cardData = cardData;
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package fr.hippo.nbastats.infrastructure.secondary.api;
2+
3+
import java.util.Collection;
4+
5+
class ApiModule {
6+
7+
private Collection<ApiCard> cards;
8+
9+
public Collection<ApiCard> getCards() {
10+
return cards;
11+
}
12+
13+
public void setCards(Collection<ApiCard> cards) {
14+
this.cards = cards;
15+
}
16+
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package fr.hippo.nbastats.infrastructure.secondary.api;
22

33
import java.util.Collection;
4+
import java.util.stream.Collectors;
45

56
class ApiScoreboard {
67

7-
private Collection<ApiScoreboardGame> games;
8+
private Collection<ApiModule> modules;
89

9-
public Collection<ApiScoreboardGame> getGames() {
10-
return games;
10+
public void setModules(Collection<ApiModule> modules) {
11+
this.modules = modules;
1112
}
1213

13-
public void setGames(Collection<ApiScoreboardGame> games) {
14-
this.games = games;
14+
public Collection<ApiScoreboardGame> getGames() {
15+
return modules.stream()
16+
.flatMap(module -> module.getCards().stream())
17+
.map(ApiCard::getCardData)
18+
.collect(Collectors.toList());
1519
}
1620
}

src/main/java/fr/hippo/nbastats/infrastructure/secondary/api/ApiScoreboardGame.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
class ApiScoreboardGame {
44

55
private String gameId;
6-
private String gameCode;
76
private int gameStatus;
87
private ApiScoreboardTeam homeTeam;
98
private ApiScoreboardTeam awayTeam;
@@ -16,14 +15,6 @@ public void setGameId(String gameId) {
1615
this.gameId = gameId;
1716
}
1817

19-
public String getGameCode() {
20-
return gameCode;
21-
}
22-
23-
public void setGameCode(String gameCode) {
24-
this.gameCode = gameCode;
25-
}
26-
2718
public int getGameStatus() {
2819
return gameStatus;
2920
}

src/main/java/fr/hippo/nbastats/infrastructure/secondary/api/ApiScoreboardWrapper.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/java/fr/hippo/nbastats/infrastructure/secondary/api/ApiScoreboards.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
package fr.hippo.nbastats.infrastructure.secondary.api;
22

3-
import java.time.LocalDate;
43
import org.springframework.http.HttpEntity;
54
import org.springframework.http.HttpHeaders;
65
import org.springframework.http.HttpMethod;
76
import org.springframework.stereotype.Repository;
87
import org.springframework.web.client.RestTemplate;
98

9+
import java.time.LocalDate;
10+
import java.time.format.DateTimeFormatter;
11+
1012
@Repository
1113
class ApiScoreboards {
1214

15+
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("MM/dd/yyyy");
16+
1317
private final RestTemplate rest;
1418

1519
ApiScoreboards(RestTemplate rest) {
1620
this.rest = rest;
1721
}
1822

1923
ApiScoreboard forDate(LocalDate date) {
20-
String url = "https://stats.nba.com/stats/scoreboardv3?LeagueID=00&GameDate=" + date;
24+
String url = "https://core-api.nba.com/cp/api/v1.9/feeds/gamecardfeed?gamedate=" + date.format(DATE_FORMATTER) + "&platform=web";
2125

22-
return rest.exchange(url, HttpMethod.GET, entityWithNbaHeaders(), ApiScoreboardWrapper.class).getBody().getScoreboard();
26+
return rest.exchange(url, HttpMethod.GET, entityWithNbaHeaders(), ApiScoreboard.class).getBody();
2327
}
2428

2529
public ApiBoxscoreGame boxscoreForGame(String gameId) {
@@ -33,6 +37,7 @@ private static HttpEntity<Void> entityWithNbaHeaders() {
3337
headers.set("Origin", "https://www.nba.com/");
3438
headers.set("Referer", "https://www.nba.com/");
3539
headers.set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64)");
40+
headers.set("ocp-apim-subscription-key", "747fa6900c6c4e89a58b81b72f36eb96");
3641

3742
return new HttpEntity<>(headers);
3843
}

src/test/java/fr/hippo/nbastats/infrastructure/secondary/api/ApiNbaGameConverterUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private ApiBoxscoreGame getBoxscoreGame() throws IOException {
121121
}
122122

123123
private ApiScoreboardGame getScoreboardGame() throws IOException {
124-
return JsonHelper.readFromJson(defaultScoreboardJson(), ApiScoreboardWrapper.class).getScoreboard().getGames().iterator().next();
124+
return JsonHelper.readFromJson(defaultScoreboardJson(), ApiScoreboard.class).getGames().iterator().next();
125125
}
126126

127127
private String defaultBoxscoreJson() throws IOException {

src/test/java/fr/hippo/nbastats/infrastructure/secondary/api/ApiNbaGamesUnitTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import fr.hippo.nbastats.domain.GameStatUnitTest;
77
import java.time.LocalDate;
8-
import java.time.format.DateTimeFormatter;
98
import java.util.ArrayList;
109
import java.util.Arrays;
1110
import java.util.List;
@@ -34,15 +33,15 @@ class ApiNbaGamesUnitTest {
3433
@BeforeEach
3534
private void mockScoreboard() {
3635
LocalDate yesterday = LocalDate.now().minusDays(1);
37-
when(scoreboards.forDate(yesterday)).thenReturn(buildScoreboard(game("01", yesterday, 3), game("02", yesterday, 3)));
36+
when(scoreboards.forDate(yesterday)).thenReturn(buildScoreboard(game("01", 3), game("02", 3)));
3837

3938
LocalDate today = LocalDate.now();
40-
when(scoreboards.forDate(today)).thenReturn(buildScoreboard(game("11", today, 2), game("12", today, 3), game("13", today, 1)));
39+
when(scoreboards.forDate(today)).thenReturn(buildScoreboard(game("11", 2), game("12", 3), game("13", 1)));
4140
}
4241

43-
private ApiScoreboard buildScoreboard(ApiScoreboardGame... games) {
42+
private ApiScoreboard buildScoreboard(ApiModule... modules) {
4443
ApiScoreboard scoreboard = new ApiScoreboard();
45-
scoreboard.setGames(Arrays.asList(games));
44+
scoreboard.setModules(Arrays.asList(modules));
4645
return scoreboard;
4746
}
4847

@@ -78,11 +77,16 @@ private void release() {
7877
assertThat(games.findUnreleased()).isPresent();
7978
}
8079

81-
private ApiScoreboardGame game(String gameId, LocalDate gameDate, int status) {
80+
private ApiModule game(String gameId, int status) {
8281
ApiScoreboardGame gameDetails = new ApiScoreboardGame();
8382
gameDetails.setGameId(gameId);
8483
gameDetails.setGameStatus(status);
85-
gameDetails.setGameCode(gameDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")) + "/url");
86-
return gameDetails;
84+
85+
ApiCard card = new ApiCard();
86+
card.setCardData(gameDetails);
87+
88+
ApiModule module = new ApiModule();
89+
module.setCards(List.of(card));
90+
return module;
8791
}
8892
}

src/test/java/fr/hippo/nbastats/infrastructure/secondary/api/ApiScoreboardWrapperUnitTest.java renamed to src/test/java/fr/hippo/nbastats/infrastructure/secondary/api/ApiScoreboardUnitTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
import java.nio.file.Path;
99
import org.junit.jupiter.api.Test;
1010

11-
class ApiScoreboardWrapperUnitTest {
11+
class ApiScoreboardUnitTest {
1212

1313
@Test
1414
void shouldReadFromJson() {
15-
ApiScoreboardWrapper scoreboard = JsonHelper.readFromJson(defaultJson(), ApiScoreboardWrapper.class);
15+
ApiScoreboard scoreboard = JsonHelper.readFromJson(defaultJson(), ApiScoreboard.class);
1616

17-
ApiScoreboardGame game = scoreboard.getScoreboard().getGames().iterator().next();
17+
ApiScoreboardGame game = scoreboard.getGames().iterator().next();
1818
assertThat(game.getGameId()).isEqualTo("0022200003");
19-
assertThat(game.getGameCode()).isEqualTo("20221019/ORLDET");
2019
assertThat(game.getGameStatus()).isEqualTo(3);
2120
assertThat(game.getHomeTeam().getTeamId()).isEqualTo("1610612765");
2221
assertThat(game.getHomeTeam().getScore()).isEqualTo(113);

0 commit comments

Comments
 (0)