simple PocketItem; GetResource method;

This commit is contained in:
2013-06-14 02:04:18 +02:00
parent a3cd9250a2
commit 3c1990982b
6 changed files with 96 additions and 6 deletions
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PocketSharp
{
public partial class PocketClient
{
}
}
+11
View File
@@ -1,5 +1,6 @@
using RestSharp;
using System;
using System.Collections.Generic;
namespace PocketSharp
{
@@ -35,5 +36,15 @@ namespace PocketSharp
/// <param name="request">The request.</param>
/// <returns></returns>
T Request<T>(RestRequest request) where T : new();
/// <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>
/// <returns></returns>
/// <exception cref="APIException">No access token available. Use authentification first.</exception>
public T GetResource<T>(string method, List<Parameter> parameters) where T : class, new();
}
}
+31
View File
@@ -0,0 +1,31 @@
using RestSharp.Serializers;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace PocketSharp.Models
{
[DataContract]
class PocketItem
{
[DataMember(Name = "item_id")]
public int ID { get; set; }
private Uri _uri;
[DataMember(Name = "resolved_url")]
public Uri Uri
{
get { return _uri; }
set { _uri = new Uri(value.ToString()); }
}
[DataMember(Name = "resolved_title")]
public string Title { get; set; }
[DataMember(Name = "given_title")]
public string FullTitle { get; set; }
[DataMember]
public string Excerpt { get; set; }
}
}
+38 -2
View File
@@ -1,6 +1,6 @@
using PocketSharp.Models.Authentification;
using RestSharp;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Net;
namespace PocketSharp
@@ -119,6 +119,9 @@ namespace PocketSharp
/// </summary>
/// <param name="response">The response.</param>
/// <returns></returns>
/// <exception cref="APIException">
/// Error retrieving response
/// </exception>
protected bool ValidateResponse(IRestResponse response)
{
if (response.StatusCode != HttpStatusCode.OK)
@@ -132,5 +135,38 @@ namespace PocketSharp
return true;
}
/// <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>
/// <returns></returns>
/// <exception cref="APIException">No access token available. Use authentification first.</exception>
public T GetResource<T>(string method, List<Parameter> parameters) where T : class, new()
{
// every single Pocket API endpoint requires HTTP POST data
var request = new RestRequest(method, Method.POST);
// check if access token is available
if(AccessCode == null)
{
throw new APIException("No access token available. Use authentification first.");
}
// add access token (necessary for all requests except authentification)
request.AddParameter("access_token", AccessCode);
// enumeration for params
parameters.ForEach(delegate(Parameter param)
{
request.AddParameter(param);
});
// do the request
return Request<T>(request);
}
}
}
+4 -4
View File
@@ -47,16 +47,16 @@
</ItemGroup>
<ItemGroup>
<Compile Include="APIException.cs" />
<Compile Include="Components\Authentification.cs" />
<Compile Include="IPocketClient.cs" />
<Compile Include="JsonDeserializer.cs" />
<Compile Include="Models\Authentification\AcessCode.cs" />
<Compile Include="Models\Authentification\AccessCode.cs" />
<Compile Include="Models\Authentification\RequestCode.cs" />
<Compile Include="Models\PocketItem.cs" />
<Compile Include="PocketClient.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Components\" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>