14 Commits

27 changed files with 1127 additions and 261 deletions
+17 -1
View File
@@ -6,9 +6,25 @@ namespace PocketSharp
/// <summary>
/// custom Pocket API Exceptions
/// </summary>
[Serializable]
public class APIException : Exception
{
/// <summary>
/// Gets or sets the pocket error code.
/// </summary>
/// <value>
/// The pocket error code.
/// </value>
public int? PocketErrorCode { get; set; }
/// <summary>
/// Gets or sets the pocket error.
/// </summary>
/// <value>
/// The pocket error.
/// </value>
public string PocketError { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="APIException"/> class.
/// </summary>
+42
View File
@@ -0,0 +1,42 @@
using PocketSharp.Models;
using System;
namespace PocketSharp
{
/// <summary>
/// PocketClient
/// </summary>
public partial class PocketClient
{
/// <summary>
/// Adds a new item to pocket
/// </summary>
/// <param name="uri">The URL of the item you want to save</param>
/// <param name="tags">A comma-separated list of tags to apply to the item</param>
/// <param name="title">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.</param>
/// <param name="tweetID">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.</param>
/// <returns></returns>
public PocketItem Add(Uri uri, string[] tags = null, string title = null, string tweetID = null)
{
AddParameters parameters = new AddParameters()
{
Uri = uri,
Tags = tags,
Title = title,
TweetID = tweetID
};
return Get<Add>("add", parameters.Convert(), true).Item;
}
/// <summary>
/// Adds a new item to pocket
/// </summary>
/// <param name="uri">The URL of the item you want to save</param>
/// <returns></returns>
public PocketItem Add(Uri uri)
{
return Add(uri, null, null, null);
}
}
}
@@ -1,8 +1,6 @@
using PocketSharp.Models.Authentification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestSharp;
namespace PocketSharp
+21 -28
View File
@@ -1,5 +1,4 @@
using PocketSharp.Models;
using System.Collections.Generic;
namespace PocketSharp
{
@@ -15,7 +14,7 @@ namespace PocketSharp
/// <returns></returns>
public bool Archive(int itemID)
{
return PutAction(itemID, "archive");
return PutSendActionDefault(itemID, "archive");
}
@@ -30,28 +29,6 @@ namespace PocketSharp
}
/// <summary>
/// Un-archives the specified item.
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <returns></returns>
public bool Readd(int itemID)
{
return Unarchive(itemID);
}
/// <summary>
/// Un-archives the specified item.
/// </summary>
/// <param name="item">The item.</param>
/// <returns></returns>
public bool Readd(PocketItem item)
{
return Unarchive(item.ID);
}
/// <summary>
/// Un-archives the specified item (alias for Readd).
/// </summary>
@@ -59,7 +36,7 @@ namespace PocketSharp
/// <returns></returns>
public bool Unarchive(int itemID)
{
return PutAction(itemID, "readd");
return PutSendActionDefault(itemID, "readd");
}
@@ -81,7 +58,7 @@ namespace PocketSharp
/// <returns></returns>
public bool Favorite(int itemID)
{
return PutAction(itemID, "favorite");
return PutSendActionDefault(itemID, "favorite");
}
@@ -103,7 +80,7 @@ namespace PocketSharp
/// <returns></returns>
public bool Unfavorite(int itemID)
{
return PutAction(itemID, "unfavorite");
return PutSendActionDefault(itemID, "unfavorite");
}
@@ -125,7 +102,7 @@ namespace PocketSharp
/// <returns></returns>
public bool Delete(int itemID)
{
return PutAction(itemID, "delete");
return PutSendActionDefault(itemID, "delete");
}
@@ -138,5 +115,21 @@ namespace PocketSharp
{
return Delete(item.ID);
}
/// <summary>
/// Puts an action
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <param name="action">The action.</param>
/// <returns></returns>
protected bool PutSendActionDefault(int itemID, string action)
{
return PutSendAction(new ActionParameter()
{
Action = action,
ID = itemID
});
}
}
}
+140 -3
View File
@@ -1,5 +1,4 @@
using PocketSharp.Models;
using System.Collections.Generic;
namespace PocketSharp
{
@@ -8,9 +7,147 @@ namespace PocketSharp
/// </summary>
public partial class PocketClient
{
public bool AddTags()
/// <summary>
/// Adds the specified tags.
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <param name="tags">The tags.</param>
/// <returns></returns>
public bool AddTags(int itemID, string[] tags)
{
return true;
return PutSendActionForTags(itemID, "tags_add", tags);
}
/// <summary>
/// Adds the specified tags.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="tags">The tags.</param>
/// <returns></returns>
public bool AddTags(PocketItem item, string[] tags)
{
return AddTags(item.ID, tags);
}
/// <summary>
/// Removes the specified tags.
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <param name="tags">The tags.</param>
/// <returns></returns>
public bool RemoveTags(int itemID, string[] tags)
{
return PutSendActionForTags(itemID, "tags_remove", tags);
}
/// <summary>
/// Removes the specified tags.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="tags">The tags.</param>
/// <returns></returns>
public bool RemoveTags(PocketItem item, string[] tags)
{
return RemoveTags(item.ID, tags);
}
/// <summary>
/// Clears all tags.
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <returns></returns>
public bool RemoveTags(int itemID)
{
return PutSendActionDefault(itemID, "tags_clear");
}
/// <summary>
/// Clears all tags.
/// </summary>
/// <param name="item">The item.</param>
/// <returns></returns>
public bool RemoveTags(PocketItem item)
{
return RemoveTags(item.ID);
}
/// <summary>
/// Replaces the specified tags.
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <param name="tags">The tags.</param>
/// <returns></returns>
public bool ReplaceTags(int itemID, string[] tags)
{
return PutSendActionForTags(itemID, "tags_replace", tags);
}
/// <summary>
/// Replaces the specified tags.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="tags">The tags.</param>
/// <returns></returns>
public bool ReplaceTags(PocketItem item, string[] tags)
{
return ReplaceTags(item.ID, tags);
}
/// <summary>
/// Renames a tag.
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <param name="oldTag">The old tag.</param>
/// <param name="newTag">The new tag name.</param>
/// <returns></returns>
public bool RenameTag(int itemID, string oldTag, string newTag)
{
return PutSendAction(new ActionParameter()
{
Action = "tag_rename",
ID = itemID,
OldTag = oldTag,
NewTag = newTag
});
}
/// <summary>
/// Renames a tag.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="oldTag">The old tag.</param>
/// <param name="newTag">The new tag name.</param>
/// <returns></returns>
public bool RenameTag(PocketItem item, string oldTag, string newTag)
{
return RenameTag(item.ID, oldTag, newTag);
}
/// <summary>
/// Puts the send action for tags.
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <param name="action">The action.</param>
/// <param name="tags">The tags.</param>
/// <returns></returns>
protected bool PutSendActionForTags(int itemID, string action, string[] tags)
{
return PutSendAction(new ActionParameter()
{
Action = action,
ID = itemID,
Tags = tags
});
}
}
}
+26 -10
View File
@@ -15,8 +15,7 @@ namespace PocketSharp
/// <returns></returns>
public List<PocketItem> Retrieve(RetrieveParameters parameters)
{
ExpectAuthentification();
return Get<Retrieve>("get", parameters.Convert()).Items;
return Get<Retrieve>("get", parameters.Convert(), true).Items;
}
@@ -27,8 +26,6 @@ namespace PocketSharp
/// <returns></returns>
public List<PocketItem> Retrieve(RetrieveFilter filter = RetrieveFilter.All)
{
ExpectAuthentification();
RetrieveParameters parameters = new RetrieveParameters();
switch(filter)
@@ -55,7 +52,7 @@ namespace PocketSharp
//parameters.DetailType = DetailTypeEnum.complete;
return Get<Retrieve>("get", parameters.Convert()).Items;
return Get<Retrieve>("get", parameters.Convert(), true).Items;
}
@@ -66,30 +63,28 @@ namespace PocketSharp
/// <returns></returns>
public List<PocketItem> SearchByTag(string tag)
{
ExpectAuthentification();
RetrieveParameters parameters = new RetrieveParameters()
{
Tag = tag,
DetailType = DetailTypeEnum.complete
};
return Get<Retrieve>("get", parameters.Convert()).Items;
return Get<Retrieve>("get", parameters.Convert(), true).Items;
}
/// <summary>
/// Retrieves items from pocket which match the specified search string in title or content
/// </summary>
/// <param name="tag">The tag.</param>
/// <param name="searchString">The search string.</param>
/// <returns></returns>
public List<PocketItem> Search(string searchString)
{
ExpectAuthentification();
RetrieveParameters parameters = new RetrieveParameters()
{
Search = searchString,
DetailType = DetailTypeEnum.complete
};
return Get<Retrieve>("get", parameters.Convert()).Items;
return Get<Retrieve>("get", parameters.Convert(), true).Items;
}
}
@@ -99,12 +94,33 @@ namespace PocketSharp
/// </summary>
public enum RetrieveFilter
{
/// <summary>
/// All types
/// </summary>
All,
/// <summary>
/// Only unread items
/// </summary>
Unread,
/// <summary>
/// Archived items
/// </summary>
Archive,
/// <summary>
/// Favorited items
/// </summary>
Favorite,
/// <summary>
/// Only articles
/// </summary>
Article,
/// <summary>
/// Only videos
/// </summary>
Video,
/// <summary>
/// Only images
/// </summary>
Image
}
}
-1
View File
@@ -2,7 +2,6 @@
using RestSharp.Deserializers;
using ServiceStack.Text;
using System;
using System.Collections.Generic;
namespace PocketSharp
{
@@ -1,15 +1,28 @@
using RestSharp.Serializers;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization;
namespace PocketSharp.Models.Authentification
{
/// <summary>
/// Access Code
/// </summary>
[DataContract]
public class AccessCode
internal class AccessCode
{
/// <summary>
/// Gets or sets the code.
/// </summary>
/// <value>
/// The code.
/// </value>
[DataMember(Name = "access_token")]
public string Code { get; set; }
/// <summary>
/// Gets or sets the username.
/// </summary>
/// <value>
/// The username.
/// </value>
[DataMember]
public string Username { get; set; }
}
@@ -1,13 +1,29 @@
using RestSharp.Serializers;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization;
namespace PocketSharp.Models.Authentification
{
class RequestCode
/// <summary>
/// Request Code
/// </summary>
[DataContract]
internal class RequestCode
{
/// <summary>
/// Gets or sets the code.
/// </summary>
/// <value>
/// The code.
/// </value>
[DataMember]
public string Code { get; set; }
/// <summary>
/// Gets or sets the state.
/// </summary>
/// <value>
/// The state.
/// </value>
[DataMember]
public string State { get; set; }
}
}
@@ -1,30 +1,84 @@
using RestSharp;
using System;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
namespace PocketSharp.Models
{
public class ActionParameter : ParameterBase
/// <summary>
/// All parameters which can be passed for a modify action
/// </summary>
public class ActionParameter
{
/// <summary>
/// Gets or sets the action.
/// </summary>
/// <value>
/// The action.
/// </value>
public string Action { get; set; }
/// <summary>
/// Gets or sets the ID.
/// </summary>
/// <value>
/// The ID.
/// </value>
public int ID { get; set; }
/// <summary>
/// Gets or sets the time.
/// </summary>
/// <value>
/// The time.
/// </value>
public DateTime? Time { get; set; }
// specific params
/// <summary>
/// Gets or sets the tags.
/// </summary>
/// <value>
/// The tags.
/// </value>
public string[] Tags { get; set; }
/// <summary>
/// Gets or sets the old tag.
/// </summary>
/// <value>
/// The old tag.
/// </value>
public string OldTag { get; set; }
/// <summary>
/// Gets or sets the new tag.
/// </summary>
/// <value>
/// The new tag.
/// </value>
public string NewTag { get; set; }
/// <summary>
/// Converts this instance to a parameter list.
/// </summary>
/// <returns></returns>
public object Convert()
{
Dictionary<string, object> parameters = new Dictionary<string, object>
{
{ "item_id", ID.ToString() },
{ "item_id", ID },
{ "action", Action }
};
if(Time != null)
parameters.Add( "time", (int)((DateTime)Time - new DateTime(1970, 1, 1)).TotalSeconds );
if (Time != null)
parameters.Add("time", Utilities.GetUnixTimestamp(Time));
if (Tags != null)
parameters.Add("tags", String.Join(",", Tags));
if (OldTag != null)
parameters.Add("old_tag", OldTag);
if (NewTag != null)
parameters.Add("new_tag", NewTag);
return parameters;
}
@@ -0,0 +1,59 @@
using RestSharp;
using System;
using System.Collections.Generic;
namespace PocketSharp.Models
{
/// <summary>
/// All parameters which can be passed to add a new item
/// </summary>
public class AddParameters
{
/// <summary>
/// Gets or sets the URI.
/// </summary>
/// <value>
/// The URI.
/// </value>
public Uri Uri { get; set; }
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>
/// The title.
/// </value>
public string Title { get; set; }
/// <summary>
/// Gets or sets the tags.
/// </summary>
/// <value>
/// The tags.
/// </value>
public string[] Tags { get; set; }
/// <summary>
/// Gets or sets the tweet ID.
/// </summary>
/// <value>
/// The tweet ID.
/// </value>
public string TweetID { get; set; }
/// <summary>
/// Converts this instance to a parameter list.
/// </summary>
/// <returns></returns>
public List<Parameter> Convert()
{
return new List<Parameter>()
{
Utilities.CreateParam("url", Uri.ToString() ),
Utilities.CreateParam("title", Title),
Utilities.CreateParam("tags", String.Join(",", Tags)),
Utilities.CreateParam("tweet_id", TweetID)
};
}
}
}
@@ -1,28 +1,37 @@
using RestSharp;
using ServiceStack.Text;
using System;
using System.Collections.Generic;
namespace PocketSharp.Models
{
public class ModifyParameters : ParameterBase
/// <summary>
/// All parameters which can be passed to modify an item
/// </summary>
public class ModifyParameters
{
/// <summary>
/// Gets or sets the actions.
/// </summary>
/// <value>
/// The actions.
/// </value>
public List<ActionParameter> Actions { get; set; }
/// <summary>
/// Converts this instance to a parameter list.
/// </summary>
/// <returns></returns>
public List<Parameter> Convert()
{
List<Parameter> parameters = new List<Parameter>();
List<object> actions = new List<object>();
Actions.ForEach(delegate(ActionParameter action)
Actions.ForEach(action => actions.Add(action.Convert()));
return new List<Parameter>()
{
actions.Add(action.Convert());
});
parameters.Add(CreateParam("actions", JsonSerializer.SerializeToString(actions)));
return parameters;
Utilities.CreateParam("actions", JsonSerializer.SerializeToString(actions))
};
}
}
}
@@ -1,21 +0,0 @@
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PocketSharp.Models
{
public abstract class ParameterBase
{
protected static Parameter CreateParam(string name, object value, ParameterType type = ParameterType.GetOrPost)
{
return new Parameter()
{
Name = name,
Value = value,
Type = type
};
}
}
}
@@ -1,93 +1,200 @@
using RestSharp;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
namespace PocketSharp.Models
{
public class RetrieveParameters : ParameterBase
/// <summary>
/// All parameters which can be passed for item retrieval
/// </summary>
public class RetrieveParameters
{
/// <summary>
/// Gets or sets the state.
/// </summary>
/// <value>
/// The state.
/// </value>
public StateEnum? State { get; set; }
/// <summary>
/// Gets or sets the favorite.
/// </summary>
/// <value>
/// The favorite.
/// </value>
public bool? Favorite { get; set; }
/// <summary>
/// Gets or sets the tag.
/// </summary>
/// <value>
/// The tag.
/// </value>
public string Tag { get; set; }
/// <summary>
/// Gets or sets the type of the content.
/// </summary>
/// <value>
/// The type of the content.
/// </value>
public ContentTypeEnum? ContentType { get; set; }
/// <summary>
/// Gets or sets the sort.
/// </summary>
/// <value>
/// The sort.
/// </value>
public SortEnum? Sort { get; set; }
/// <summary>
/// Gets or sets the type of the detail.
/// </summary>
/// <value>
/// The type of the detail.
/// </value>
public DetailTypeEnum? DetailType { get; set; }
/// <summary>
/// Gets or sets the search.
/// </summary>
/// <value>
/// The search.
/// </value>
public string Search { get; set; }
/// <summary>
/// Gets or sets the domain.
/// </summary>
/// <value>
/// The domain.
/// </value>
public string Domain { get; set; }
public DateTime Since { get; set; }
/// <summary>
/// Gets or sets the since.
/// </summary>
/// <value>
/// The since.
/// </value>
public DateTime? Since { get; set; }
/// <summary>
/// Gets or sets the count.
/// </summary>
/// <value>
/// The count.
/// </value>
public int? Count { get; set; }
/// <summary>
/// Gets or sets the offset.
/// </summary>
/// <value>
/// The offset.
/// </value>
public int? Offset { get; set; }
/// <summary>
/// Converts this instance to a parameter list.
/// </summary>
/// <returns></returns>
public List<Parameter> Convert()
{
List<Parameter> parameters = new List<Parameter>();
if (State != null)
parameters.Add(CreateParam("state", State.ToString()));
if (Favorite != null)
parameters.Add(CreateParam("favorite", (bool)Favorite ? "1" : "0"));
if (Tag != null)
parameters.Add(CreateParam("tag", Tag));
if (ContentType != null)
parameters.Add(CreateParam("contentType", ContentType.ToString()));
if (Sort != null)
parameters.Add(CreateParam("sort", Sort.ToString()));
if (DetailType != null)
parameters.Add(CreateParam("detailType", DetailType.ToString()));
if (Search != null)
parameters.Add(CreateParam("search", Search));
if (Domain != null)
parameters.Add(CreateParam("domain", Domain));
if (Since != null)
parameters.Add(CreateParam("since", (int)(Since - new DateTime(1970, 1, 1)).TotalSeconds));
if (Count != null)
parameters.Add(CreateParam("count", Count));
if (Offset != null)
parameters.Add(CreateParam("offset", Offset));
return parameters;
return new List<Parameter>()
{
Utilities.CreateParam("state", State != null ? State.ToString() : null ),
Utilities.CreateParam("favorite", Favorite != null ? (bool)Favorite ? "1" : "0" : null),
Utilities.CreateParam("tag", Tag),
Utilities.CreateParam("contentType", ContentType != null ? ContentType.ToString() : null),
Utilities.CreateParam("sort", Sort != null ? Sort.ToString() : null),
Utilities.CreateParam("detailType", DetailType != null ? DetailType.ToString() : null),
Utilities.CreateParam("search", Search),
Utilities.CreateParam("domain", Domain),
Utilities.CreateParam("since", Utilities.GetUnixTimestamp(Since)),
Utilities.CreateParam("count", Count),
Utilities.CreateParam("offset", Offset)
};
}
}
/// <summary>
/// Item states
/// </summary>
public enum StateEnum
{
/// <summary>
/// Only unread items
/// </summary>
unread,
/// <summary>
/// Only archived items
/// </summary>
archive,
/// <summary>
/// All items
/// </summary>
all
}
/// <summary>
/// Sorting
/// </summary>
public enum SortEnum
{
/// <summary>
/// Newest first
/// </summary>
newest,
/// <summary>
/// Oldest first
/// </summary>
oldest,
/// <summary>
/// Title A-Z descending
/// </summary>
title,
/// <summary>
/// URL descending
/// </summary>
site
}
/// <summary>
/// Item types
/// </summary>
public enum ContentTypeEnum
{
/// <summary>
/// Articles
/// </summary>
article,
/// <summary>
/// Videos
/// </summary>
video,
/// <summary>
/// Images
/// </summary>
image
}
/// <summary>
/// Item data
/// </summary>
public enum DetailTypeEnum
{
/// <summary>
/// Necessary data
/// </summary>
simple,
/// <summary>
/// Includes all Images/videos/tags/authors
/// </summary>
complete
}
}
+21
View File
@@ -3,15 +3,36 @@ using System.Runtime.Serialization;
namespace PocketSharp.Models
{
/// <summary>
/// Author
/// </summary>
[DataContract]
public class PocketAuthor
{
/// <summary>
/// Gets or sets the ID.
/// </summary>
/// <value>
/// The ID.
/// </value>
[DataMember(Name = "author_id")]
public string ID { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
[DataMember]
public string Name { get; set; }
/// <summary>
/// Gets or sets the URI.
/// </summary>
/// <value>
/// The URI.
/// </value>
[DataMember(Name = "url")]
public Uri Uri { get; set; }
}
+27
View File
@@ -3,18 +3,45 @@ using System.Runtime.Serialization;
namespace PocketSharp.Models
{
/// <summary>
/// Image
/// </summary>
[DataContract]
public class PocketImage
{
/// <summary>
/// Gets or sets the ID.
/// </summary>
/// <value>
/// The ID.
/// </value>
[DataMember(Name = "image_id")]
public string ID { get; set; }
/// <summary>
/// Gets or sets the caption.
/// </summary>
/// <value>
/// The caption.
/// </value>
[DataMember]
public string Caption { get; set; }
/// <summary>
/// Gets or sets the credit.
/// </summary>
/// <value>
/// The credit.
/// </value>
[DataMember]
public string Credit { get; set; }
/// <summary>
/// Gets or sets the URI.
/// </summary>
/// <value>
/// The URI.
/// </value>
[DataMember(Name = "src")]
public Uri Uri { get; set; }
}
+170 -4
View File
@@ -4,106 +4,272 @@ using System.Runtime.Serialization;
namespace PocketSharp.Models
{
/// <summary>
/// Item containing all available data
/// see: http://getpocket.com/developer/docs/v3/retrieve
/// </summary>
[DataContract]
public class PocketItem
{
/// <summary>
/// Gets or sets the ID.
/// </summary>
/// <value>
/// The ID.
/// </value>
[DataMember(Name = "item_id")]
public int ID { get; set; }
/// <summary>
/// Gets or sets the URI.
/// </summary>
/// <value>
/// The URI.
/// </value>
[DataMember(Name = "resolved_url")]
public Uri Uri { get; set; }
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>
/// The title.
/// </value>
[DataMember(Name = "resolved_title")]
public string Title { get; set; }
/// <summary>
/// Gets or sets the full title.
/// </summary>
/// <value>
/// The full title.
/// </value>
[DataMember(Name = "given_title")]
public string FullTitle { get; set; }
/// <summary>
/// Gets or sets the excerpt.
/// </summary>
/// <value>
/// The excerpt.
/// </value>
[DataMember]
public string Excerpt { get; set; }
/// <summary>
/// Gets or sets the status.
/// </summary>
/// <value>
/// The status.
/// </value>
[DataMember]
public int Status { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is favorite.
/// </summary>
/// <value>
/// <c>true</c> if this instance is favorite; otherwise, <c>false</c>.
/// </value>
[DataMember(Name = "favorite")]
public bool IsFavorite { get; set; }
/// <summary>
/// Gets a value indicating whether this instance is archive.
/// </summary>
/// <value>
/// <c>true</c> if this instance is archive; otherwise, <c>false</c>.
/// </value>
[IgnoreDataMember]
public bool IsArchive { get { return Status == 1; } }
/// <summary>
/// Gets a value indicating whether this instance is deleted.
/// </summary>
/// <value>
/// <c>true</c> if this instance is deleted; otherwise, <c>false</c>.
/// </value>
[IgnoreDataMember]
public bool IsDeleted { get { return Status == 2; } }
/// <summary>
/// Gets or sets a value indicating whether this instance is article.
/// </summary>
/// <value>
/// <c>true</c> if this instance is article; otherwise, <c>false</c>.
/// </value>
[DataMember(Name = "is_article")]
public bool IsArticle { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance has image.
/// </summary>
/// <value>
/// <c>true</c> if this instance has image; otherwise, <c>false</c>.
/// </value>
[DataMember(Name = "has_image")]
public bool HasImage { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance has video.
/// </summary>
/// <value>
/// <c>true</c> if this instance has video; otherwise, <c>false</c>.
/// </value>
[DataMember(Name = "has_video")]
public bool HasVideo { get; set; }
/// <summary>
/// Gets or sets the word count.
/// </summary>
/// <value>
/// The word count.
/// </value>
[DataMember(Name = "word_count")]
public int WordCount { get; set; }
/// <summary>
/// Gets or sets the sort.
/// </summary>
/// <value>
/// The sort.
/// </value>
[DataMember(Name = "sort_id")]
public int Sort { get; set; }
/// <summary>
/// Gets or sets the add time.
/// </summary>
/// <value>
/// The add time.
/// </value>
[DataMember(Name = "time_added")]
public DateTime? AddTime { get; set; }
/// <summary>
/// Gets or sets the update time.
/// </summary>
/// <value>
/// The update time.
/// </value>
[DataMember(Name = "time_updated")]
public DateTime? UpdateTime { get; set; }
/// <summary>
/// Gets or sets the read time.
/// </summary>
/// <value>
/// The read time.
/// </value>
[DataMember(Name = "time_read")]
public DateTime? ReadTime { get; set; }
/// <summary>
/// Gets or sets the favorite time.
/// </summary>
/// <value>
/// The favorite time.
/// </value>
[DataMember(Name = "time_favorited")]
public DateTime? FavoriteTime { get; set; }
/// <summary>
/// Gets or sets the _ tag dictionary.
/// </summary>
/// <value>
/// The _ tag dictionary.
/// </value>
[DataMember(Name = "tags")]
public Dictionary<string, PocketTag> _TagDictionary { get; set; }
/// <summary>
/// Gets or sets the _ image dictionary.
/// </summary>
/// <value>
/// The _ image dictionary.
/// </value>
[DataMember(Name = "images")]
public Dictionary<string, PocketImage> _ImageDictionary { get; set; }
/// <summary>
/// Gets or sets the _ video dictionary.
/// </summary>
/// <value>
/// The _ video dictionary.
/// </value>
[DataMember(Name = "videos")]
public Dictionary<string, PocketVideo> _VideoDictionary { get; set; }
/// <summary>
/// Gets or sets the _ author dictionary.
/// </summary>
/// <value>
/// The _ author dictionary.
/// </value>
[DataMember(Name = "authors")]
public Dictionary<string, PocketAuthor> _AuthorDictionary { get; set; }
/// <summary>
/// Gets the tags.
/// </summary>
/// <value>
/// The tags.
/// </value>
[IgnoreDataMember]
public List<PocketTag> Tags
{
get { return PocketClient.DictionaryToList<PocketTag>(_TagDictionary); }
get { return Utilities.DictionaryToList<PocketTag>(_TagDictionary); }
}
/// <summary>
/// Gets the images.
/// </summary>
/// <value>
/// The images.
/// </value>
[IgnoreDataMember]
public List<PocketImage> Images
{
get { return PocketClient.DictionaryToList<PocketImage>(_ImageDictionary); }
get { return Utilities.DictionaryToList<PocketImage>(_ImageDictionary); }
}
/// <summary>
/// Gets the lead image.
/// </summary>
/// <value>
/// The lead image.
/// </value>
[IgnoreDataMember]
public PocketImage LeadImage
{
get { return Images != null ? Images[0] : null; }
}
/// <summary>
/// Gets the videos.
/// </summary>
/// <value>
/// The videos.
/// </value>
[IgnoreDataMember]
public List<PocketVideo> Videos
{
get { return PocketClient.DictionaryToList<PocketVideo>(_VideoDictionary); }
get { return Utilities.DictionaryToList<PocketVideo>(_VideoDictionary); }
}
/// <summary>
/// Gets the authors.
/// </summary>
/// <value>
/// The authors.
/// </value>
[IgnoreDataMember]
public List<PocketAuthor> Authors
{
get { return PocketClient.DictionaryToList<PocketAuthor>(_AuthorDictionary); }
get { return Utilities.DictionaryToList<PocketAuthor>(_AuthorDictionary); }
}
}
}
+9
View File
@@ -2,9 +2,18 @@
namespace PocketSharp.Models
{
/// <summary>
/// Tag
/// </summary>
[DataContract]
public class PocketTag
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
[DataMember(Name = "tag")]
public string Name { get; set; }
}
+21
View File
@@ -3,15 +3,36 @@ using System.Runtime.Serialization;
namespace PocketSharp.Models
{
/// <summary>
/// Video
/// </summary>
[DataContract]
public class PocketVideo
{
/// <summary>
/// Gets or sets the ID.
/// </summary>
/// <value>
/// The ID.
/// </value>
[DataMember(Name = "video_id")]
public string ID { get; set; }
/// <summary>
/// Gets or sets the external ID.
/// </summary>
/// <value>
/// The external ID.
/// </value>
[DataMember(Name = "vid")]
public string ExternalID { get; set; }
/// <summary>
/// Gets or sets the URI.
/// </summary>
/// <value>
/// The URI.
/// </value>
[DataMember(Name = "src")]
public Uri Uri { get; set; }
}
+20
View File
@@ -0,0 +1,20 @@
using System.Runtime.Serialization;
namespace PocketSharp.Models
{
/// <summary>
/// Add Response
/// </summary>
[DataContract]
class Add : ResponseBase
{
/// <summary>
/// Gets or sets the item.
/// </summary>
/// <value>
/// The item.
/// </value>
[DataMember]
public PocketItem Item { get; set; }
}
}
+10 -6
View File
@@ -1,16 +1,20 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization;
namespace PocketSharp.Models
{
/// <summary>
/// Modify Response
/// </summary>
[DataContract]
class Modify : ResponseBase
{
/// <summary>
/// Gets or sets the action results.
/// </summary>
/// <value>
/// The action results.
/// </value>
[DataMember(Name = "action_results")]
public bool[] ActionResults { get; set; }
[DataMember(Name = "status")]
public int Status { get; set; }
}
}
+12 -4
View File
@@ -1,12 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace PocketSharp.Models
{
/// <summary>
/// Base for Responses
/// </summary>
[DataContract]
class ResponseBase
{
/// <summary>
/// Gets or sets a value indicating whether this <see cref="ResponseBase"/> is status.
/// </summary>
/// <value>
/// <c>true</c> if status is OK; otherwise, <c>false</c>.
/// </value>
[DataMember(Name = "status")]
public bool Status { get; set; }
}
}
+29 -3
View File
@@ -1,25 +1,51 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace PocketSharp.Models
{
/// <summary>
/// Item Response
/// </summary>
[DataContract]
class Retrieve : ResponseBase
{
/// <summary>
/// Gets or sets the complete.
/// </summary>
/// <value>
/// The complete.
/// </value>
[DataMember(Name = "complete")]
public int Complete { get; set; }
/// <summary>
/// Gets or sets the since.
/// </summary>
/// <value>
/// The since.
/// </value>
[DataMember]
public int Since { get; set; }
/// <summary>
/// Gets or sets the _ item dictionary.
/// </summary>
/// <value>
/// The _ item dictionary.
/// </value>
[DataMember(Name = "list")]
public Dictionary<string, PocketItem> _ItemDictionary { get; set; }
/// <summary>
/// Gets the items.
/// </summary>
/// <value>
/// The items.
/// </value>
[IgnoreDataMember]
public List<PocketItem> Items
{
get { return PocketClient.DictionaryToList<PocketItem>(_ItemDictionary); }
get { return Utilities.DictionaryToList<PocketItem>(_ItemDictionary); }
}
}
}
+46 -83
View File
@@ -3,7 +3,6 @@ using RestSharp;
using System;
using System.Collections.Generic;
using System.Net;
using ServiceStack.Text;
namespace PocketSharp
{
@@ -63,17 +62,19 @@ namespace PocketSharp
/// <summary>
/// Initializes a new instance of the <see cref="PocketClient"/> class.
/// Initializes a new instance of the <see cref="PocketClient" /> class.
/// </summary>
/// <param name="consumerKey">The API key.</param>
/// <param name="accessCode">The access code.</param>
public PocketClient(string consumerKey, string accessCode)
: this(consumerKey, accessCode, defaultBaseUrl) { }
/// <summary>
/// Initializes a new instance of the <see cref="PocketClient"/> class.
/// Initializes a new instance of the <see cref="PocketClient" /> class.
/// </summary>
/// <param name="consumerKey">The API key.</param>
/// <param name="baseUrl">The base URL.</param>
public PocketClient(string consumerKey, Uri baseUrl)
: this(consumerKey, "", baseUrl) { }
@@ -83,6 +84,7 @@ namespace PocketSharp
/// </summary>
/// <param name="consumerKey">The API key.</param>
/// <param name="accessCode">Provide an access code if the user is already authenticated</param>
/// <param name="baseUrl">The base URL.</param>
public PocketClient(string consumerKey, string accessCode, Uri baseUrl)
{
// assign public properties
@@ -156,10 +158,16 @@ namespace PocketSharp
/// <typeparam name="T"></typeparam>
/// <param name="method">Requested method (path after /v3/)</param>
/// <param name="parameters">Additional POST parameters</param>
/// <param name="requireAuth">if set to <c>true</c> [require auth].</param>
/// <returns></returns>
/// <exception cref="APIException">No access token available. Use authentification first.</exception>
protected T Get<T>(string method, List<Parameter> parameters = null) where T : class, new()
protected T Get<T>(string method, List<Parameter> parameters = null, bool requireAuth = false) where T : class, new()
{
if (requireAuth && AccessCode == null)
{
throw new APIException("No access token available. Use authentification first.");
}
// every single Pocket API endpoint requires HTTP POST data
var request = new RestRequest(method, Method.POST);
@@ -183,54 +191,19 @@ namespace PocketSharp
}
/// <summary>
/// Puts/Updates a typed resource
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method">Requested method (path after /v3/)</param>
/// <param name="actions">Additional action parameters</param>
/// <returns></returns>
protected T Put<T>(string method, List<ActionParameter> actions) where T : class, new()
{
// put requests only with authentification
ExpectAuthentification();
ModifyParameters parameters = new ModifyParameters()
{
Actions = actions
};
return Get<T>(method, parameters.Convert());
}
/// <summary>
/// Puts/Updates a typed resource
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method">Requested method (path after /v3/)</param>
/// <param name="action">action parameter</param>
/// <returns></returns>
protected T Put<T>(string method, ActionParameter action) where T : class, new()
{
return Put<T>(method, new List<ActionParameter>() { action });
}
/// <summary>
/// Puts an action
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <param name="action">The action.</param>
/// <param name="actionParameter">The action parameter.</param>
/// <returns></returns>
protected bool PutAction(int itemID, string action)
protected bool PutSendAction(ActionParameter actionParameter)
{
ActionParameter actionParam = new ActionParameter()
ModifyParameters parameters = new ModifyParameters()
{
Action = action,
ID = itemID
Actions = new List<ActionParameter>() { actionParameter }
};
return Put<Modify>("send", actionParam).Status == 1;
return Get<Modify>("send", parameters.Convert(), true).Status;
}
@@ -246,50 +219,40 @@ namespace PocketSharp
{
if (response.StatusCode != HttpStatusCode.OK)
{
throw new APIException(response.Content, response.ErrorException);
// get pocket error headers
Parameter error = response.Headers[1];
Parameter errorCode = response.Headers[2];
string exceptionString = response.Content;
bool isPocketError = error.Name == "X-Error";
// update message to include pocket response data
if (isPocketError)
{
exceptionString = exceptionString + "\nPocketResponse: (" + errorCode.Value + ") " + error.Value;
}
// create exception
APIException exception = new APIException(exceptionString, response.ErrorException);
if (isPocketError)
{
// add custom pocket fields
exception.PocketError = error.Value.ToString();
exception.PocketErrorCode = Convert.ToInt32(errorCode.Value);
// add to generic exception data
exception.Data.Add(error.Name, error.Value);
exception.Data.Add(errorCode.Name, errorCode.Value);
}
throw exception;
}
else if (response.ErrorException != null)
{
throw new APIException("Error retrieving response", response.ErrorException);
}
}
/// <summary>
/// Throws exception if access code is not available
/// </summary>
/// <exception cref="APIException">No access token available. Use authentification first.</exception>
protected void ExpectAuthentification()
{
if (AccessCode == null)
{
throw new APIException("No access token available. Use authentification first.");
}
}
/// <summary>
/// Convert a dictionary to a list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items">The items.</param>
/// <returns></returns>
internal static List<T> DictionaryToList<T>(Dictionary<string, T> items) where T : new()
{
if(items == null)
{
return null;
}
var itemEnumerator = items.GetEnumerator();
List<T> list = new List<T>();
while (itemEnumerator.MoveNext())
{
list.Add(itemEnumerator.Current.Value);
}
return list;
}
}
}
+4 -1
View File
@@ -50,6 +50,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="APIException.cs" />
<Compile Include="Components\Add.cs" />
<Compile Include="Components\Authentification.cs" />
<Compile Include="Components\Modify.cs" />
<Compile Include="Components\ModifyTags.cs" />
@@ -57,13 +58,14 @@
<Compile Include="JsonDeserializer.cs" />
<Compile Include="Models\Authentification\AccessCode.cs" />
<Compile Include="Models\Authentification\RequestCode.cs" />
<Compile Include="Models\Parameters\AddParameters.cs" />
<Compile Include="Models\PocketAuthor.cs" />
<Compile Include="Models\PocketImage.cs" />
<Compile Include="Models\PocketVideo.cs" />
<Compile Include="Models\Response\Add.cs" />
<Compile Include="Models\Response\Modify.cs" />
<Compile Include="Models\Parameters\ActionParameter.cs" />
<Compile Include="Models\Parameters\ModifyParameters.cs" />
<Compile Include="Models\Parameters\ParameterBase.cs" />
<Compile Include="Models\Parameters\RetrieveParameters.cs" />
<Compile Include="Models\PocketItem.cs" />
<Compile Include="Models\PocketTag.cs" />
@@ -71,6 +73,7 @@
<Compile Include="Models\Response\Retrieve.cs" />
<Compile Include="PocketClient.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utilities.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
+70
View File
@@ -0,0 +1,70 @@
using RestSharp;
using System;
using System.Collections.Generic;
namespace PocketSharp
{
/// <summary>
/// General utilities
/// </summary>
internal class Utilities
{
/// <summary>
/// converts DateTime to an UNIX timestamp
/// </summary>
/// <param name="dateTime">The date.</param>
/// <returns>UNIX timestamp</returns>
public static int? GetUnixTimestamp(DateTime? dateTime)
{
if (dateTime == null)
{
return null;
}
return (int)((DateTime)dateTime - new DateTime(1970, 1, 1)).TotalSeconds;
}
/// <summary>
/// Creates a Parameter object.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
/// <param name="type">The type.</param>
/// <returns></returns>
public static Parameter CreateParam(string name, object value, ParameterType type = ParameterType.GetOrPost)
{
return new Parameter()
{
Name = name,
Value = value,
Type = type
};
}
/// <summary>
/// Convert a dictionary to a list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items">The items.</param>
/// <returns></returns>
public static List<T> DictionaryToList<T>(Dictionary<string, T> items) where T : new()
{
if (items == null)
{
return null;
}
var itemEnumerator = items.GetEnumerator();
List<T> list = new List<T>();
while (itemEnumerator.MoveNext())
{
list.Add(itemEnumerator.Current.Value);
}
return list;
}
}
}
+128 -38
View File
@@ -19,18 +19,24 @@ Request a Consumer Key on Pocket: [My Applications on Pocket](http://getpocket.c
Include the PocketSharp namespace and it's associated models (you will need them later):
using PocketSharp;
using PocketSharp.Models;
```csharp
using PocketSharp;
using PocketSharp.Models;
```
Initialize PocketClient with:
PocketClient _client = new PocketClient("[YOUR_CONSUMER_KEY]", "[YOUR_ACCESS_CODE]");
```csharp
PocketClient _client = new PocketClient("[YOUR_CONSUMER_KEY]", "[YOUR_ACCESS_CODE]");
```
Do a simple request - e.g. a search for `CSS`:
_client.Search("css").ForEach(
item => Console.WriteLine(item.ID + " | " + item.Title)
);
```csharp
_client.Search("css").ForEach(
item => Console.WriteLine(item.ID + " | " + item.Title)
);
```
Which will output:
@@ -43,22 +49,26 @@ Which will output:
## Create an instance
There are 4 overloads for `PocketClient`:
// used for authentication, when no accessCode is available yet
PocketClient(string consumerKey)
// start the PocketClient with an accessCode
PocketClient(string consumerKey, string accessCode)
// different base URL
PocketClient(string consumerKey, Uri baseUrl)
// accessCode and different base URL
PocketClient(string consumerKey, string accessCode, Uri baseUrl)
```csharp
// used for authentication, when no accessCode is available yet
PocketClient(string consumerKey)
// start the PocketClient with an accessCode
PocketClient(string consumerKey, string accessCode)
// different base URL
PocketClient(string consumerKey, Uri baseUrl)
// accessCode and different base URL
PocketClient(string consumerKey, string accessCode, Uri baseUrl)
```
You can change the _Access Code_ after initialization:
_client.AccessCode = "[YOU_ACCESS_CODE]";
```csharp
_client.AccessCode = "[YOU_ACCESS_CODE]";
```
## Authentication
@@ -68,7 +78,9 @@ This is a 3-step process:
1) Receive the authentication URI from the library by calling `Uri Authenticate(Uri callbackUri)`:
Uri authenticationUri = client.Authenticate(new Uri("http://example.com"));
```csharp
Uri authenticationUri = client.Authenticate(new Uri("http://example.com"));
```
The `callbackUri` is the URI which is triggered after the user successfully (or not) authenticated your application.
@@ -78,7 +90,9 @@ In this step not only the `authenticationUri` is generated, but the PocketSharp
3) Call `string GetAccessCode()`
string accessCode = GetAccessCode();
```csharp
string accessCode = GetAccessCode();
```
Again, the received _Access Code_ is stored internally.
Note that `GetAccessCode` can only be called with an existing _Request Code_. If you need to re-authenticate a user, start again with Step 1).
@@ -87,11 +101,15 @@ Note that `GetAccessCode` can only be called with an existing _Request Code_. If
If you need to re-instantiate PocketClient after the redirect, please provide the _Request Code_:
_client.RequestCode = "[YOU_REQUEST_CODE]";
```csharp
_client.RequestCode = "[YOU_REQUEST_CODE]";
```
After Step 1) you have public access to the RequestCode property and can store it for later usage.
string requestCode = _client.RequestCode;
```csharp
string requestCode = _client.RequestCode;
```
#### Important
@@ -103,37 +121,109 @@ Without it you would always have to redo the authentication process.
Find items by a tag:
List<PocketItem> items = _client.SearchByTag("tutorial");
```csharp
List<PocketItem> items = _client.SearchByTag("tutorial");
```
Find items by a search string:
List<PocketItem> items = _client.Search("css");
```csharp
List<PocketItem> items = _client.Search("css");
```
Find items by a filter:
List<PocketItem> items = _client.Retrieve(RetrieveFilter.Favorite); // only favorites
```csharp
List<PocketItem> items = _client.Retrieve(RetrieveFilter.Favorite); // only favorites
```
The RetrieveFilter Enum is specified as follows:
enum RetrieveFilter { All, Unread, Archive, Favorite, Article, Video, Image }
```csharp
enum RetrieveFilter { All, Unread, Archive, Favorite, Article, Video, Image }
```
### Custom Parameters
#### Custom Parameters
You can create a completely custom parameter list for retrieval with the POCO `RetrieveParameters`:
var parameters = new RetrieveParameters()
{
Count = 50,
Offset = 100,
Sort = SortEnum.oldest
...
};
```csharp
var parameters = new RetrieveParameters()
{
Count = 50,
Offset = 100,
Sort = SortEnum.oldest
...
};
List<PocketItem> items = _client.Retrieve(parameters);
List<PocketItem> items = _client.Retrieve(parameters);
```
## Add & Modify
## Add
This is work in progress - don't use these methods!
Adds a new item to your pocket list.
Accepts four parameters, with `uri` being required.
```csharp
PocketItem Add(Uri uri, string[] tags = null, string title = null, string tweetID = null)
```
Example:
```csharp
PocketItem newItem = _client.Add(
new Uri("http://www.neowin.net/news/full-build-2013-conference-sessions-listing-revealed"),
new string[] { "microsoft", "neowin", "build" }
);
```
The title 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 (with the tweetID). This allows Pocket to show the original tweet alongside the article.
## Modify
All Modify methods accept either the itemID (as int) or a `PocketItem` as parameter.
Archive the specified item:
bool isSuccess = _client.Archive(myPocketItem);
Un-archive the specified item:
bool isSuccess = _client.Unarchive(myPocketItem);
Favorites the specified item:
bool isSuccess = _client.Favorite(myPocketItem);
Un-favorites the specified item:
bool isSuccess = _client.Unfavorite(myPocketItem);
Deletes the specified item:
bool isSuccess = _client.Delete(myPocketItem);
Add tags to the specified item:
bool isSuccess = _client.AddTags(myPocketItem, new string[] { "css", "2013" });
Remove tags from the specified item:
bool isSuccess = _client.RemoveTags(myPocketItem, new string[] { "css", "2013" });
Remove all tags from the specified item:
bool isSuccess = _client.RemoveTags(myPocketItem);
Replaces all existing tags with new ones for the specified item:
bool isSuccess = _client.ReplaceTags(myPocketItem, new string[] { "css", "2013" });
Renames a tag for the specified item:
bool isSuccess = _client.RenameTag(myPocketItem, "oldTagName", "newTagName");
---