add authentification for Retrieve; remove Utilties namespace to provide better access for APIException;

This commit is contained in:
2013-06-20 15:10:48 +02:00
parent fdef02b106
commit 1f5c4d672c
6 changed files with 95 additions and 14 deletions
+8 -2
View File
@@ -13,12 +13,18 @@ namespace PocketSharp.Console
System.Console.WriteLine("PocketClient internal usage tests");
System.Console.WriteLine("---------------------------------");
// this apiKey is just for demonstration purposes
// this consumerKey is just for demonstration purposes
// please create your own application and retrieve it's key. It's a 1-step process ;-)
PocketClient client = new PocketClient(
consumerKey: "15396-f6f92101d72c8e270a6c9bb3"
consumerKey: "15396-f6f92101d72c8e270a6c9bb3",
accessCode: "a85134a7-243c-6656-ab82-97c901"
);
client.Search("css").ForEach(delegate(PocketItem item)
{
System.Console.WriteLine(item.FullTitle);
});
System.Console.ReadKey();
}
}
+25
View File
@@ -0,0 +1,25 @@
using System;
using System.Runtime.Serialization;
namespace PocketSharp
{
[Serializable]
public class APIException : Exception
{
/// <summary>
/// necessary constructors for the exception
/// including inner exceptions and serialization
/// </summary>
public APIException()
: base() { }
public APIException(string message)
: base(message) { }
public APIException(string message, Exception innerException)
: base(message, innerException) { }
protected APIException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
}
}
+6
View File
@@ -12,6 +12,7 @@ namespace PocketSharp
/// <returns></returns>
public List<PocketItem> Retrieve(RetrieveParameters parameters)
{
ExpectAuthentification();
return GetResource<Retrieve>("get", parameters.Convert()).Items;
}
@@ -23,6 +24,8 @@ namespace PocketSharp
/// <returns></returns>
public List<PocketItem> Retrieve(RetrieveFilter filter = RetrieveFilter.All)
{
ExpectAuthentification();
RetrieveParameters parameters = new RetrieveParameters();
switch(filter)
@@ -58,6 +61,7 @@ namespace PocketSharp
/// <returns></returns>
public List<PocketItem> SearchByTag(string tag)
{
ExpectAuthentification();
RetrieveParameters parameters = new RetrieveParameters()
{
Tag = tag
@@ -73,6 +77,7 @@ namespace PocketSharp
/// <returns></returns>
public List<PocketItem> Search(string searchString)
{
ExpectAuthentification();
RetrieveParameters parameters = new RetrieveParameters()
{
Search = searchString
@@ -81,6 +86,7 @@ namespace PocketSharp
}
}
/// <summary>
/// Filter for simple retrieve requests
/// </summary>
+34
View File
@@ -0,0 +1,34 @@
using RestSharp;
using RestSharp.Deserializers;
using ServiceStack.Text;
namespace PocketSharp
{
internal class JsonDeserializer : IDeserializer
{
public const string JsonContentType = "application/json";
/// <summary>
/// Deserializes the specified response.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="response">The response.</param>
/// <returns></returns>
public T Deserialize<T>(IRestResponse response)
{
var x = JsonSerializer.DeserializeFromString<T>(response.Content);
return x;
}
public string DateFormat { get; set; }
public string Namespace { get; set; }
public string RootElement { get; set; }
public string ContentType
{
get { return JsonContentType; }
}
}
}
+17 -9
View File
@@ -2,8 +2,6 @@
using System;
using System.Collections.Generic;
using System.Net;
using PocketSharp.Utilities;
using PocketSharp.Models;
namespace PocketSharp
{
@@ -135,14 +133,11 @@ namespace PocketSharp
// 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);
if (AccessCode != null)
{
request.AddParameter("access_token", AccessCode);
}
// enumeration for params
parameters.ForEach(delegate(Parameter param)
@@ -174,5 +169,18 @@ namespace PocketSharp
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.");
}
}
}
}
+5 -3
View File
@@ -46,10 +46,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Utilties\APIException.cs" />
<Compile Include="APIException.cs" />
<Compile Include="Components\Authentification.cs" />
<Compile Include="Components\Retrieve.cs" />
<Compile Include="Utilties\JsonDeserializer.cs" />
<Compile Include="JsonDeserializer.cs" />
<Compile Include="Models\Authentification\AccessCode.cs" />
<Compile Include="Models\Authentification\RequestCode.cs" />
<Compile Include="Models\Parameters\ParameterBase.cs" />
@@ -60,7 +60,9 @@
<Compile Include="PocketClient.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="Utilties\" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>