add exception handling for requests
This commit is contained in:
@@ -15,15 +15,17 @@ namespace PocketSharp.Console
|
||||
// please create your own application and retrieve it's key. It's a 1-step process ;-)
|
||||
PocketClient client = new PocketClient("15396-f6f92101d72c8e270a6c9bb3");
|
||||
|
||||
client.Test();
|
||||
//client.Test();
|
||||
|
||||
System.Console.WriteLine(client.Test2());
|
||||
//System.Console.WriteLine(client.Test2());
|
||||
|
||||
System.Console.WriteLine("---------------------------------");
|
||||
//System.Console.WriteLine("---------------------------------");
|
||||
|
||||
var result = client.Test3();
|
||||
//var result = client.Test3();
|
||||
|
||||
System.Console.WriteLine(string.Format("Code: {0}, Username: {1}", result.Code, result.Username));
|
||||
//System.Console.WriteLine(string.Format("Code: {0}, Username: {1}", result.Code, result.Username));
|
||||
|
||||
client.Test4("a85134a7-243c-6656-ab82-97c901");
|
||||
|
||||
System.Console.ReadKey();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,11 @@ namespace PocketSharp
|
||||
/// </summary>
|
||||
string ConsumerKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns all associated data from the last request
|
||||
/// </summary>
|
||||
IRestResponse LastRequestData { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Makes a HTTP REST request to the API
|
||||
/// </summary>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using PocketSharp.Models.Authentification;
|
||||
using RestSharp;
|
||||
using System;
|
||||
using System.Net;
|
||||
|
||||
namespace PocketSharp
|
||||
{
|
||||
@@ -27,6 +28,11 @@ namespace PocketSharp
|
||||
/// </summary>
|
||||
public string ConsumerKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns all associated data from the last request
|
||||
/// </summary>
|
||||
public IRestResponse LastRequestData { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Code retrieved on authentification
|
||||
/// </summary>
|
||||
@@ -38,7 +44,6 @@ namespace PocketSharp
|
||||
protected string AccessCode { get; set; }
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PocketClient"/> class.
|
||||
/// </summary>
|
||||
@@ -95,7 +100,18 @@ namespace PocketSharp
|
||||
/// <returns></returns>
|
||||
public T Request<T>(RestRequest request) where T : new()
|
||||
{
|
||||
var response = _restClient.Execute<T>(request);
|
||||
IRestResponse<T> response = _restClient.Execute<T>(request);
|
||||
|
||||
LastRequestData = response;
|
||||
|
||||
if(response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
throw new APIException(response.Content, response.ErrorException);
|
||||
}
|
||||
else if(response.ErrorException != null)
|
||||
{
|
||||
throw new APIException("Error retrieving response", response.ErrorException);
|
||||
}
|
||||
|
||||
return response.Data;
|
||||
}
|
||||
@@ -122,11 +138,23 @@ namespace PocketSharp
|
||||
{
|
||||
var request = new RestRequest("oauth/authorize", Method.POST);
|
||||
|
||||
request.AddParameter("code", RequestCode);
|
||||
request.AddParameter("code", "fbe15035-d6b1-e4c2-590b-60de6e");
|
||||
|
||||
AccessCode rawResponse = Request<AccessCode>(request);
|
||||
|
||||
return rawResponse;
|
||||
}
|
||||
|
||||
public bool Test4(string code)
|
||||
{
|
||||
var request = new RestRequest("get", Method.POST);
|
||||
|
||||
request.AddParameter("access_toiken", code);
|
||||
request.AddParameter("favorite", "1");
|
||||
|
||||
AccessCode rawResponse = Request<AccessCode>(request);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user