sharee.bike-App/TINKLib/Model/Connector/Connector.cs

71 lines
3 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using System;
using TINK.Model.Services.CopriApi;
2021-06-26 20:57:55 +02:00
using TINK.Repository;
2021-05-13 20:03:07 +02:00
namespace TINK.Model.Connector
{
/// <summary>
2022-08-30 15:42:25 +02:00
/// Connects app to copri data by getting data from copri.
2021-05-13 20:03:07 +02:00
/// </summary>
public class Connector : IConnector
{
/// <summary>Constructs a copri connector object.</summary>
/// <param name="activeUri"> Uri to connect to.</param>
2022-01-04 18:54:03 +01:00
/// <param name="appContextInfo">Provides app related info (app name and version, merchantid) to pass to COPRI.</param>
2022-08-30 15:42:25 +02:00
/// <param name="uiIsoLangugageName">Two letter ISO language name.</param>
/// <param name="sessionCookie"> Holds the session cookie.</param>
/// <param name="mail">Mail of user.</param>
2021-05-13 20:03:07 +02:00
/// <param name="expiresAfter">Timespan which holds value after which cache expires.</param>
2022-08-30 15:42:25 +02:00
/// <param name="server"> Is null in production and migh be a mock in testing context.</param>
2021-05-13 20:03:07 +02:00
public Connector(
Uri activeUri,
2022-01-04 18:54:03 +01:00
AppContextInfo appContextInfo,
2022-08-30 15:42:25 +02:00
string uiIsoLangugageName,
2021-05-13 20:03:07 +02:00
string sessionCookie,
string mail,
TimeSpan? expiresAfter = null,
2022-08-30 15:42:25 +02:00
ICachedCopriServer server = null)
2021-05-13 20:03:07 +02:00
{
Command = GetCommand(
2022-08-30 15:42:25 +02:00
server ?? new CopriProviderHttps(
activeUri,
appContextInfo.MerchantId,
appContextInfo,
uiIsoLangugageName,
sessionCookie),
sessionCookie,
2021-05-13 20:03:07 +02:00
mail);
Query = GetQuery(
2022-08-30 15:42:25 +02:00
server ?? new CopriProviderHttps(
activeUri,
appContextInfo.MerchantId,
appContextInfo,
uiIsoLangugageName,
sessionCookie,
expiresAfter),
sessionCookie,
2021-05-13 20:03:07 +02:00
mail);
}
/// <summary> Object for queriying stations and bikes.</summary>
public ICommand Command { get; private set; }
/// <summary> Object for queriying stations and bikes.</summary>
public IQuery Query { get; private set; }
/// <summary> True if connector has access to copri server, false if cached values are used. </summary>
public bool IsConnected => Command.IsConnected;
/// <summary> Gets a command object to perform copri commands. </summary>
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;
/// <summary> Gets a command object to perform copri queries. </summary>
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);
}
}