From 3470c7bb8068c05075896989fa7f20a033bd4c8e Mon Sep 17 00:00:00 2001 From: ceee Date: Mon, 24 Jun 2013 19:55:16 +0200 Subject: [PATCH] add first version of Modify; --- PocketSharp.Console/Program.cs | 22 +++++++++---- PocketSharp/Components/Authentification.cs | 4 +-- PocketSharp/Components/Modify.cs | 18 +++++++++++ PocketSharp/Components/Retrieve.cs | 8 ++--- PocketSharp/Models/Modify.cs | 16 ++++++++++ .../Models/Parameters/ActionParameter.cs | 32 +++++++++++++++++++ .../Models/Parameters/ModifyParameters.cs | 28 ++++++++++++++++ .../Models/Parameters/ParameterBase.cs | 2 +- PocketSharp/Models/PocketTag.cs | 11 +++++++ PocketSharp/PocketClient.cs | 24 ++++++++++++-- PocketSharp/PocketSharp.csproj | 5 +++ 11 files changed, 154 insertions(+), 16 deletions(-) create mode 100644 PocketSharp/Components/Modify.cs create mode 100644 PocketSharp/Models/Modify.cs create mode 100644 PocketSharp/Models/Parameters/ActionParameter.cs create mode 100644 PocketSharp/Models/Parameters/ModifyParameters.cs create mode 100644 PocketSharp/Models/PocketTag.cs diff --git a/PocketSharp.Console/Program.cs b/PocketSharp.Console/Program.cs index d4350ac..f6a7216 100644 --- a/PocketSharp.Console/Program.cs +++ b/PocketSharp.Console/Program.cs @@ -19,18 +19,28 @@ namespace PocketSharp.Console consumerKey: "15396-f6f92101d72c8e270a6c9bb3" ); - Uri redirect = client.Authenticate(new Uri("http://example.com")); + //Uri redirect = client.Authenticate(new Uri("http://example.com")); - System.Console.WriteLine(redirect.ToString()); + //System.Console.WriteLine(redirect.ToString()); - System.Console.WriteLine("---------------------------------"); - System.Console.WriteLine("Press Any key after you've authenticated the user via the given URI"); + //System.Console.WriteLine("---------------------------------"); + //System.Console.WriteLine("Press Any key after you've authenticated the user via the given URI"); - System.Console.ReadKey(); + //System.Console.ReadKey(); + //System.Console.WriteLine("---------------------------------"); + + //System.Console.WriteLine(client.GetAccessCode()); + + client.Search("css").ForEach(delegate(PocketItem item) + { + System.Console.WriteLine(item.ID + " ::: " + item.FullTitle); + }); + System.Console.WriteLine("---------------------------------"); - System.Console.WriteLine(client.GetAccessCode()); + client.Archive(330361896); + System.Console.ReadKey(); } diff --git a/PocketSharp/Components/Authentification.cs b/PocketSharp/Components/Authentification.cs index a7ecc23..e2b1eca 100644 --- a/PocketSharp/Components/Authentification.cs +++ b/PocketSharp/Components/Authentification.cs @@ -16,7 +16,7 @@ namespace PocketSharp /// public Uri Authenticate(Uri callbackUri) { - RequestCode response = GetResource("oauth/request", new List() + RequestCode response = Get("oauth/request", new List() { new Parameter() { Name = "redirect_uri", Value = callbackUri, Type = ParameterType.GetOrPost } }); @@ -41,7 +41,7 @@ namespace PocketSharp throw new APIException("Authenticate the user first to receive a request_code"); } - AccessCode response = GetResource("oauth/authorize", new List() + AccessCode response = Get("oauth/authorize", new List() { new Parameter() { Name = "code", Value = RequestCode, Type = ParameterType.GetOrPost } }); diff --git a/PocketSharp/Components/Modify.cs b/PocketSharp/Components/Modify.cs new file mode 100644 index 0000000..c7ba7da --- /dev/null +++ b/PocketSharp/Components/Modify.cs @@ -0,0 +1,18 @@ +using PocketSharp.Models; +using System.Collections.Generic; + +namespace PocketSharp +{ + public partial class PocketClient + { + public bool Archive(int itemID) + { + List actions = new List() + { + new ActionParameter() { Action = "archive", ID = itemID } + }; + + return Put("send", actions).Status == 1; + } + } +} diff --git a/PocketSharp/Components/Retrieve.cs b/PocketSharp/Components/Retrieve.cs index e16dcb8..4287edb 100644 --- a/PocketSharp/Components/Retrieve.cs +++ b/PocketSharp/Components/Retrieve.cs @@ -13,7 +13,7 @@ namespace PocketSharp public List Retrieve(RetrieveParameters parameters) { ExpectAuthentification(); - return GetResource("get", parameters.Convert()).Items; + return Get("get", parameters.Convert()).Items; } @@ -50,7 +50,7 @@ namespace PocketSharp break; } - return GetResource("get", parameters.Convert()).Items; + return Get("get", parameters.Convert()).Items; } @@ -66,7 +66,7 @@ namespace PocketSharp { Tag = tag }; - return GetResource("get", parameters.Convert()).Items; + return Get("get", parameters.Convert()).Items; } @@ -82,7 +82,7 @@ namespace PocketSharp { Search = searchString }; - return GetResource("get", parameters.Convert()).Items; + return Get("get", parameters.Convert()).Items; } } diff --git a/PocketSharp/Models/Modify.cs b/PocketSharp/Models/Modify.cs new file mode 100644 index 0000000..1361472 --- /dev/null +++ b/PocketSharp/Models/Modify.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Runtime.Serialization; + +namespace PocketSharp.Models +{ + [DataContract] + class Modify : ResponseBase + { + [DataMember(Name = "action_results")] + public bool[] ActionResults { get; set; } + + [DataMember(Name = "status")] + public int Status { get; set; } + } +} diff --git a/PocketSharp/Models/Parameters/ActionParameter.cs b/PocketSharp/Models/Parameters/ActionParameter.cs new file mode 100644 index 0000000..7c331cb --- /dev/null +++ b/PocketSharp/Models/Parameters/ActionParameter.cs @@ -0,0 +1,32 @@ +using RestSharp; +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Runtime.Serialization; + +namespace PocketSharp.Models +{ + public class ActionParameter : ParameterBase + { + public string Action { get; set; } + + public int ID { get; set; } + + public DateTime? Time { get; set; } + + + public object Convert() + { + Dictionary parameters = new Dictionary + { + { "item_id", ID.ToString() }, + { "action", Action } + }; + + if(Time != null) + parameters.Add( "time", (int)((DateTime)Time - new DateTime(1970, 1, 1)).TotalSeconds ); + + return parameters; + } + } +} diff --git a/PocketSharp/Models/Parameters/ModifyParameters.cs b/PocketSharp/Models/Parameters/ModifyParameters.cs new file mode 100644 index 0000000..8d411a3 --- /dev/null +++ b/PocketSharp/Models/Parameters/ModifyParameters.cs @@ -0,0 +1,28 @@ +using RestSharp; +using ServiceStack.Text; +using System; +using System.Collections.Generic; + +namespace PocketSharp.Models +{ + public class ModifyParameters : ParameterBase + { + public List Actions { get; set; } + + + public List Convert() + { + List parameters = new List(); + List actions = new List(); + + Actions.ForEach(delegate(ActionParameter action) + { + actions.Add(action.Convert()); + }); + + parameters.Add(CreateParam("actions", JsonSerializer.SerializeToString(actions))); + + return parameters; + } + } +} diff --git a/PocketSharp/Models/Parameters/ParameterBase.cs b/PocketSharp/Models/Parameters/ParameterBase.cs index 347a615..14653e1 100644 --- a/PocketSharp/Models/Parameters/ParameterBase.cs +++ b/PocketSharp/Models/Parameters/ParameterBase.cs @@ -8,7 +8,7 @@ namespace PocketSharp.Models { public abstract class ParameterBase { - protected Parameter CreateParam(string name, object value, ParameterType type = ParameterType.GetOrPost) + protected static Parameter CreateParam(string name, object value, ParameterType type = ParameterType.GetOrPost) { return new Parameter() { diff --git a/PocketSharp/Models/PocketTag.cs b/PocketSharp/Models/PocketTag.cs new file mode 100644 index 0000000..2a6ced1 --- /dev/null +++ b/PocketSharp/Models/PocketTag.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace PocketSharp.Models +{ + public class PocketTag + { + } +} diff --git a/PocketSharp/PocketClient.cs b/PocketSharp/PocketClient.cs index d45ee34..8d8e054 100644 --- a/PocketSharp/PocketClient.cs +++ b/PocketSharp/PocketClient.cs @@ -1,7 +1,9 @@ -using RestSharp; +using PocketSharp.Models; +using RestSharp; using System; using System.Collections.Generic; using System.Net; +using ServiceStack.Text; namespace PocketSharp { @@ -131,14 +133,14 @@ namespace PocketSharp /// - /// Fetches a typed resource + /// Fetches/Updates a typed resource /// /// /// Requested method (path after /v3/) /// Additional POST parameters /// /// No access token available. Use authentification first. - protected T GetResource(string method, List parameters = null) where T : class, new() + protected T Get(string method, List parameters = null) where T : class, new() { // every single Pocket API endpoint requires HTTP POST data var request = new RestRequest(method, Method.POST); @@ -163,6 +165,22 @@ namespace PocketSharp } + protected T Put(string method, List actions) where T : class, new() + { + // put requests only with authentification + ExpectAuthentification(); + + ModifyParameters parameters = new ModifyParameters() + { + Actions = actions + }; + + //var x = (parameters.Convert()); + + return Get(method, parameters.Convert()); + } + + /// /// Validates the response. /// diff --git a/PocketSharp/PocketSharp.csproj b/PocketSharp/PocketSharp.csproj index 1f78d05..b366538 100644 --- a/PocketSharp/PocketSharp.csproj +++ b/PocketSharp/PocketSharp.csproj @@ -48,13 +48,18 @@ + + + + +