-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9be1b6
commit 1e1650e
Showing
12 changed files
with
384 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/HttpTvdbClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.jordansimsmith.immersiontracker; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Preconditions; | ||
import com.jordansimsmith.secrets.Secrets; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
|
||
public class HttpTvdbClient implements TvdbClient { | ||
@VisibleForTesting static final String SECRET = "immersion_tracker_api"; | ||
|
||
private final ObjectMapper objectMapper; | ||
private final Secrets secrets; | ||
private final HttpClient httpClient; | ||
|
||
public HttpTvdbClient(ObjectMapper objectMapper, Secrets secrets, HttpClient httpClient) { | ||
this.objectMapper = objectMapper; | ||
this.secrets = secrets; | ||
this.httpClient = httpClient; | ||
} | ||
|
||
private record LoginRequest(@JsonProperty("apikey") String apiKey) {} | ||
|
||
private record LoginResponse( | ||
@JsonProperty("status") String status, @JsonProperty("data") LoginData data) {} | ||
|
||
private record LoginData(@JsonProperty("token") String token) {} | ||
|
||
private record SeriesResponse( | ||
@JsonProperty("status") String status, @JsonProperty("data") SeriesData data) {} | ||
|
||
private record SeriesData(@JsonProperty("name") String name, @JsonProperty String image) {} | ||
|
||
@Override | ||
public Show getShow(int id) { | ||
try { | ||
return doGetShow(id); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private Show doGetShow(int id) throws Exception { | ||
var secret = secrets.get(SECRET); | ||
var apikey = objectMapper.readTree(secret).get("tvdb_api_key").asText(null); | ||
Preconditions.checkNotNull(apikey); | ||
|
||
var loginReq = | ||
HttpRequest.newBuilder() | ||
.uri(new URI("https://api4.thetvdb.com/v4/login")) | ||
.header("Accept", "application/json") | ||
.header("Content-Type", "application/json") | ||
.POST( | ||
HttpRequest.BodyPublishers.ofString( | ||
objectMapper.writeValueAsString(new LoginRequest(apikey)))) | ||
.build(); | ||
var loginRes = httpClient.send(loginReq, HttpResponse.BodyHandlers.ofString()); | ||
|
||
if (loginRes.statusCode() != 200) { | ||
throw new IOException( | ||
"tvdb.com login request failed with status code " + loginRes.statusCode()); | ||
} | ||
|
||
var loginResBody = objectMapper.readValue(loginRes.body(), LoginResponse.class); | ||
if (!loginResBody.status.equals("success")) { | ||
throw new IOException("tvdb.com login request failed with status " + loginResBody.status); | ||
} | ||
|
||
var token = loginResBody.data.token; | ||
Preconditions.checkNotNull(token); | ||
|
||
var seriesReq = | ||
HttpRequest.newBuilder() | ||
.uri(new URI("https://api4.thetvdb.com/v4/series/" + id)) | ||
.header("Accept", "application/json") | ||
.header("Content-Type", "application/json") | ||
.GET() | ||
.build(); | ||
var seriesRes = httpClient.send(seriesReq, HttpResponse.BodyHandlers.ofString()); | ||
|
||
if (seriesRes.statusCode() != 200) { | ||
throw new IOException( | ||
"tvdb.com series request failed with status code " + seriesRes.statusCode()); | ||
} | ||
|
||
var seriesResBody = objectMapper.readValue(seriesRes.body(), SeriesResponse.class); | ||
if (!seriesResBody.status.equals("success")) { | ||
throw new IOException("tvdb.com series request failed with status " + seriesResBody.status); | ||
} | ||
|
||
Preconditions.checkNotNull(seriesResBody.data.name); | ||
Preconditions.checkNotNull(seriesResBody.data.image); | ||
return new Show(id, seriesResBody.data.name, seriesResBody.data.image); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/TvdbClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.jordansimsmith.immersiontracker; | ||
|
||
public interface TvdbClient { | ||
record Show(int id, String name, String image) {} | ||
|
||
Show getShow(int id); | ||
} |
77 changes: 77 additions & 0 deletions
77
...sion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/UpdateShowHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.jordansimsmith.immersiontracker; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent; | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Preconditions; | ||
import java.util.Map; | ||
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; | ||
import software.amazon.awssdk.enhanced.dynamodb.Key; | ||
|
||
public class UpdateShowHandler | ||
implements RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse> { | ||
private final ObjectMapper objectMapper; | ||
private final DynamoDbTable<ImmersionTrackerItem> immersionTrackerTable; | ||
private final TvdbClient tvdbClient; | ||
|
||
@VisibleForTesting | ||
record UpdateShowRequest( | ||
@JsonProperty("folder_name") String folderName, @JsonProperty("tvdb_id") int tvdbId) {} | ||
|
||
@VisibleForTesting | ||
record UpdateShowResponse() {} | ||
|
||
public UpdateShowHandler() { | ||
this(ImmersionTrackerFactory.create()); | ||
} | ||
|
||
@VisibleForTesting | ||
UpdateShowHandler(ImmersionTrackerFactory factory) { | ||
this.objectMapper = factory.objectMapper(); | ||
this.immersionTrackerTable = factory.immersionTrackerTable(); | ||
this.tvdbClient = factory.tvdbClient(); | ||
} | ||
|
||
@Override | ||
public APIGatewayV2HTTPResponse handleRequest(APIGatewayV2HTTPEvent event, Context context) { | ||
try { | ||
return doHandleRequest(event, context); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private APIGatewayV2HTTPResponse doHandleRequest(APIGatewayV2HTTPEvent event, Context context) | ||
throws Exception { | ||
var user = event.getQueryStringParameters().get("user"); | ||
Preconditions.checkNotNull(user); | ||
|
||
var body = objectMapper.readValue(event.getBody(), UpdateShowRequest.class); | ||
|
||
var show = | ||
immersionTrackerTable.getItem( | ||
Key.builder() | ||
.partitionValue(ImmersionTrackerItem.formatPk(user)) | ||
.sortValue(ImmersionTrackerItem.formatShowSk(body.folderName)) | ||
.build()); | ||
Preconditions.checkNotNull(show); | ||
|
||
var tvdbShow = tvdbClient.getShow(body.tvdbId); | ||
|
||
show.setTvdbId(tvdbShow.id()); | ||
show.setTvdbName(tvdbShow.name()); | ||
show.setTvdbImage(tvdbShow.image()); | ||
immersionTrackerTable.updateItem(show); | ||
|
||
var res = new UpdateShowResponse(); | ||
return APIGatewayV2HTTPResponse.builder() | ||
.withStatusCode(200) | ||
.withHeaders(Map.of("Content-Type", "application/json; charset=utf-8")) | ||
.withBody(objectMapper.writeValueAsString(res)) | ||
.build(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
immersion_tracker_api/src/test/java/com/jordansimsmith/immersiontracker/FakeTvdbClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.jordansimsmith.immersiontracker; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class FakeTvdbClient implements TvdbClient { | ||
private final Map<Integer, Show> shows = new HashMap<>(); | ||
|
||
@Override | ||
public Show getShow(int id) { | ||
return shows.get(id); | ||
} | ||
|
||
public void addShow(Show show) { | ||
shows.put(show.id(), show); | ||
} | ||
|
||
public void reset() { | ||
shows.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.