//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOAuth.CommonConsumers {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetOAuth.ChannelElements;
///
/// A useful base class to derive from for Consumers written against a specific Service Provider.
///
public abstract class CommonConsumerBase {
///
/// Initializes a new instance of the class.
///
/// The service description.
/// The token manager.
/// The consumer key.
protected CommonConsumerBase(ServiceProviderDescription serviceDescription, ITokenManager tokenManager, string consumerKey) {
if (serviceDescription == null) {
throw new ArgumentNullException("serviceDescription");
}
if (tokenManager == null) {
throw new ArgumentNullException("tokenManager");
}
if (consumerKey == null) {
throw new ArgumentNullException("consumerKey");
}
this.Consumer = new WebConsumer(serviceDescription, tokenManager) {
ConsumerKey = consumerKey,
};
}
///
/// Gets the consumer.
///
protected WebConsumer Consumer { get; private set; }
///
/// Enumerates through the individual set bits in a flag enum.
///
/// The flags enum value.
/// An enumeration of just the set bits in the flags enum.
protected static IEnumerable GetIndividualFlags(Enum flags) {
long flagsLong = Convert.ToInt64(flags);
for (int i = 0; i < sizeof(long) * 8; i++) { // long is the type behind the largest enum
// Select an individual application from the scopes.
long individualFlagPosition = (long)Math.Pow(2, i);
long individualFlag = flagsLong & individualFlagPosition;
if (individualFlag == individualFlagPosition) {
yield return individualFlag;
}
}
}
}
}