using System;
using TINK.Model.Services.CopriApi;
using TINK.Repository;
namespace TINK.Model.Connector
{
///
/// Connects tink app to copri by getting data from copri and updating tink app model (i.e. bikes, user, ...)
///
public class Connector : IConnector
{
/// Constructs a copri connector object.
/// Uri to connect to.
/// Provides app related info (app name and version, merchantid) to pass to COPRI.
/// /// Holds the session cookie.
/// Mail of user.
/// Timespan which holds value after which cache expires.
/// Provides cached addess to copri.
public Connector(
Uri activeUri,
AppContextInfo appContextInfo,
string sessionCookie,
string mail,
TimeSpan? expiresAfter = null,
ICachedCopriServer server = null )
{
Command = GetCommand(
server ?? new CopriProviderHttps(activeUri, appContextInfo.MerchantId, appContextInfo, sessionCookie),
sessionCookie,
mail);
Query = GetQuery(
server ?? new CopriProviderHttps(activeUri, appContextInfo.MerchantId, appContextInfo, sessionCookie, expiresAfter),
sessionCookie,
mail);
}
/// Object for queriying stations and bikes.
public ICommand Command { get; private set; }
/// Object for queriying stations and bikes.
public IQuery Query { get; private set; }
/// True if connector has access to copri server, false if cached values are used.
public bool IsConnected => Command.IsConnected;
/// Gets a command object to perform copri commands.
public static ICommand GetCommand(ICopriServerBase copri, string sessioncookie, string mail) => string.IsNullOrEmpty(sessioncookie)
? new Command(copri)
: new CommandLoggedIn(copri, sessioncookie, mail, () => DateTime.Now) as ICommand;
/// Gets a command object to perform copri queries.
private static IQuery GetQuery(ICachedCopriServer copri, string sessioncookie, string mail) => string.IsNullOrEmpty(sessioncookie)
? new CachedQuery(copri) as IQuery
: new CachedQueryLoggedIn(copri, sessioncookie, mail, () => DateTime.Now);
}
}