using PocketSharp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace PocketSharp
{
///
/// PocketClient
///
public partial class PocketClient
{
///
/// Retrieves items from pocket
/// with the given filters
///
/// The state.
/// The favorite.
/// The tag.
/// Type of the content.
/// The sort.
/// The search.
/// The domain.
/// The since.
/// The count.
/// The offset.
/// The cancellation token.
///
///
public async Task> Get(
State? state = null,
bool? favorite = null,
string tag = null,
ContentType? contentType = null,
Sort? sort = null,
string search = null,
string domain = null,
DateTime? since = null,
int? count = null,
int? offset = null,
CancellationToken cancellationToken = default(CancellationToken)
)
{
RetrieveParameters parameters = new RetrieveParameters()
{
State = state,
Favorite = favorite,
Tag = tag,
ContentType = contentType,
Sort = sort,
DetailType = DetailType.complete,
Search = search,
Domain = domain,
Since = since.HasValue ? ((DateTime)since).ToUniversalTime() : since,
Count = count,
Offset = offset
};
return (await Request("get", cancellationToken, parameters.Convert())).Items ?? new List();
}
///
/// Retrieves an item by a given ID
/// Note: The Pocket API contains no method, which allows to retrieve a single item, so all items are retrieved and filtered locally by the ID.
///
/// The item ID.
/// The cancellation token.
///
///
public async Task Get(string itemID, CancellationToken cancellationToken = default(CancellationToken))
{
return (await Get(
cancellationToken: cancellationToken,
state: State.all
)).SingleOrDefault(item => item.ID == itemID);
}
///
/// Retrieves all items by a given filter
///
/// The filter.
/// The cancellation token.
///
///
public async Task> Get(RetrieveFilter filter, CancellationToken cancellationToken = default(CancellationToken))
{
RetrieveParameters parameters = new RetrieveParameters();
switch (filter)
{
case RetrieveFilter.Article:
parameters.ContentType = ContentType.article;
break;
case RetrieveFilter.Image:
parameters.ContentType = ContentType.image;
break;
case RetrieveFilter.Video:
parameters.ContentType = ContentType.video;
break;
case RetrieveFilter.Favorite:
parameters.Favorite = true;
break;
case RetrieveFilter.Unread:
parameters.State = State.unread;
break;
case RetrieveFilter.Archive:
parameters.State = State.archive;
break;
case RetrieveFilter.All:
parameters.State = State.all;
break;
}
parameters.DetailType = DetailType.complete;
return (await Request("get", cancellationToken, parameters.Convert())).Items;
}
///
/// Converts a raw JSON response to a PocketItem list
///
/// The raw JSON response.
///
///
public IEnumerable ConvertJsonToList(string itemsJSON)
{
return DeserializeJson(itemsJSON).Items;
}
///
/// Retrieves all available tags.
/// Note: The Pocket API contains no method, which allows to retrieve all tags, so all items are retrieved and the associated tags extracted.
///
/// The cancellation token.
///
///
public async Task> GetTags(CancellationToken cancellationToken = default(CancellationToken))
{
IEnumerable items = await Get(
cancellationToken: cancellationToken,
state: State.all
);
return items
.Where(item => item.Tags != null)
.SelectMany(item => item.Tags)
.GroupBy(item => item.Name)
.Select(item => item.First())
.ToList();
}
///
/// Retrieves items by tag
///
/// The tag.
/// The cancellation token.
///
///
public async Task> SearchByTag(string tag, CancellationToken cancellationToken = default(CancellationToken))
{
return await Get(
state: State.all,
cancellationToken: cancellationToken,
tag: tag
);
}
///
/// Retrieves items which match the specified search string in title and URI
///
/// The search string.
/// Filter by tag.
/// The cancellation token.
///
/// Search string length has to be a minimum of 2 chars
///
public async Task> Search(string searchString, string tag = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (String.IsNullOrEmpty(searchString) || searchString.Length < 2)
{
throw new ArgumentOutOfRangeException("Search string length has to be a minimum of 2 chars");
}
return await Get(
state: State.all,
search: searchString,
tag: tag,
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);
}
}
///
/// Filter for simple retrieve requests
///
public enum RetrieveFilter
{
///
/// All types
///
All,
///
/// Only unread items
///
Unread,
///
/// Archived items
///
Archive,
///
/// Favorited items
///
Favorite,
///
/// Only articles
///
Article,
///
/// Only videos
///
Video,
///
/// Only images
///
Image
}
}