simplify parameter creation; fix several deserialization issues

This commit is contained in:
2013-09-16 23:34:13 +02:00
parent 6cf9892f78
commit e522c8e30a
14 changed files with 164 additions and 143 deletions
+14 -1
View File
@@ -6,8 +6,21 @@ using PocketSharp.Models;
namespace PocketSharp.Tests
{
class AddTests : TestsBase
public class AddTests : TestsBase
{
public AddTests() : base() { }
[Fact]
public async Task AddSimpleItemWithUriOnly()
{
var uri = new Uri("http://frontendplay.com");
PocketItem item = await client.Add(uri);
Assert.Equal<Uri>(uri, item.Uri);
itemsToDelete.Add(item.ID);
}
}
}
+12 -13
View File
@@ -6,10 +6,9 @@ using PocketSharp.Models;
namespace PocketSharp.Tests
{
public class RetrieveTests : TestsBase
{
public RetrieveTests() : base() {}
public RetrieveTests() : base() { }
[Fact]
@@ -21,20 +20,20 @@ namespace PocketSharp.Tests
}
[Fact]
public async Task ItemContainsUri()
{
List<PocketItem> items = await client.Retrieve(new RetrieveParameters()
{
Count = 1
});
//[Fact]
//public async Task ItemContainsUri()
//{
// List<PocketItem> items = await client.Retrieve(new RetrieveParameters()
// {
// Count = 1
// });
Assert.True(items.Count == 1);
// Assert.True(items.Count == 1);
PocketItem item = items[0];
// PocketItem item = items[0];
Assert.True(item.Uri.ToString().StartsWith("http"));
}
// Assert.True(item.Uri.ToString().StartsWith("http"));
//}
[Fact]
+7 -2
View File
@@ -10,6 +10,8 @@ namespace PocketSharp.Tests
{
protected PocketClient client;
protected List<int> itemsToDelete = new List<int>();
// setup
public TestsBase()
@@ -24,9 +26,12 @@ namespace PocketSharp.Tests
// teardown
public void Dispose()
public void Dispose()
{
itemsToDelete.ForEach(async id =>
{
await client.Delete(id);
});
}
}
}
+9 -2
View File
@@ -1,5 +1,6 @@
using PocketSharp.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace PocketSharp
@@ -22,7 +23,10 @@ namespace PocketSharp
}
// do request
RequestCode response = await Request<RequestCode>("oauth/request", Utilities.CreateParamInList("redirect_uri", CallbackUri));
RequestCode response = await Request<RequestCode>("oauth/request", new Dictionary<string, string>()
{
{ "redirect_uri", CallbackUri }
});
// save code to client
RequestCode = response.Code;
@@ -74,7 +78,10 @@ namespace PocketSharp
}
// do request
AccessCode response = await Request<AccessCode>("oauth/authorize", Utilities.CreateParamInList("code", RequestCode));
AccessCode response = await Request<AccessCode>("oauth/authorize", new Dictionary<string, string>()
{
{ "code", RequestCode }
});
// save code to client
AccessCode = response.Code;
+1 -1
View File
@@ -14,7 +14,7 @@ namespace PocketSharp
/// </summary>
/// <param name="parameters">parameters, which are mapped to the officials from http://getpocket.com/developer/docs/v3/retrieve </param>
/// <returns></returns>
public async Task<List<PocketItem>> Retrieve(RetrieveParameters parameters)
internal async Task<List<PocketItem>> Retrieve(RetrieveParameters parameters)
{
Retrieve response = await Request<Retrieve>("get", parameters.Convert(), true);
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace PocketSharp.Models
{
/// <summary>
/// All parameters which can be passed for a modify action
/// </summary>
[DataContract]
public class ActionParameter
{
/// <summary>
@@ -14,6 +16,7 @@ namespace PocketSharp.Models
/// <value>
/// The action.
/// </value>
[DataMember(Name = "action")]
public string Action { get; set; }
/// <summary>
@@ -22,6 +25,7 @@ namespace PocketSharp.Models
/// <value>
/// The ID.
/// </value>
[DataMember(Name = "item_id")]
public int ID { get; set; }
/// <summary>
@@ -30,6 +34,7 @@ namespace PocketSharp.Models
/// <value>
/// The time.
/// </value>
[DataMember(Name = "time")]
public DateTime? Time { get; set; }
// specific params
@@ -40,6 +45,7 @@ namespace PocketSharp.Models
/// <value>
/// The tags.
/// </value>
[DataMember(Name = "tags")]
public string[] Tags { get; set; }
/// <summary>
@@ -48,6 +54,7 @@ namespace PocketSharp.Models
/// <value>
/// The old tag.
/// </value>
[DataMember(Name = "old_tag")]
public string OldTag { get; set; }
/// <summary>
@@ -56,6 +63,7 @@ namespace PocketSharp.Models
/// <value>
/// The new tag.
/// </value>
[DataMember(Name = "new_tag")]
public string NewTag { get; set; }
@@ -63,7 +71,7 @@ namespace PocketSharp.Models
/// Converts this instance to a parameter list.
/// </summary>
/// <returns></returns>
public object Convert()
public Dictionary<string, object> Convert()
{
Dictionary<string, object> parameters = new Dictionary<string, object>
{
+9 -16
View File
@@ -1,12 +1,16 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Linq;
namespace PocketSharp.Models
{
/// <summary>
/// All parameters which can be passed to add a new item
/// </summary>
public class AddParameters
[DataContract]
internal class AddParameters : Parameters
{
/// <summary>
/// Gets or sets the URI.
@@ -14,6 +18,7 @@ namespace PocketSharp.Models
/// <value>
/// The URI.
/// </value>
[DataMember(Name="url")]
public Uri Uri { get; set; }
/// <summary>
@@ -22,6 +27,7 @@ namespace PocketSharp.Models
/// <value>
/// The title.
/// </value>
[DataMember(Name="title")]
public string Title { get; set; }
/// <summary>
@@ -30,6 +36,7 @@ namespace PocketSharp.Models
/// <value>
/// The tags.
/// </value>
[DataMember(Name="tags")]
public string[] Tags { get; set; }
/// <summary>
@@ -38,21 +45,7 @@ namespace PocketSharp.Models
/// <value>
/// The tweet ID.
/// </value>
[DataMember(Name="tweet_id")]
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,11 +1,13 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace PocketSharp.Models
{
/// <summary>
/// All parameters which can be passed to modify an item
/// </summary>
public class ModifyParameters
[DataContract]
internal class ModifyParameters : Parameters
{
/// <summary>
/// Gets or sets the actions.
@@ -13,23 +15,7 @@ namespace PocketSharp.Models
/// <value>
/// The actions.
/// </value>
[DataMember(Name = "actions")]
public List<ActionParameter> Actions { get; set; }
/// <summary>
/// Converts this instance to a parameter list.
/// </summary>
/// <returns></returns>
public List<Parameter> Convert()
{
List<object> actions = new List<object>();
//Actions.ForEach(action => actions.Add(action.Convert()));
return new List<Parameter>()
{
// Utilities.CreateParam("actions", JsonSerializer.SerializeToString(actions))
};
}
}
}
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Linq;
namespace PocketSharp.Models
{
/// <summary>
/// Parameter
/// </summary>
internal class Parameters
{
/// <summary>
/// Converts an object to a list of HTTP Post parameters.
/// </summary>
/// <returns></returns>
public Dictionary<string, string> Convert()
{
// store HTTP parameters here
Dictionary<string, string> parameterDict = new Dictionary<string, string>();
// get object properties
IEnumerable<PropertyInfo> properties = this.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(p => Attribute.IsDefined(p, typeof(DataMemberAttribute)));
// gather attributes of object
foreach (PropertyInfo propertyInfo in properties)
{
DataMemberAttribute attribute = (DataMemberAttribute)propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), false).FirstOrDefault();
string name = attribute.Name ?? propertyInfo.Name.ToLower();
if (propertyInfo.GetValue(this, null) != null)
{
parameterDict.Add(name, propertyInfo.GetValue(this, null).ToString());
}
}
return parameterDict;
}
}
}
@@ -1,12 +1,15 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
namespace PocketSharp.Models
{
/// <summary>
/// All parameters which can be passed for item retrieval
/// </summary>
public class RetrieveParameters
[DataContract]
internal class RetrieveParameters : Parameters
{
/// <summary>
/// Gets or sets the state.
@@ -14,6 +17,7 @@ namespace PocketSharp.Models
/// <value>
/// The state.
/// </value>
[DataMember(Name = "state")]
public State? State { get; set; }
/// <summary>
@@ -22,6 +26,7 @@ namespace PocketSharp.Models
/// <value>
/// The favorite.
/// </value>
[DataMember(Name = "favorite")]
public bool? Favorite { get; set; }
/// <summary>
@@ -30,6 +35,7 @@ namespace PocketSharp.Models
/// <value>
/// The tag.
/// </value>
[DataMember(Name = "tag")]
public string Tag { get; set; }
/// <summary>
@@ -38,6 +44,7 @@ namespace PocketSharp.Models
/// <value>
/// The type of the content.
/// </value>
[DataMember(Name = "contentType")]
public ContentType? ContentType { get; set; }
/// <summary>
@@ -46,6 +53,7 @@ namespace PocketSharp.Models
/// <value>
/// The sort.
/// </value>
[DataMember(Name = "sort")]
public Sort? Sort { get; set; }
/// <summary>
@@ -54,6 +62,7 @@ namespace PocketSharp.Models
/// <value>
/// The type of the detail.
/// </value>
[DataMember(Name="detailType")]
public DetailType? DetailType { get; set; }
/// <summary>
@@ -62,6 +71,7 @@ namespace PocketSharp.Models
/// <value>
/// The search.
/// </value>
[DataMember(Name = "search")]
public string Search { get; set; }
/// <summary>
@@ -70,6 +80,7 @@ namespace PocketSharp.Models
/// <value>
/// The domain.
/// </value>
[DataMember(Name = "domain")]
public string Domain { get; set; }
/// <summary>
@@ -78,6 +89,7 @@ namespace PocketSharp.Models
/// <value>
/// The since.
/// </value>
[DataMember(Name = "since")]
public DateTime? Since { get; set; }
/// <summary>
@@ -86,6 +98,7 @@ namespace PocketSharp.Models
/// <value>
/// The count.
/// </value>
[DataMember(Name = "count")]
public int? Count { get; set; }
/// <summary>
@@ -94,30 +107,8 @@ namespace PocketSharp.Models
/// <value>
/// The offset.
/// </value>
[DataMember(Name = "offset")]
public int? Offset { get; set; }
/// <summary>
/// Converts this instance to a parameter list.
/// </summary>
/// <returns></returns>
public List<Parameter> Convert()
{
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)
};
}
}
+32 -10
View File
@@ -37,7 +37,29 @@ namespace PocketSharp.Models
/// The title.
/// </value>
[JsonProperty("resolved_title")]
public string Title { get; set; }
private string _ResolvedTitle { get; set; }
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>
/// The title.
/// </value>
[JsonProperty("title")]
private string _InternalTitle { get; set; }
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>
/// The title.
/// </value>
[JsonIgnore]
public string Title
{
get { return _InternalTitle ?? _ResolvedTitle; }
set { _InternalTitle = value; _ResolvedTitle = value; }
}
/// <summary>
/// Gets or sets the full title.
@@ -183,7 +205,7 @@ namespace PocketSharp.Models
/// The _ tag dictionary.
/// </value>
[JsonProperty("tags")]
public Dictionary<string, PocketTag> TagDictionary { get; set; }
private Dictionary<string, PocketTag> _TagDictionary { get; set; }
/// <summary>
/// Gets or sets the _ image dictionary.
@@ -192,7 +214,7 @@ namespace PocketSharp.Models
/// The _ image dictionary.
/// </value>
[JsonProperty("images")]
public Dictionary<string, PocketImage> ImageDictionary { get; set; }
private Dictionary<string, PocketImage> _ImageDictionary { get; set; }
/// <summary>
/// Gets or sets the _ video dictionary.
@@ -201,7 +223,7 @@ namespace PocketSharp.Models
/// The _ video dictionary.
/// </value>
[JsonProperty("videos")]
public Dictionary<string, PocketVideo> VideoDictionary { get; set; }
private Dictionary<string, PocketVideo> _VideoDictionary { get; set; }
/// <summary>
/// Gets or sets the _ author dictionary.
@@ -210,7 +232,7 @@ namespace PocketSharp.Models
/// The _ author dictionary.
/// </value>
[JsonProperty("authors")]
public Dictionary<string, PocketAuthor> AuthorDictionary { get; set; }
private Dictionary<string, PocketAuthor> _AuthorDictionary { get; set; }
/// <summary>
@@ -222,7 +244,7 @@ namespace PocketSharp.Models
[JsonIgnore]
public List<PocketTag> Tags
{
get { return Utilities.DictionaryToList<PocketTag>(TagDictionary); }
get { return Utilities.DictionaryToList<PocketTag>(_TagDictionary); }
}
/// <summary>
@@ -234,7 +256,7 @@ namespace PocketSharp.Models
[JsonIgnore]
public List<PocketImage> Images
{
get { return Utilities.DictionaryToList<PocketImage>(ImageDictionary); }
get { return Utilities.DictionaryToList<PocketImage>(_ImageDictionary); }
}
/// <summary>
@@ -246,7 +268,7 @@ namespace PocketSharp.Models
[JsonIgnore]
public PocketImage LeadImage
{
get { return Images != null ? Images[0] : null; }
get { return Images.Count > 0 ? Images[0] : null; }
}
/// <summary>
@@ -258,7 +280,7 @@ namespace PocketSharp.Models
[JsonIgnore]
public List<PocketVideo> Videos
{
get { return Utilities.DictionaryToList<PocketVideo>(VideoDictionary); }
get { return Utilities.DictionaryToList<PocketVideo>(_VideoDictionary); }
}
/// <summary>
@@ -270,7 +292,7 @@ namespace PocketSharp.Models
[JsonIgnore]
public List<PocketAuthor> Authors
{
get { return Utilities.DictionaryToList<PocketAuthor>(AuthorDictionary); }
get { return Utilities.DictionaryToList<PocketAuthor>(_AuthorDictionary); }
}
}
}
+7 -28
View File
@@ -94,50 +94,27 @@ namespace PocketSharp
}
/// <summary>
/// Fetches a typed resource
/// </summary>
/// <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="PocketException">No access token available. Use authentification first.</exception>
protected async Task<T> Request<T>(string method, List<Parameter> parameters = null, bool requireAuth = false) where T : class, new()
protected async Task<T> Request<T>(string method, Dictionary<string, string> parameters = null, bool requireAuth = false) where T : class, new()
{
if (requireAuth && AccessCode == null)
{
throw new PocketException("SDK error: No access token available. Use authentification first.");
}
// convert parameters
Dictionary<string, string> parameterDict = new Dictionary<string, string>();
if (parameters != null)
{
foreach (Parameter item in parameters)
{
if (item.Value != null)
{
parameterDict.Add(item.Name, item.Value.ToString());
}
}
}
// every single Pocket API endpoint requires HTTP POST data
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, method);
// add consumer key to each request
parameterDict.Add("consumer_key", ConsumerKey);
parameters.Add("consumer_key", ConsumerKey);
// add access token (necessary for all requests except authentification)
if (AccessCode != null)
{
parameterDict.Add("access_token", AccessCode);
parameters.Add("access_token", AccessCode);
}
// content of the request
request.Content = new FormUrlEncodedContent(parameterDict);
request.Content = new FormUrlEncodedContent(parameters);
// make async request
HttpResponseMessage response = await _restClient.SendAsync(request);
@@ -148,9 +125,11 @@ namespace PocketSharp
// read response
var responseString = await response.Content.ReadAsStringAsync();
responseString = responseString.Replace("[]", "{}");
// deserialize object
T parsedResponse = JsonConvert.DeserializeObject<T>(
responseString,
responseString,
new JsonSerializerSettings
{
Error = (object sender, ErrorEventArgs args) =>
+1 -1
View File
@@ -49,7 +49,7 @@
<Compile Include="Models\Parameters\ActionParameter.cs" />
<Compile Include="Models\Parameters\AddParameters.cs" />
<Compile Include="Models\Parameters\ModifyParameters.cs" />
<Compile Include="Models\Parameters\Parameter.cs" />
<Compile Include="Models\Parameters\Parameters.cs" />
<Compile Include="Models\Parameters\RetrieveParameters.cs" />
<Compile Include="Models\PocketAuthor.cs" />
<Compile Include="Models\PocketImage.cs" />
-26
View File
@@ -25,32 +25,6 @@ namespace PocketSharp
}
/// <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)
{
return new Parameter() { Name = name, Value = value };
}
/// <summary>
/// Creates a Parameter object within a list.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
/// <param name="type">The type.</param>
/// <returns></returns>
public static List<Parameter> CreateParamInList(string name, object value)
{
return new List<Parameter>() { CreateParam(name, value) };
}
/// <summary>
/// Convert a dictionary to a list
/// </summary>