From b46cc563621881dbfebff87d5a2e56d8fdd0dead Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Sun, 11 May 2014 22:50:06 +0200 Subject: [PATCH] implement ParserAPI func --- PocketSharp/Components/Get.cs | 28 +++++++++++++++++++++++++++ PocketSharp/IPocketClient.cs | 15 +++++++++++++++ PocketSharp/PocketClient.cs | 35 +++++++++++++++++++++++++++++++--- PocketSharp/PocketSharp.csproj | 1 + 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/PocketSharp/Components/Get.cs b/PocketSharp/Components/Get.cs index 70e40ec..6f1538f 100644 --- a/PocketSharp/Components/Get.cs +++ b/PocketSharp/Components/Get.cs @@ -196,6 +196,34 @@ namespace PocketSharp cancellationToken: cancellationToken ); } + + + /// + /// Retrieves the article content from an URI + /// WARNING: + /// You have to pass the parseUri in the PocketClient ctor for this method to work. + /// This is a private API and can only be used by authenticated users. + /// + /// The article URI. + /// Include images into content or use placeholder. + /// Include videos into content or use placeholder. + /// Force refresh of the content (don't use cache). + /// The cancellation token. + /// + /// + public async Task GetArticle(Uri uri, bool includeImages = true, bool includeVideos = true, bool forceRefresh = false, CancellationToken cancellationToken = default(CancellationToken)) + { + Dictionary parameters = new Dictionary() + { + { "url", uri.OriginalString }, + { "images", includeImages ? "1" : "0" }, + { "videos", includeVideos ? "1" : "0" }, + { "refresh", forceRefresh ? "1" : "0" }, + { "output", "json" } + }; + + return await Request("", cancellationToken, parameters, false, true); + } } diff --git a/PocketSharp/IPocketClient.cs b/PocketSharp/IPocketClient.cs index caefcab..479fb60 100644 --- a/PocketSharp/IPocketClient.cs +++ b/PocketSharp/IPocketClient.cs @@ -196,6 +196,21 @@ namespace PocketSharp /// Search string length has to be a minimum of 2 chars /// Task> Search(string searchString, string tag = null, CancellationToken cancellationToken = default(CancellationToken)); + + /// + /// Retrieves the article content from an URI + /// WARNING: + /// You have to pass the parseUri in the PocketClient ctor for this method to work. + /// This is a private API and can only be used by authenticated users. + /// + /// The article URI. + /// Include images into content or use placeholder. + /// Include videos into content or use placeholder. + /// Force refresh of the content (don't use cache). + /// The cancellation token. + /// + /// + Task GetArticle(Uri uri, bool includeImages = true, bool includeVideos = true, bool forceRefresh = false, CancellationToken cancellationToken = default(CancellationToken)); #endregion #region modify methods diff --git a/PocketSharp/PocketClient.cs b/PocketSharp/PocketClient.cs index 5e9bcb5..d4fe6a3 100644 --- a/PocketSharp/PocketClient.cs +++ b/PocketSharp/PocketClient.cs @@ -42,6 +42,11 @@ namespace PocketSharp /// protected string authentificationUri = "https://getpocket.com/auth/authorize?request_token={0}&redirect_uri={1}&mobile={2}"; + /// + /// The parser API URL + /// + protected Uri parserUri = null; + /// /// Indicates, whether this client is used for mobile or desktop /// @@ -98,13 +103,15 @@ namespace PocketSharp /// The HttpMessage handler. /// Request timeout (in seconds). /// Indicates, whether this client is used for mobile or desktop + /// Enables the wrapper for the private Text Parser API public PocketClient( string consumerKey, string accessCode = null, string callbackUri = null, HttpMessageHandler handler = null, int? timeout = null, - bool isMobileClient = true) + bool isMobileClient = true, + Uri parserUri = null) { // assign public properties ConsumerKey = consumerKey; @@ -123,6 +130,12 @@ namespace PocketSharp CallbackUri = Uri.EscapeUriString(callbackUri.ToString()); } + // assign text parser uri if submitted + if (parserUri != null) + { + this.parserUri = parserUri; + } + // initialize REST client _restClient = new HttpClient(handler ?? new HttpClientHandler() { @@ -159,13 +172,29 @@ namespace PocketSharp string method, CancellationToken cancellationToken, Dictionary parameters = null, - bool requireAuth = true) where T : class, new() + bool requireAuth = true, + bool isReaderRequest = false) where T : class, new() { if (requireAuth && AccessCode == null) { throw new PocketException("SDK error: No access token available. Use authentication first."); } + // rewrite base if it is a request to the Parser API + if (isReaderRequest) + { + if (parserUri == null) + { + throw new PocketException("Please pass the parserUri in the PocketClient ctor."); + } + + _restClient.BaseAddress = parserUri; + } + else + { + _restClient.BaseAddress = baseUri; + } + // every single Pocket API endpoint requires HTTP POST data HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, method); HttpResponseMessage response = null; @@ -179,7 +208,7 @@ namespace PocketSharp parameters.Add("consumer_key", ConsumerKey); // add access token (necessary for all requests except authentification) - if (AccessCode != null) + if (AccessCode != null && requireAuth) { parameters.Add("access_token", AccessCode); } diff --git a/PocketSharp/PocketSharp.csproj b/PocketSharp/PocketSharp.csproj index f25936e..bdaa3e9 100644 --- a/PocketSharp/PocketSharp.csproj +++ b/PocketSharp/PocketSharp.csproj @@ -63,6 +63,7 @@ +