Skip to content

Commit b106179

Browse files
committed
Refactor request logic (#332)
* refactor request logic * move the retry logic to the http client * small cleanup + tests * remove invalid comment * add integration test support + IT for TmdbHttpClient * update comment
1 parent 8a59abc commit b106179

74 files changed

Lines changed: 1416 additions & 778 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,7 @@ The wrapper implements most, if not all, of the JSON API. However, because the A
99
not be implemented, or current functionality may break. Please point this out by submitting an issue, or even better, just send us a pull
1010
request!
1111

12-
It's available via [Maven Central](https://central.sonatype.com/artifact/uk.co.conoregan/themoviedbapi). Just add it as dependency to your
13-
project.
14-
15-
<details open>
16-
<summary>Maven</summary>
17-
18-
```xml
19-
<dependency>
20-
<groupId>uk.co.conoregan</groupId>
21-
<artifactId>themoviedbapi</artifactId>
22-
<version>{version}</version>
23-
</dependency>
24-
```
25-
</details>
26-
27-
<details>
28-
<summary>Gradle (Kotlin)</summary>
29-
30-
```kotlin
31-
dependencies {
32-
implementation("uk.co.conoregan:themoviedbapi:{version}")
33-
}
34-
```
35-
</details>
12+
It's available via [Maven Central](https://central.sonatype.com/artifact/uk.co.conoregan/themoviedbapi). Just add it as dependency to your project.
3613

3714
## Usage
3815
To register for a TMdB API key, click the [API link](https://www.themoviedb.org/settings/api) from within your account settings page. There are two types of API keys currently provided by TMdB, please ensure you are using the `API Read Access Token` key.
@@ -42,6 +19,9 @@ With this you can instantiate `info.movito.themoviedbapi.TmdbApi`, which has get
4219
TmdbApi tmdbApi = new TmdbApi("<apikey>");
4320
```
4421

22+
By default this uses the library's built-in HTTP client. If you would rather plug in your own, see
23+
[Using your own HTTP client](#using-your-own-http-client).
24+
4525
### Examples
4626
#### Get movie details
4727
```java
@@ -78,7 +58,7 @@ MovieDb movie = tmdbMovies.getDetails(5353, "en-US", MovieAppendToResponse.value
7858
To find all methods that use append to response, see the `info.movito.themoviedbapi.tools.appendtoresponse.AppendToResponse` interface
7959
implementations.
8060

81-
### Exception Handling
61+
### Exception handling
8262
Every API method can throw a `info.movito.themoviedbapi.tools.TmdbException` if the request fails for any reason. You should catch this
8363
exception and handle it appropriately.
8464

@@ -106,6 +86,20 @@ catch (TmdbException exception) {
10686
We chose to throw exceptions rather than returning `null`, so you have more control over what you do with each failure case. E.g. with the
10787
example above, you may want to display an error message to the user about failing authentication.
10888

89+
### Using your own HTTP client
90+
By default, `TmdbApi` uses the built-in `info.movito.themoviedbapi.tools.TmdbHttpClient`, which is backed by the JDK's
91+
`java.net.http.HttpClient`. If you would rather use a different HTTP client, you can provide your own
92+
implementation of the `info.movito.themoviedbapi.tools.TmdbRequestExecutor` interface.
93+
94+
Your implementation only has to make the call and hand back the raw response. If the request fails (e.g. an `IOException`), wrap it in a
95+
`info.movito.themoviedbapi.tools.TmdbException`. Interpreting TMdB status codes (e.g. mapping unsuccessful responses to exceptions) is handled by the library on top of your executor, so
96+
you do not need to deal with that (see `info.movito.themoviedbapi.tools.TmdbApiClient` for more information).
97+
98+
Then, simply pass your implementation to `TmdbApi` instead of the API key:
99+
```java
100+
TmdbApi tmdbApi = new TmdbApi(new CustomHttpClient());
101+
```
102+
109103
## Project Logging
110104

111105
This project uses [SLF4J](http://www.slf4j.org) to abstract the logging in the project. To use the logging in your own

build.gradle.kts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ dependencies {
2828
testImplementation("org.mockito:mockito-core")
2929
testImplementation("org.mockito:mockito-junit-jupiter")
3030

31+
testImplementation("org.wiremock:wiremock:3.13.2")
32+
3133
// util
3234
compileOnly("org.projectlombok:lombok:1.18.46")
3335
annotationProcessor("org.projectlombok:lombok:1.18.46")
@@ -49,7 +51,24 @@ java {
4951
}
5052

5153
tasks.test {
52-
useJUnitPlatform()
54+
useJUnitPlatform {
55+
excludeTags("integration")
56+
}
57+
}
58+
59+
tasks.register<Test>("integrationTest") {
60+
description = "Runs integration tests (tagged with \"integration\")."
61+
group = "verification"
62+
useJUnitPlatform {
63+
includeTags("integration")
64+
}
65+
testClassesDirs = sourceSets["test"].output.classesDirs
66+
classpath = sourceSets["test"].runtimeClasspath
67+
shouldRunAfter(tasks.test)
68+
}
69+
70+
tasks.check {
71+
dependsOn("integrationTest")
5372
}
5473

5574
checkstyle {

src/main/java/info/movito/themoviedbapi/AbstractTmdbApi.java

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

src/main/java/info/movito/themoviedbapi/TmdbAccount.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import info.movito.themoviedbapi.model.rated.RatedTvSeriesResultsPage;
1313
import info.movito.themoviedbapi.tools.ApiUrl;
1414
import info.movito.themoviedbapi.tools.RequestType;
15+
import info.movito.themoviedbapi.tools.TmdbApiClient;
1516
import info.movito.themoviedbapi.tools.TmdbException;
1617
import info.movito.themoviedbapi.tools.sortby.AccountSortBy;
1718
import info.movito.themoviedbapi.util.JsonUtil;
@@ -20,15 +21,17 @@
2021
* The movie database api for accounts. See the
2122
* <a href="https://developer.themoviedb.org/reference/account-details">documentation</a> for more info.
2223
*/
23-
public class TmdbAccount extends AbstractTmdbApi {
24+
public class TmdbAccount {
2425
public static final String PARAM_SESSION = "session_id";
2526
protected static final String TMDB_METHOD_ACCOUNT = "account";
2627

28+
private final TmdbApiClient tmdbApiClient;
29+
2730
/**
2831
* Create a new TmdbAccount instance to call the account related TMDb API methods.
2932
*/
30-
TmdbAccount(TmdbApi tmdbApi) {
31-
super(tmdbApi);
33+
TmdbAccount(TmdbApiClient tmdbApiClient) {
34+
this.tmdbApiClient = tmdbApiClient;
3235
}
3336

3437
/**
@@ -44,7 +47,7 @@ public Account getDetails(Integer accountId, String sessionId) throws TmdbExcept
4447
ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_ACCOUNT, accountId)
4548
.addQueryParam(PARAM_SESSION, sessionId);
4649

47-
return mapJsonResult(apiUrl, Account.class);
50+
return tmdbApiClient.get(apiUrl, Account.class);
4851
}
4952

5053
/**
@@ -90,7 +93,7 @@ private ResponseStatus changeFavoriteStatus(Integer accountId, String sessionId,
9093
body.put("favorite", isFavorite);
9194

9295
String jsonBody = JsonUtil.toJson(body);
93-
return mapJsonResult(apiUrl, jsonBody, RequestType.POST, ResponseStatus.class);
96+
return tmdbApiClient.request(apiUrl, jsonBody, RequestType.POST, ResponseStatus.class);
9497
}
9598

9699
/**
@@ -136,7 +139,7 @@ private ResponseStatus changeWatchListStatus(Integer accountId, String sessionId
136139
body.put("watchlist", isWatchList);
137140

138141
String jsonBody = JsonUtil.toJson(body);
139-
return mapJsonResult(apiUrl, jsonBody, RequestType.POST, ResponseStatus.class);
142+
return tmdbApiClient.request(apiUrl, jsonBody, RequestType.POST, ResponseStatus.class);
140143
}
141144

142145
/**
@@ -159,7 +162,7 @@ public MovieResultsPage getFavoriteMovies(Integer accountId, String sessionId, S
159162
.addPage(page)
160163
.addSortBy(sortBy);
161164

162-
return mapJsonResult(apiUrl, MovieResultsPage.class);
165+
return tmdbApiClient.get(apiUrl, MovieResultsPage.class);
163166
}
164167

165168
/**
@@ -182,7 +185,7 @@ public TvSeriesResultsPage getFavoriteTv(Integer accountId, String sessionId, St
182185
.addPage(page)
183186
.addSortBy(sortBy);
184187

185-
return mapJsonResult(apiUrl, TvSeriesResultsPage.class);
188+
return tmdbApiClient.get(apiUrl, TvSeriesResultsPage.class);
186189
}
187190

188191
/**
@@ -200,7 +203,7 @@ public MovieListResultsPage getLists(Integer accountId, String sessionId, Intege
200203
.addQueryParam(PARAM_SESSION, sessionId)
201204
.addPage(page);
202205

203-
return mapJsonResult(apiUrl, MovieListResultsPage.class);
206+
return tmdbApiClient.get(apiUrl, MovieListResultsPage.class);
204207
}
205208

206209
/**
@@ -223,7 +226,7 @@ public RatedMovieResultsPage getRatedMovies(int accountId, String sessionId, Str
223226
.addPage(page)
224227
.addSortBy(sortBy);
225228

226-
return mapJsonResult(apiUrl, RatedMovieResultsPage.class);
229+
return tmdbApiClient.get(apiUrl, RatedMovieResultsPage.class);
227230
}
228231

229232
/**
@@ -246,7 +249,7 @@ public RatedTvSeriesResultsPage getRatedTvSeries(int accountId, String sessionId
246249
.addPage(page)
247250
.addSortBy(sortBy);
248251

249-
return mapJsonResult(apiUrl, RatedTvSeriesResultsPage.class);
252+
return tmdbApiClient.get(apiUrl, RatedTvSeriesResultsPage.class);
250253
}
251254

252255
/**
@@ -269,7 +272,7 @@ public RatedTvEpisodeResultsPage getRatedTvEpisodes(int accountId, String sessio
269272
.addPage(page)
270273
.addSortBy(sortBy);
271274

272-
return mapJsonResult(apiUrl, RatedTvEpisodeResultsPage.class);
275+
return tmdbApiClient.get(apiUrl, RatedTvEpisodeResultsPage.class);
273276
}
274277

275278
/**
@@ -292,7 +295,7 @@ public MovieResultsPage getWatchListMovies(Integer accountId, String sessionId,
292295
.addPage(page)
293296
.addSortBy(sortBy);
294297

295-
return mapJsonResult(apiUrl, MovieResultsPage.class);
298+
return tmdbApiClient.get(apiUrl, MovieResultsPage.class);
296299
}
297300

298301
/**
@@ -315,7 +318,7 @@ public TvSeriesResultsPage getWatchListTvSeries(Integer accountId, String sessio
315318
.addPage(page)
316319
.addSortBy(sortBy);
317320

318-
return mapJsonResult(apiUrl, TvSeriesResultsPage.class);
321+
return tmdbApiClient.get(apiUrl, TvSeriesResultsPage.class);
319322
}
320323

321324
/**

0 commit comments

Comments
 (0)