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

Commit d31c212

Browse files
[v2] Add TMDB context (#153 #158)
1 parent 07a12a0 commit d31c212

File tree

7 files changed

+125
-2
lines changed

7 files changed

+125
-2
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace TraktNET
2+
{
3+
public sealed class TMDBContext : ITraktContext
4+
{
5+
private string _readAccessToken = string.Empty;
6+
7+
public string ID { get; } = Guid.NewGuid().ToString();
8+
9+
public Uri BaseUri { get; internal set; }
10+
11+
internal TMDBContext(string readAccessToken)
12+
{
13+
ReadAccessToken = readAccessToken;
14+
BaseUri = new Uri($"{TMDBConstants.API.BaseURL}{TMDBConstants.API.Version}/");
15+
HttpClientProvider = new TMDBHttpClientProvider();
16+
}
17+
18+
internal string ReadAccessToken
19+
{
20+
get => _readAccessToken;
21+
22+
set
23+
{
24+
ArgumentValidator.ThrowIfNullOrWhiteSpace(value, "read access token must not be null or empty or only whitespace", checkSpaces: true);
25+
_readAccessToken = value;
26+
}
27+
}
28+
29+
internal HttpClientProvider<TMDBContext> HttpClientProvider { get; set; }
30+
31+
internal HttpClient GetHttpClient() => HttpClientProvider.GetHttpClient(this);
32+
}
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Concurrent;
2+
3+
namespace TraktNET
4+
{
5+
internal sealed class TMDBHttpClientProvider : HttpClientProvider<TMDBContext>
6+
{
7+
private static readonly ConcurrentDictionary<string, HttpClient> s_httpClientCache = new();
8+
9+
internal override HttpClient GetHttpClient(TMDBContext context)
10+
=> s_httpClientCache.GetOrAdd(context.ID, CreateHttpClient(context));
11+
}
12+
}

src/libs/Trakt.NET.Extensions.TMDB/Internal/TMDBConstants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace TraktNET
44
{
55
internal static class TMDBConstants
66
{
7+
internal const string ContextRegistryKey = "tmdb";
8+
79
internal static class API
810
{
911
internal const string BaseURL = "https://api.themoviedb.org/";

src/libs/Trakt.NET.Extensions.TMDB/TraktTMDBExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ namespace TraktNET
44
{
55
public static class TraktTMDBExtensions
66
{
7+
public static TraktClient WithTMDB(this TraktClient client, string readAccessToken)
8+
{
9+
var context = new TMDBContext(readAccessToken);
10+
ContextRegistry.Add(TMDBConstants.ContextRegistryKey, context);
11+
return client;
12+
}
13+
714
public static async Task<TMDBResponse<TMDBMovieImages>> GetTMDBImagesAsync(this TraktMovieMinimal traktMovie, CancellationToken cancellationToken = default)
815
{
916
// TODO
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Concurrent;
2+
using System.Diagnostics;
3+
4+
namespace TraktNET
5+
{
6+
internal static class ContextRegistry
7+
{
8+
private static readonly ConcurrentDictionary<string, ITraktContext> s_contexts = new();
9+
10+
internal static void Add(ITraktContext context) => _ = s_contexts.TryAdd(context.ID, context);
11+
12+
internal static void Add(string id, ITraktContext context)
13+
{
14+
Debug.Assert(!string.IsNullOrWhiteSpace(id));
15+
_ = s_contexts.TryAdd(id, context);
16+
}
17+
18+
internal static TContext Get<TContext>(string id) where TContext : class, ITraktContext
19+
{
20+
Debug.Assert(s_contexts.ContainsKey(id));
21+
return (s_contexts[id] as TContext)!;
22+
}
23+
}
24+
}

src/libs/Trakt.NET/Internal/Contexts/TraktContext.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,18 @@ public abstract class TraktContext : ITraktContext
7777
public TraktUsersModule Users { get; }
7878

7979
public static TraktContext Create(string clientID, string clientSecret)
80-
=> new TraktDefaultContext(clientID, clientSecret);
80+
{
81+
var context = new TraktDefaultContext(clientID, clientSecret);
82+
ContextRegistry.Add(context);
83+
return context;
84+
}
8185

8286
public static TraktContext CreateForSandbox(string clientID, string clientSecret)
83-
=> new TraktSandboxContext(clientID, clientSecret);
87+
{
88+
var context = new TraktSandboxContext(clientID, clientSecret);
89+
ContextRegistry.Add(context);
90+
return context;
91+
}
8492

8593
protected TraktContext(string clientID, string clientSecret)
8694
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace TraktNET.Contexts
2+
{
3+
public sealed class TMDBContextTests
4+
{
5+
private const string ReadAccessToken = "readAccessToken";
6+
7+
[Fact]
8+
public void TestTMDBContextWithReadAccessToken()
9+
{
10+
var context = new TMDBContext(ReadAccessToken);
11+
12+
context.ID.ShouldNotBeNullOrEmpty();
13+
context.ReadAccessToken.ShouldBe(ReadAccessToken);
14+
}
15+
16+
[Fact]
17+
public void TestTMDBContextHasCorrectBaseUri()
18+
{
19+
var context = new TMDBContext(ReadAccessToken);
20+
21+
context.BaseUri.AbsoluteUri.ShouldBe("https://api.themoviedb.org/3/");
22+
}
23+
24+
[Fact]
25+
public void TestTMDBContextInvalidReadAccessToken()
26+
{
27+
Action act = () => _ = new TMDBContext(string.Empty);
28+
act.ShouldThrow<ArgumentException>();
29+
30+
act = () => _ = new TMDBContext(" ");
31+
act.ShouldThrow<ArgumentException>();
32+
33+
act = () => _ = new TMDBContext(" id ");
34+
act.ShouldThrow<ArgumentException>();
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)