Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit a859359

Browse files
[v2] Add TMDB requests (#153 #158)
1 parent 6ef8251 commit a859359

16 files changed

+693
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace TraktNET
2+
{
3+
public sealed partial class TMDBRequestValidationException : Exception
4+
{
5+
/// <summary>The name of the proeprty that caused the current exception.</summary>
6+
public string? PropertyName { get; }
7+
}
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace TraktNET
2+
{
3+
public sealed partial class TMDBRequestValidationException
4+
{
5+
internal TMDBRequestValidationException(string message, Exception? innerException = null) : base(message, innerException)
6+
{
7+
}
8+
9+
internal TMDBRequestValidationException(string propertyName, string message, Exception? innerException = null)
10+
: base(message, innerException)
11+
=> PropertyName = propertyName;
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace TraktNET
2+
{
3+
internal abstract class TMDBRequestBase(HttpMethod method, Uri? requestUri) : HttpRequestMessage(method, requestUri)
4+
{
5+
internal abstract void BuildUri();
6+
7+
internal virtual void Validate() { }
8+
}
9+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
namespace TraktNET
2+
{
3+
internal sealed class TMDBConfigurationGetRequest : TMDBRequestBase
4+
{
5+
public TMDBConfigurationGetRequest() : base(HttpMethod.Get, null) { }
6+
7+
internal override void BuildUri() => RequestUri = new Uri("configuration", UriKind.Relative);
8+
}
9+
10+
internal abstract class TMDBImagesGetRequest(string path) : TMDBRequestBase(HttpMethod.Get, null)
11+
{
12+
protected readonly string _path = path;
13+
14+
internal required uint Id { get; init; }
15+
16+
internal string? Language { get; set; }
17+
18+
internal string? IncludeImageLanguage { get; set; }
19+
20+
internal override void BuildUri()
21+
{
22+
string requestUri = $"{_path}/{Id}/images";
23+
List<string> queries = GetQueries();
24+
25+
if (queries.Count > 0)
26+
{
27+
requestUri = requestUri + "?" + string.Join("&", queries);
28+
}
29+
30+
RequestUri = new Uri(requestUri, UriKind.Relative);
31+
}
32+
33+
protected List<string> GetQueries()
34+
{
35+
List<string> queries = [];
36+
37+
if (!string.IsNullOrWhiteSpace(Language))
38+
{
39+
queries.Add($"language={Language}");
40+
}
41+
42+
if (!string.IsNullOrWhiteSpace(IncludeImageLanguage))
43+
{
44+
queries.Add($"include_image_language={IncludeImageLanguage}");
45+
}
46+
47+
return queries;
48+
}
49+
50+
internal override void Validate()
51+
{
52+
if (Id == 0)
53+
{
54+
throw new TMDBRequestValidationException(nameof(Id), "Id must not be 0");
55+
}
56+
}
57+
}
58+
59+
internal abstract class TMDBVideosGetRequest(string path) : TMDBRequestBase(HttpMethod.Get, null)
60+
{
61+
protected readonly string _path = path;
62+
63+
internal required uint Id { get; init; }
64+
65+
internal string? Language { get; set; }
66+
67+
internal string? IncludeVideoLanguage { get; set; }
68+
69+
internal override void BuildUri()
70+
{
71+
string requestUri = $"{_path}/{Id}/videos";
72+
List<string> queries = GetQueries();
73+
74+
if (queries.Count > 0)
75+
{
76+
requestUri = requestUri + "?" + string.Join("&", queries);
77+
}
78+
79+
RequestUri = new Uri(requestUri, UriKind.Relative);
80+
}
81+
82+
protected List<string> GetQueries()
83+
{
84+
List<string> queries = [];
85+
86+
if (!string.IsNullOrWhiteSpace(Language))
87+
{
88+
queries.Add($"language={Language}");
89+
}
90+
91+
if (!string.IsNullOrWhiteSpace(IncludeVideoLanguage))
92+
{
93+
queries.Add($"include_video_language={IncludeVideoLanguage}");
94+
}
95+
96+
return queries;
97+
}
98+
99+
internal override void Validate()
100+
{
101+
if (Id == 0)
102+
{
103+
throw new TMDBRequestValidationException(nameof(Id), "Id must not be 0");
104+
}
105+
}
106+
}
107+
108+
internal sealed class TMDBMovieImagesGetRequest : TMDBImagesGetRequest
109+
{
110+
public TMDBMovieImagesGetRequest() : base("movies") { }
111+
}
112+
113+
internal class TMDBShowImagesGetRequest : TMDBImagesGetRequest
114+
{
115+
public TMDBShowImagesGetRequest() : base("shows") { }
116+
}
117+
118+
internal class TMDBSeasonImagesGetRequest : TMDBShowImagesGetRequest
119+
{
120+
internal required uint SeasonNumber { get; init; }
121+
122+
internal override void BuildUri()
123+
{
124+
string requestUri = $"{_path}/{Id}/season/{SeasonNumber}/images";
125+
List<string> queries = GetQueries();
126+
127+
if (queries.Count > 0)
128+
{
129+
requestUri = requestUri + "?" + string.Join("&", queries);
130+
}
131+
132+
RequestUri = new Uri(requestUri, UriKind.Relative);
133+
}
134+
}
135+
136+
internal sealed class TMDBEpisodeImagesGetRequest : TMDBSeasonImagesGetRequest
137+
{
138+
internal required uint EpisodeNumber { get; init; }
139+
140+
internal override void BuildUri()
141+
{
142+
string requestUri = $"{_path}/{Id}/season/{SeasonNumber}/episode/{EpisodeNumber}/images";
143+
List<string> queries = GetQueries();
144+
145+
if (queries.Count > 0)
146+
{
147+
requestUri = requestUri + "?" + string.Join("&", queries);
148+
}
149+
150+
RequestUri = new Uri(requestUri, UriKind.Relative);
151+
}
152+
}
153+
154+
internal sealed class TMDBPersonImagesGetRequest : TMDBRequestBase
155+
{
156+
internal required uint Id { get; init; }
157+
158+
public TMDBPersonImagesGetRequest() : base(HttpMethod.Get, null) { }
159+
160+
internal override void BuildUri() => RequestUri = new Uri($"person/{Id}/images", UriKind.Relative);
161+
162+
internal override void Validate()
163+
{
164+
if (Id == 0)
165+
{
166+
throw new TMDBRequestValidationException(nameof(Id), "Id must not be 0");
167+
}
168+
}
169+
}
170+
171+
internal sealed class TMDBMovieVideosGetRequest : TMDBVideosGetRequest
172+
{
173+
public TMDBMovieVideosGetRequest() : base("movies") { }
174+
}
175+
176+
internal class TMDBShowVideosGetRequest : TMDBVideosGetRequest
177+
{
178+
public TMDBShowVideosGetRequest() : base("shows") { }
179+
}
180+
181+
internal class TMDBSeasonVideosGetRequest : TMDBShowVideosGetRequest
182+
{
183+
internal required uint SeasonNumber { get; init; }
184+
185+
internal override void BuildUri()
186+
{
187+
string requestUri = $"{_path}/{Id}/season/{SeasonNumber}/videos";
188+
List<string> queries = GetQueries();
189+
190+
if (queries.Count > 0)
191+
{
192+
requestUri = requestUri + "?" + string.Join("&", queries);
193+
}
194+
195+
RequestUri = new Uri(requestUri, UriKind.Relative);
196+
}
197+
}
198+
199+
internal sealed class TMDBEpisodeVideosGetRequest : TMDBSeasonVideosGetRequest
200+
{
201+
internal required uint EpisodeNumber { get; init; }
202+
203+
internal override void BuildUri()
204+
{
205+
string requestUri = $"{_path}/{Id}/season/{SeasonNumber}/episode/{EpisodeNumber}/videos";
206+
List<string> queries = GetQueries();
207+
208+
if (queries.Count > 0)
209+
{
210+
requestUri = requestUri + "?" + string.Join("&", queries);
211+
}
212+
213+
RequestUri = new Uri(requestUri, UriKind.Relative);
214+
}
215+
}
216+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
using System.Runtime.CompilerServices;
22

33
[assembly: InternalsVisibleTo("Trakt.NET.Tests.Utility")]
4+
[assembly: InternalsVisibleTo("Trakt.NET.Extensions.TMDB.Tests")]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#if TRAKT_NET_4XX_FRAMEWORK_TARGET
2+
using System.Net.Http;
3+
#endif
4+
5+
namespace TraktNET.GetRequests
6+
{
7+
public sealed class TMDBConfigurationGetRequestTests
8+
{
9+
[Fact]
10+
public void TestTMDBConfigurationGetRequestHasValidURIPath()
11+
{
12+
var configurationGetRequest = new TMDBConfigurationGetRequest();
13+
14+
configurationGetRequest.BuildUri();
15+
configurationGetRequest.RequestUri.ShouldBe(new Uri("configuration", UriKind.Relative));
16+
}
17+
18+
[Fact]
19+
public void TestTMDBConfigurationGetRequestIsGetRequest()
20+
{
21+
var configurationGetRequest = new TMDBConfigurationGetRequest();
22+
configurationGetRequest.Method.ShouldBe(HttpMethod.Get);
23+
}
24+
}
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#if TRAKT_NET_4XX_FRAMEWORK_TARGET
2+
using System.Net.Http;
3+
#endif
4+
5+
namespace TraktNET.GetRequests
6+
{
7+
public sealed class TMDBEpisodeImagesGetRequestTests
8+
{
9+
private const string URIPath = $"shows/{TMDBTestConstants.Shows.ShowIDString}/season/{TMDBTestConstants.Episodes.EpisodeNrString}/episode/{TMDBTestConstants.Episodes.EpisodeNrString}/images";
10+
11+
[Fact]
12+
public void TestTMDBEpisodeImagesGetRequestHasValidURIPath()
13+
{
14+
var episodeImagesGetRequest = new TMDBEpisodeImagesGetRequest
15+
{
16+
Id = TMDBTestConstants.Shows.ShowID,
17+
SeasonNumber = TMDBTestConstants.Seasons.SeasonNr,
18+
EpisodeNumber = TMDBTestConstants.Episodes.EpisodeNr
19+
};
20+
21+
episodeImagesGetRequest.BuildUri();
22+
episodeImagesGetRequest.RequestUri.ShouldBe(new Uri(URIPath, UriKind.Relative));
23+
}
24+
25+
[Fact]
26+
public void TestTMDBEpisodeImagesGetRequestIsGetRequest()
27+
{
28+
var episodeImagesGetRequest = new TMDBEpisodeImagesGetRequest
29+
{
30+
Id = TMDBTestConstants.Shows.ShowID,
31+
SeasonNumber = TMDBTestConstants.Seasons.SeasonNr,
32+
EpisodeNumber = TMDBTestConstants.Episodes.EpisodeNr
33+
};
34+
35+
episodeImagesGetRequest.Method.ShouldBe(HttpMethod.Get);
36+
}
37+
38+
[Fact]
39+
public void TestTMDBEpisodeImagesGetRequestValidate()
40+
{
41+
var episodeImagesGetRequest = new TMDBEpisodeImagesGetRequest
42+
{
43+
Id = 0,
44+
SeasonNumber = TMDBTestConstants.Seasons.SeasonNr,
45+
EpisodeNumber = TMDBTestConstants.Episodes.EpisodeNr
46+
};
47+
48+
Action act = () => episodeImagesGetRequest.Validate();
49+
act.ShouldThrow<TMDBRequestValidationException>();
50+
}
51+
}
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#if TRAKT_NET_4XX_FRAMEWORK_TARGET
2+
using System.Net.Http;
3+
#endif
4+
5+
namespace TraktNET.GetRequests
6+
{
7+
public sealed class TMDBEpisodeVideosGetRequestTests
8+
{
9+
private const string URIPath = $"shows/{TMDBTestConstants.Shows.ShowIDString}/season/{TMDBTestConstants.Episodes.EpisodeNrString}/episode/{TMDBTestConstants.Episodes.EpisodeNrString}/videos";
10+
11+
[Fact]
12+
public void TestTMDBEpisodeVideosGetRequestHasValidURIPath()
13+
{
14+
var episodeVideosGetRequest = new TMDBEpisodeVideosGetRequest
15+
{
16+
Id = TMDBTestConstants.Shows.ShowID,
17+
SeasonNumber = TMDBTestConstants.Seasons.SeasonNr,
18+
EpisodeNumber = TMDBTestConstants.Episodes.EpisodeNr
19+
};
20+
21+
episodeVideosGetRequest.BuildUri();
22+
episodeVideosGetRequest.RequestUri.ShouldBe(new Uri(URIPath, UriKind.Relative));
23+
}
24+
25+
[Fact]
26+
public void TestTMDBEpisodeVideosGetRequestIsGetRequest()
27+
{
28+
var episodeVideosGetRequest = new TMDBEpisodeVideosGetRequest
29+
{
30+
Id = TMDBTestConstants.Shows.ShowID,
31+
SeasonNumber = TMDBTestConstants.Seasons.SeasonNr,
32+
EpisodeNumber = TMDBTestConstants.Episodes.EpisodeNr
33+
};
34+
35+
episodeVideosGetRequest.Method.ShouldBe(HttpMethod.Get);
36+
}
37+
38+
[Fact]
39+
public void TestTMDBEpisodeVideosGetRequestValidate()
40+
{
41+
var episodeVideosGetRequest = new TMDBEpisodeVideosGetRequest
42+
{
43+
Id = 0,
44+
SeasonNumber = TMDBTestConstants.Seasons.SeasonNr,
45+
EpisodeNumber = TMDBTestConstants.Episodes.EpisodeNr
46+
};
47+
48+
Action act = () => episodeVideosGetRequest.Validate();
49+
act.ShouldThrow<TMDBRequestValidationException>();
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)