From 50d896c6703228f6c6743ae5cccc092d9cff2d18 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Fri, 8 Nov 2013 01:12:34 +0100 Subject: [PATCH] cancellationToken for Add and Account methods --- PocketSharp.Tests/StressTests.cs | 4 +- PocketSharp/Components/Account.cs | 72 +++++++++++++++++++++++-------- PocketSharp/Components/Add.cs | 22 +++++++++- PocketSharp/IPocketClient.cs | 68 +++++++++++++++++++++++------ 4 files changed, 132 insertions(+), 34 deletions(-) diff --git a/PocketSharp.Tests/StressTests.cs b/PocketSharp.Tests/StressTests.cs index 8395fae..4287c8d 100644 --- a/PocketSharp.Tests/StressTests.cs +++ b/PocketSharp.Tests/StressTests.cs @@ -29,9 +29,9 @@ namespace PocketSharp.Tests [Fact] - public async Task Are100ItemsRetrievedProperly() + public async Task Are100IdingtemsRetrievedProperly() { - // await FillAccount(1577, 10000); + await FillAccount(3817, 10000); } [Fact] diff --git a/PocketSharp/Components/Account.cs b/PocketSharp/Components/Account.cs index 0810500..cac5889 100644 --- a/PocketSharp/Components/Account.cs +++ b/PocketSharp/Components/Account.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; namespace PocketSharp @@ -18,6 +19,19 @@ namespace PocketSharp /// Authentication methods need a callbackUri on initialization of the PocketClient class /// public async Task GetRequestCode() + { + return await GetRequestCode(CancellationToken.None); + } + + + /// + /// Retrieves the requestCode from Pocket, which is used to generate the Authentication URI to authenticate the user + /// + /// The cancellation token. + /// + /// Authentication methods need a callbackUri on initialization of the PocketClient class + /// + public async Task GetRequestCode(CancellationToken cancellationToken) { // check if request code is available if (CallbackUri == null) @@ -26,7 +40,7 @@ namespace PocketSharp } // do request - RequestCode response = await Request("oauth/request", new Dictionary() + RequestCode response = await Request("oauth/request", cancellationToken, new Dictionary() { { "redirect_uri", CallbackUri } }, false); @@ -64,29 +78,31 @@ namespace PocketSharp /// - /// Requests the access code after authentication - /// The access code has to permanently be stored within the users session, and should be passed in the constructor for all future PocketClient initializations. + /// Requests the access code and username after authentication + /// The access code has to permanently be stored within the users session, and should be passed in the constructor for all future PocketClient initializations. /// - /// The requestCode. If no requestCode is supplied, the property from the PocketClient intialization is used. - /// The permanent access code, which is used to authenticate the user with the application + /// The request code. + /// + /// The authenticated user + /// /// Call GetRequestCode() first to receive a request_code - /// - [Obsolete("Please use GetUser instead")] - public async Task GetAccessCode(string requestCode = null) + public async Task GetUser(string requestCode = null) { - await GetUser(requestCode); - return AccessCode; + return await GetUser(CancellationToken.None, requestCode); } /// /// Requests the access code and username after authentication - /// The access code has to permanently be stored within the users session, and should be passed in the constructor for all future PocketClient initializations. + /// The access code has to permanently be stored within the users session, and should be passed in the constructor for all future PocketClient initializations. /// + /// The cancellation token. /// The request code. - /// The authenticated user + /// + /// The authenticated user + /// /// Call GetRequestCode() first to receive a request_code - public async Task GetUser(string requestCode = null) + public async Task GetUser(CancellationToken cancellationToken, string requestCode = null) { // check if request code is available if (RequestCode == null && requestCode == null) @@ -101,7 +117,7 @@ namespace PocketSharp } // do request - PocketUser response = await Request("oauth/authorize", new Dictionary() + PocketUser response = await Request("oauth/authorize", cancellationToken, new Dictionary() { {"code", RequestCode} }, false); @@ -122,15 +138,35 @@ namespace PocketSharp /// The password. /// /// All parameters are required - /// - /// Invalid email address. + /// Invalid email address. /// or /// Invalid username. Please only use letters, numbers, and/or dashes and between 1-20 characters. /// or - /// Invalid password. - /// + /// Invalid password. /// public async Task RegisterAccount(string username, string email, string password) + { + return await RegisterAccount(CancellationToken.None, username, email, password); + } + + + /// + /// Registers a new account. + /// Account has to be activated via a activation email sent by Pocket. + /// + /// The cancellation token. + /// The username. + /// The email. + /// The password. + /// + /// All parameters are required + /// Invalid email address. + /// or + /// Invalid username. Please only use letters, numbers, and/or dashes and between 1-20 characters. + /// or + /// Invalid password. + /// + public async Task RegisterAccount(CancellationToken cancellationToken, string username, string email, string password) { if (username == null || email == null || password == null) { diff --git a/PocketSharp/Components/Add.cs b/PocketSharp/Components/Add.cs index 598c3e0..18418f0 100644 --- a/PocketSharp/Components/Add.cs +++ b/PocketSharp/Components/Add.cs @@ -1,5 +1,6 @@ using PocketSharp.Models; using System; +using System.Threading; using System.Threading.Tasks; namespace PocketSharp @@ -22,6 +23,25 @@ namespace PocketSharp /// (1) Uri should be absolute. /// public async Task Add(Uri uri, string[] tags = null, string title = null, string tweetID = null) + { + return await Add(CancellationToken.None, uri, tags, title, tweetID); + } + + + /// + /// Adds a new item to pocket + /// + /// The cancellation token. + /// The URL of the item you want to save + /// A comma-separated list of tags to apply to the item + /// This can be included for cases where an item does not have a title, which is typical for image or PDF URLs. If Pocket detects a title from the content of the page, this parameter will be ignored. + /// If you are adding Pocket support to a Twitter client, please send along a reference to the tweet status id. This allows Pocket to show the original tweet alongside the article. + /// + /// A simple representation of the saved item which doesn't contain all data (is only returned by calling the Retrieve method) + /// + /// (1) Uri should be absolute. + /// + public async Task Add(CancellationToken cancellationToken, Uri uri, string[] tags = null, string title = null, string tweetID = null) { if (!uri.IsAbsoluteUri) { @@ -36,7 +56,7 @@ namespace PocketSharp TweetID = tweetID }; - Add response = await Request("add", parameters.Convert()); + Add response = await Request("add", cancellationToken, parameters.Convert()); return response.Item; } diff --git a/PocketSharp/IPocketClient.cs b/PocketSharp/IPocketClient.cs index 0696fcb..f35c389 100644 --- a/PocketSharp/IPocketClient.cs +++ b/PocketSharp/IPocketClient.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using PocketSharp.Models; +using System.Threading; namespace PocketSharp { @@ -51,6 +52,15 @@ namespace PocketSharp /// Task GetRequestCode(); + /// + /// Retrieves the requestCode from Pocket, which is used to generate the Authentication URI to authenticate the user + /// + /// The cancellation token. + /// + /// Authentication methods need a callbackUri on initialization of the PocketClient class + /// + Task GetRequestCode(CancellationToken cancellationToken); + /// /// Generate Authentication URI from requestCode /// @@ -61,19 +71,6 @@ namespace PocketSharp /// Call GetRequestCode() first to receive a request_code Uri GenerateAuthenticationUri(string requestCode = null); - /// - /// Requests the access code after authentication - /// The access code has to permanently be stored within the users session, and should be passed in the constructor for all future PocketClient initializations. - /// - /// The requestCode. If no requestCode is supplied, the property from the PocketClient intialization is used. - /// - /// The permanent access code, which is used to authenticate the user with the application - /// - /// Call GetRequestCode() first to receive a request_code - /// - [Obsolete("Please use GetUser instead")] - Task GetAccessCode(string requestCode = null); - /// /// Requests the access code and username after authentication /// The access code has to permanently be stored within the users session, and should be passed in the constructor for all future PocketClient initializations. @@ -85,6 +82,18 @@ namespace PocketSharp /// Call GetRequestCode() first to receive a request_code Task GetUser(string requestCode = null); + /// + /// Requests the access code and username after authentication + /// The access code has to permanently be stored within the users session, and should be passed in the constructor for all future PocketClient initializations. + /// + /// The cancellation token. + /// The request code. + /// + /// The authenticated user + /// + /// Call GetRequestCode() first to receive a request_code + Task GetUser(CancellationToken cancellationToken, string requestCode = null); + /// /// Registers a new account. /// Account has to be activated via a activation email sent by Pocket. @@ -103,6 +112,24 @@ namespace PocketSharp /// /// Task RegisterAccount(string username, string email, string password); + + /// + /// Registers a new account. + /// Account has to be activated via a activation email sent by Pocket. + /// + /// The cancellation token. + /// The username. + /// The email. + /// The password. + /// + /// All parameters are required + /// Invalid email address. + /// or + /// Invalid username. Please only use letters, numbers, and/or dashes and between 1-20 characters. + /// or + /// Invalid password. + /// + Task RegisterAccount(CancellationToken cancellationToken, string username, string email, string password); #endregion #region add methods @@ -119,6 +146,21 @@ namespace PocketSharp /// (1) Uri should be absolute. /// Task Add(Uri uri, string[] tags = null, string title = null, string tweetID = null); + + /// + /// Adds a new item to pocket + /// + /// The cancellation token. + /// The URL of the item you want to save + /// A comma-separated list of tags to apply to the item + /// This can be included for cases where an item does not have a title, which is typical for image or PDF URLs. If Pocket detects a title from the content of the page, this parameter will be ignored. + /// If you are adding Pocket support to a Twitter client, please send along a reference to the tweet status id. This allows Pocket to show the original tweet alongside the article. + /// + /// A simple representation of the saved item which doesn't contain all data (is only returned by calling the Retrieve method) + /// + /// (1) Uri should be absolute. + /// + Task Add(CancellationToken cancellationToken, Uri uri, string[] tags = null, string title = null, string tweetID = null); #endregion #region get methods