/// <summary> /// Saves a single article and updates the progress indicator when done. /// </summary> /// <param name="bookmark">The bookmark that has information about the article.</param> /// <param name="readabilityClient">The client to use for connection.</param> /// <param name="local">The folder representing the user's local storage.</param> /// <param name="saveCompleteAction">An callback when save is complete.</param> /// <returns>An awaitable task.</returns> async Task SaveArticle(Bookmark bookmark, ReadabilityClient readabilityClient, StorageFolder local, Action saveCompleteAction) { var article = await readabilityClient.ArticleEndpoint.GetArticle(bookmark); var folder = await local.CreateFolderAsync(bookmark.Article.Id, CreationCollisionOption.OpenIfExists); try { var articleFile = await folder.CreateFileAsync(bookmark.Article.Id, CreationCollisionOption.FailIfExists); var articleStream = await articleFile.OpenStreamForWriteAsync(); using (var innerWriter = new BinaryWriter(articleStream)) { var bson = new BsonWriter(innerWriter); var innerSerializer = new JsonSerializer(); innerSerializer.Serialize(bson, article); } } catch { //swallow exception? } if (null != saveCompleteAction) { saveCompleteAction(); } }
/// <summary> /// Adds a given article to the reading list. /// </summary> /// <param name="url">The url to save.</param> /// <returns>An awaitable task.</returns> public async Task AddBookmark(string url) { var request = new RestRequest("bookmarks"); request.AddParameter("url", url); request.Method = Method.POST; await ReadabilityClient.MakeRequestAsync(request); }
/// <summary> /// Gets the current user from Readability. /// </summary> public async Task <User> GetCurrentUser() { var request = new RestRequest("/users/_current"); var content = await ReadabilityClient.MakeRequestAsync(request); var js = new StringReader(content); var jr = new JsonTextReader(js); var serializer = new JsonSerializer(); return(serializer.Deserialize <User>(jr)); }
/// <summary> /// Gets a specific article from a bookmark based on its href. /// </summary> /// <param name="bookmark">The bookmark that has the article to be returned.</param> /// <returns>The full article as a string. This is useful for immediately saving it.</returns> public async Task <Article> GetArticle(Bookmark bookmark) { var request = new RestRequest(bookmark.ArticleHref.Replace("/api/rest/v1", "")); var content = await ReadabilityClient.MakeRequestAsync(request).ConfigureAwait(false); var js = new StringReader(content); var jr = new JsonTextReader(js); var serializer = new JsonSerializer(); return(serializer.Deserialize <Article>(jr)); }
/// <summary> /// Complete authentication with a manually entered string. /// </summary> /// <param name="verifierKey">The string that user entered manually.</param> /// <param name="callback">A function to call when the user has been added.</param> public async Task <User> CompleteAuth(string verifierKey) { var result = await ReadabilityClient.GetAccessToken(verifierKey); if (result == true) { return(await GetCurrentUser()); } else { return(null); } }
/// <summary> /// Gets a single page of bookmarks. /// </summary> /// <param name="conditions">The conditions of the request. It gets and converted to a rest request.</param> public async Task <BookmarkList> GetBookmarks(Conditions conditions) { var request = BuildRequestFromConditions(conditions); request.Resource = "bookmarks"; var content = await ReadabilityClient.MakeRequestAsync(request).ConfigureAwait(false); var js = new StringReader(content); var jr = new JsonTextReader(js); var serializer = new JsonSerializer(); var bookmarkList = serializer.Deserialize <BookmarkList>(jr); return(bookmarkList); }
/// <summary> /// Archives or unarchives an article. /// </summary> /// <param name="bookmark">The bookmark to be archived or unarchived.</param> /// <returns>An awaitable task.</returns> public async Task ToggleArchive(Bookmark bookmark) { var request = new RestRequest("bookmarks/" + bookmark.Id, Method.POST); if (bookmark.Archive) { request.AddParameter("archive", 0); } else { request.AddParameter("archive", 1); } await ReadabilityClient.MakeRequestAsync(request); }
/// <summary> /// Saves the given bookmark list to a file and downloads all the articles in those bookmarks. /// </summary> /// <param name="readabilityClient">The readability client to use to request the documents.</param> /// <param name="bookmarkList">The bookmark list to be saved.</param> /// <returns>A simple task to make this awaitable.</returns> public async Task SaveBookmarkList(ReadabilityClient readabilityClient, BookmarkList masterList) { StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; var file = await local.CreateFileAsync("bookmarks", CreationCollisionOption.ReplaceExisting); var fileStream = await file.OpenStreamForWriteAsync(); using (var writer = new BsonWriter(new BinaryWriter(fileStream))) { var serializer = new JsonSerializer(); serializer.Serialize(writer, masterList); } }
/// <summary> /// Complete the authentication with a URI. /// </summary> /// <param name="uri">The URI that finished the auth process. We will parse this for the verification key.</param> /// <param name="callback">A function to call when the user has been added.</param> public async Task <User> CompleteAuth(Uri uri) { if (uri.OriginalString.Contains("oauth_callback")) { var decodedUri = System.Net.HttpUtility.UrlDecode(uri.OriginalString); var arguments = decodedUri.Split('?'); if (arguments.Length < 1) { return(null); } // 2 is the second part of the url (after the second question mark) var verifierKey = Utilities.GetQueryParameter(arguments[2], "oauth_verifier"); await ReadabilityClient.GetAccessToken(verifierKey); return(await GetCurrentUser()); } return(null); }
internal ArticleEndpoint(ReadabilityClient readabilityClient) : base(readabilityClient) { }
internal BookmarkListEndpoint(ReadabilityClient readabilityClient) : base(readabilityClient) { }
/// <summary> /// Deletes an bookmark. /// </summary> /// <param name="bookmark">The bookmark to be deleted.</param> /// <returns>An awaitable task.</returns> public async Task Delete(Bookmark bookmark) { var request = new RestRequest("bookmarks/" + bookmark.Id, Method.DELETE); await ReadabilityClient.MakeRequestAsync(request); }
/// <summary> /// Begin authentication for the user. /// </summary> public void BeginAuth() { ReadabilityClient.BeginAuth(); }
internal IEndpoint(ReadabilityClient readabilityClient) { ReadabilityClient = readabilityClient; }
internal UserEndpoint(ReadabilityClient readabilityClient) : base(readabilityClient) { }
/// <summary> /// Generic function to make requests for protected data. /// </summary> /// <param name="request">The request to be made.</param> /// <param name="callback">A function to call when done with the request.</param> protected async void MakeRequest(RestRequest request) { await ReadabilityClient.MakeRequestAsync(request); }