85 lines
3.0 KiB
C#
85 lines
3.0 KiB
C#
|
using RestSharp;
|
|||
|
using RestSharp.Authenticators;
|
|||
|
using RestSharp.Deserializers;
|
|||
|
using System;
|
|||
|
using System.Configuration;
|
|||
|
using System.Net;
|
|||
|
using System.Security.Authentication;
|
|||
|
using Tanshu.Accounts.Contracts;
|
|||
|
|
|||
|
namespace Tanshu.Accounts.Repository
|
|||
|
{
|
|||
|
public sealed class JsonStore
|
|||
|
{
|
|||
|
private static JsonStore _instance = new JsonStore(ConfigurationManager.AppSettings["server"]);
|
|||
|
|
|||
|
private readonly Uri _baseUrl;
|
|||
|
private readonly CookieContainer _cookieJar;
|
|||
|
|
|||
|
private JsonStore(string baseUrl)
|
|||
|
{
|
|||
|
_baseUrl = new System.Uri(baseUrl);
|
|||
|
_cookieJar = new CookieContainer();
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public static JsonStore GetStore()
|
|||
|
{
|
|||
|
return _instance;
|
|||
|
}
|
|||
|
public static T Execute<T>(RestRequest request) where T : new()
|
|||
|
{
|
|||
|
return _instance.Ex<T>(request);
|
|||
|
}
|
|||
|
public static void Execute(RestRequest request)
|
|||
|
{
|
|||
|
_instance.Ex(request);
|
|||
|
}
|
|||
|
public T Ex<T>(RestRequest request) where T : new()
|
|||
|
{
|
|||
|
var client = new RestClient();
|
|||
|
client.BaseUrl = _baseUrl;
|
|||
|
client.CookieContainer = _cookieJar;
|
|||
|
if (Session.IsAuthenticated)
|
|||
|
client.Authenticator = new HttpBasicAuthenticator(Session.User.Name, Session.User.Password);
|
|||
|
request.DateFormat = "dd-MMM-yyyy HH:mm:ss";
|
|||
|
var response = client.Execute<T>(request);
|
|||
|
|
|||
|
if (response.ErrorException != null)
|
|||
|
throw response.ErrorException;
|
|||
|
if (response.StatusCode != HttpStatusCode.OK)
|
|||
|
HandleHTTPException(response);
|
|||
|
return response.Data;
|
|||
|
}
|
|||
|
public void Ex(RestRequest request)
|
|||
|
{
|
|||
|
var client = new RestClient();
|
|||
|
client.BaseUrl = _baseUrl;
|
|||
|
client.CookieContainer = _cookieJar;
|
|||
|
if (Session.IsAuthenticated)
|
|||
|
client.Authenticator = new HttpBasicAuthenticator(Session.User.Name, Session.User.Password);
|
|||
|
request.DateFormat = "dd-MMM-yyyy HH:mm:ss";
|
|||
|
var response = client.Execute(request);
|
|||
|
if (response.ErrorException != null)
|
|||
|
throw response.ErrorException;
|
|||
|
if (response.StatusCode != HttpStatusCode.OK)
|
|||
|
HandleHTTPException(response);
|
|||
|
}
|
|||
|
|
|||
|
private void HandleHTTPException(IRestResponse response)
|
|||
|
{
|
|||
|
string message = new JsonDeserializer().Deserialize<dynamic>(response)["Error"];
|
|||
|
if (response.StatusCode == HttpStatusCode.Forbidden)
|
|||
|
throw new AuthenticationException(string.Format("Forbidden (403) - {0}", message));
|
|||
|
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
|||
|
throw new AuthenticationException(string.Format("Unauthorized (401) - {0}", message));
|
|||
|
if (response.StatusCode == HttpStatusCode.InternalServerError)
|
|||
|
throw new ArgumentException(string.Format("Error - {0}", message));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|