using MessengerApi.Model;
using portaloggy;
namespace MessengerApi
{
///
/// This exists so you can mock it.
///
public interface ISubscriptionClient : IDisposable
{
///
/// Subscribes to given message type mask. Thread-safe.
///
/// Expected format of mask: "MY-MESSAGE-TYPE". No wildcards or placeholders, we only compare MessageType.StartsWith using this value.
Subscription Subscribe(string payloadTypeMask);
///
/// Unsubscribes from given subscriptions. Thread-safe.
///
void Unsubscribe(Subscription subscription);
}
///
/// This is where you begin. Instantiate one of these and start subscribing with .
///
public class SubscriptionClient : Client, ISubscriptionClient
{
private readonly SubscriptionPollingEngine _engine;
private bool _isDisposed;
public SubscriptionClient(Credentials credentials, HttpClient client = null, ILogger logger = null) : base(credentials, client, logger)
{
this._engine = new SubscriptionPollingEngine(this._logger, this);
}
public void Dispose()
{
if (this._isDisposed)
{
return;
}
this._engine.Dispose();
this._isDisposed = true;
}
public Subscription Subscribe(string messageTypeMask)
{
var sub = new Subscription(messageTypeMask, this);
this._engine.AddSubscription(sub);
return sub;
}
public void Unsubscribe(Subscription subscription)
{
this._engine.RemoveSubscription(subscription);
}
}
}