Quantcast
Channel: Tweetinvi a friendly Twitter C# library
Viewing all articles
Browse latest Browse all 4126

New Post: Suggestions on Async Operation where each Operation Could Have Different Credentials

$
0
0
Based on your work and deep understanding of this framework (creator :))....

What potential issues would I have if I added something like the following class below.... and then switched the code to actually make the outer part async... and used the sync operation for the inner operation ??. I want to insure the credentials are passed all the way down on the actual web query building:
                TwitterCredentialsAsync.ExecuteOperationWithCredentials(credentials, () =>
                {
                        var tweet = Tweet.PublishTweet(message);
}


using System;
using Tweetinvi.Core.Interfaces.Credentials;
using Tweetinvi.Core.Interfaces.Factories;
using Tweetinvi.Core.Interfaces.oAuth;
using System.Threading.Tasks;

namespace Tweetinvi
{
public class TwitterCredentialsAsync
{
    private static readonly ICredentialsFactory _credentialsFactory;

    [ThreadStatic]
    private static ICredentialsAccessor _credentialsAccessor;
    public static ICredentialsAccessor CredentialsAccessor
    {
        get
        {
            if (_credentialsAccessor == null)
            {
                Initialize();
            }

            return _credentialsAccessor;
        }
    }

    static TwitterCredentialsAsync()
    {
        _credentialsFactory = TweetinviContainer.Resolve<ICredentialsFactory>();
    }

    private static void Initialize()
    {
        _credentialsAccessor = TweetinviContainer.Resolve<ICredentialsAccessor>();
    }

    public static IOAuthCredentials ApplicationCredentials
    {
        get { return CredentialsAccessor.ApplicationCredentials; }
        set { CredentialsAccessor.ApplicationCredentials = value; }
    }

    public static IOAuthCredentials Credentials
    {
        get { return CredentialsAccessor.CurrentThreadCredentials; }
        set { CredentialsAccessor.CurrentThreadCredentials = value; }
    }

    public static IOAuthCredentials CreateCredentials(string userAccessToken, string userAccessSecret, string consumerKey, string consumerSecret)
    {
        return _credentialsFactory.CreateOAuthCredentials(userAccessToken, userAccessSecret, consumerKey, consumerSecret);
    }

    public static void SetCredentials(string userAccessToken, string userAccessSecret, string consumerKey, string consumerSecret)
    {
        Credentials = CreateCredentials(userAccessToken, userAccessSecret, consumerKey, consumerSecret);
    }

    public static void SetCredentials(IOAuthCredentials credentials)
    {
        Credentials = credentials;
    }

    public static async Task<T> ExecuteOperationWithCredentials<T>(IOAuthCredentials credentials, Func<T> operation)
    {
        return await Sync.ExecuteTaskAsync(() => CredentialsAccessor.ExecuteOperationWithCredentials(credentials, operation));
    }

    public static async Task ExecuteOperationWithCredentials(IOAuthCredentials credentials, Action operation)
    {
        await Sync.ExecuteTaskAsync(() => CredentialsAccessor.ExecuteOperationWithCredentials(credentials, operation));
    }
}
}

Viewing all articles
Browse latest Browse all 4126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>