Geeks With Blogs

News
Joe Mayo

The next version of LINQ to Twitter will be async. I’m currently doing some early spikes and everything is coming together nicely. Here’s a sample of what the syntax looks like, starting with Authorization. Here’s a standard PinAuthorizer, which allows you to authorize from console, desktop, or Windows Phone applications:

        async static Task TestLinqToTwitterAsync()
        {
            var credStore = new InMemoryCredentialStore
            {
                ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]
            };

            var auth = new PinAuthorizer(credStore, false, AuthAccessType.NoChange, null)
            {
                GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
                GetPin = () =>
                {
                    // this executes after user authorizes, which begins with the call to auth.Authorize() below.
                    Console.WriteLine("\nAfter authorizing this application, Twitter will give you a 7-digit PIN Number.\n");
                    Console.Write("Enter the PIN number here: ");
                    return Console.ReadLine();
                }
            };

            await auth.AuthorizeAsync();

What’s interesting about the PinAuthorizer above is that after it’s configured, you kick off the authorization process by awaiting the AuthorizeAsync method. After the user redirects to Twitter, as specified by GoToTwitterAuthorization, the user gets a PIN and gives it to the authorizer via GetPin. After authorization is complete, you have a valid authorizer to use on all subsequent queries. Below, you can see the authorizer passed to TwitterContext. This allows the code to perform a Status/Home query, which returns the authenticated user’s home timeline from Twitter.

            var ctx = new TwitterContext(auth);

            var tweets = await
                (from tweet in ctx.Status
                 where tweet.Type == StatusType.Home
                 select tweet)
                .ToListAsync();

            tweets.ForEach(tweet =>
                Console.WriteLine("\nName:\n{0}\nTweet:{1}\n", tweet.ScreenName, tweet.Text));

The key syntax in the query above is a new operator named ToListAsync. Accompany this operator with await and you have an async LINQ query. Similarly, the code below shows another operator, FirstOrDefaultAsync:

            var firstTweet = await
                (from twt in ctx.Status
                 where twt.Type == StatusType.Home
                 select twt)
                .FirstOrDefaultAsync();

            Console.WriteLine("\nName:\n{0}\nTweet:{1}\n", firstTweet.ScreenName, firstTweet.Text);
        }

Again, notice the use of await to initiate async behavior.

This is async all the way down to the HTTP call for  the Twitter API endpoint. So, now you get the performance, responsiveness, and scalability of async with the convenience of LINQ to interact with the Twitter API.

BTW, this code will be deployed as a Portable Class Library (PCL). You can download the source from the LINQ to Twitter site on CodePlex.com and find the source in the LinqToTwitterPcl project.

This is all very early and I have a lot of work to clean up, tweak the design, and support all of Twitter’s API. In so being, this is also a good time for you to discuss any design issues that you care about. What could be better? What is your greatest pain point? What do you think?

 

@JoeMayo

Posted on Friday, October 4, 2013 9:23 AM Twitter , LINQ , LINQ to Twitter , Async , PCL | Back to top

Copyright © Joe Mayo | Powered by: GeeksWithBlogs.net