Last active
January 25, 2017 23:50
-
-
Save henrikfroehling/1ad0402197f4134ed71dfde3976b98ab to your computer and use it in GitHub Desktop.
TraktApiSharp Idea: Context-based usage approach
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
namespace Idea | |
{ | |
using TraktApiSharp.Authentication; | |
public abstract class TraktConfigurationBase : ITraktConfiguration | |
{ | |
private const string API_URL = "https://api.trakt.tv/"; | |
private const string API_STAGING_URL = "https://api-staging.trakt.tv/"; | |
private const string OAUTH_BASE_AUTHORIZATION_URL = "https://trakt.tv"; | |
private const string OAUTH_BASE_AUHORIZATION_STAGING_URL = "https://staging.trakt.tv"; | |
public abstract int ApiVersion { get; set; } | |
public abstract bool ForceAuthorization { get; set; } | |
public abstract bool UseSandboxEnvironment { get; set; } | |
public string BaseUrl => UseSandboxEnvironment ? API_STAGING_URL : API_URL; | |
public string BaseOAuthAuthorizationUrl | |
=> UseSandboxEnvironment ? OAUTH_BASE_AUHORIZATION_STAGING_URL : OAUTH_BASE_AUTHORIZATION_URL; | |
} | |
public abstract class TraktContextBase : ITraktContext | |
{ | |
public abstract string UniqueContextId { get; } | |
public abstract TraktConfigurationBase Configuration { get; } | |
public abstract int ApiVersion { get; set; } | |
public abstract bool ForceAuthorization { get; set; } | |
public abstract bool UseSandboxEnvironment { get; set; } | |
public abstract string ClientId { get; set; } | |
public abstract string ClientSecret { get; set; } | |
public abstract TraktAuthorization Authorization { get; set; } | |
} | |
} |
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
namespace Idea | |
{ | |
public class DebugContext : TraktContextBase | |
{ | |
// ... | |
} | |
public class ProductionContext : TraktContextBase | |
{ | |
// ... | |
} | |
// or | |
public class DebugContext : TraktContext | |
{ | |
// ... | |
} | |
public class ProductionContext : TraktContext | |
{ | |
// ... | |
} | |
} |
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
namespace Idea | |
{ | |
using System; | |
using TraktApiSharp.Authentication; | |
public class TraktConfiguration : TraktConfigurationBase | |
{ | |
public override int ApiVersion { get; set; } = 2; | |
public override bool UseSandboxEnvironment { get; set; } = false; | |
public override bool ForceAuthorization { get; set; } = false; | |
} | |
public class TraktContext : TraktContextBase | |
{ | |
private readonly string _uniqueId = Guid.NewGuid().ToString(); | |
public override string UniqueContextId => _uniqueId; | |
public override string ClientId { get; set; } = string.Empty; | |
public override string ClientSecret { get; set; } = string.Empty; | |
public override TraktAuthorization Authorization { get; set; } = new TraktAuthorization(); | |
public override TraktConfigurationBase Configuration { get; } = new TraktConfiguration(); | |
public override int ApiVersion | |
{ | |
get { return Configuration.ApiVersion; } | |
set { Configuration.ApiVersion = value; } | |
} | |
public override bool ForceAuthorization | |
{ | |
get { return Configuration.ForceAuthorization; } | |
set { Configuration.ForceAuthorization = value; } | |
} | |
public override bool UseSandboxEnvironment | |
{ | |
get { return Configuration.UseSandboxEnvironment; } | |
set { Configuration.UseSandboxEnvironment = value; } | |
} | |
public override string ToString() => UniqueContextId; | |
} | |
} |
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
namespace Idea | |
{ | |
public static class TraktContextBaseExtensions | |
{ | |
public static TraktMoviesModule GetMoviesModule(this TraktContextBase context) { // ... } | |
public static async Task<TraktMovie> GetMovieAsync(this TraktContextBase context, string movieId) { // ... } | |
} | |
} |
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
namespace Idea | |
{ | |
using TraktApiSharp.Authentication; | |
public interface ITraktConfiguration | |
{ | |
int ApiVersion { get; set; } | |
bool UseSandboxEnvironment { get; set; } | |
bool ForceAuthorization { get; set; } | |
} | |
public interface ITraktAuthorizationContext | |
{ | |
string ClientId { get; set; } | |
string ClientSecret { get; set; } | |
TraktAuthorization Authorization { get; set; } | |
} | |
public interface ITraktContext : ITraktConfiguration, ITraktAuthorizationContext | |
{ | |
string UniqueContextId { get; } | |
TraktConfigurationBase Configuration { get; } | |
} | |
} |
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
namespace Idea | |
{ | |
public sealed class TraktMoviesModule | |
{ | |
public TraktMoviesModule(ITraktContext context) { // ... } | |
public async Task<TraktMovie> GetMovieAsync(string movieId) { // ... } | |
public static async Task<TraktMovie> GetMovieAsync(ITraktContext context, string movieId) { // ... } | |
} | |
} |
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
namespace Idea | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
ITraktContext context = new TraktContext | |
{ | |
ClientId = "myClientId", | |
ClientSecret = "myClientSecret", | |
Authorization = TraktAuthorization.CreateWith("myAccessToken", "myRefreshToken"), | |
ApiVersion = 2, | |
ForceAuthorization = true, | |
UseSandboxEnvironment = true | |
}; | |
TraktMoviesModule moviesModule = new TraktMoviesModule(context); | |
TraktMovie movie = await moviesModule.GetMovieAsync("movie-id"); | |
// or | |
TraktMovie movie = await TraktMoviesModule.GetMovieAsync(context, "movie-id"); | |
// or, with extension methods for TraktContext | |
TraktMovie movie = await context.GetMovieAsync("movie-id"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment