Skip to content

Instantly share code, notes, and snippets.

@henrikfroehling
Last active January 25, 2017 23:50
Show Gist options
  • Save henrikfroehling/1ad0402197f4134ed71dfde3976b98ab to your computer and use it in GitHub Desktop.
Save henrikfroehling/1ad0402197f4134ed71dfde3976b98ab to your computer and use it in GitHub Desktop.
TraktApiSharp Idea: Context-based usage approach
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; }
}
}
namespace Idea
{
public class DebugContext : TraktContextBase
{
// ...
}
public class ProductionContext : TraktContextBase
{
// ...
}
// or
public class DebugContext : TraktContext
{
// ...
}
public class ProductionContext : TraktContext
{
// ...
}
}
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;
}
}
namespace Idea
{
public static class TraktContextBaseExtensions
{
public static TraktMoviesModule GetMoviesModule(this TraktContextBase context) { // ... }
public static async Task<TraktMovie> GetMovieAsync(this TraktContextBase context, string movieId) { // ... }
}
}
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; }
}
}
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) { // ... }
}
}
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