using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
//using Mobile.Comuns.Filter;
//using Mobile.Struct;
using RestSharp;
using SendMailOrdenado.Demo.Core.ViewModels;
using System.Threading.Tasks;
using RestSharp.Authenticators;
using Newtonsoft.Json;
namespace SendMailOrdenado.Demo.WebApi.Controllers.Api
{
public class ClientRest
{
#region Variáveis e inicializador
private const string JsonContentType = "application/json; charset=utf-8";
public string ServiceBaseUrl { get; private set; }
public string Token { get; private set; }
public ClientRest(string serviceBaseUrl, string token)
{
if (token == null) throw new ArgumentNullException("token");
ServiceBaseUrl = serviceBaseUrl;
Token = token;
}
#endregion
#region Métodos GET
public Task<T> ExecuteAssync<T>(RestRequest request) where T : new()
{
var client = new RestClient();
var taskCompletionSource = new TaskCompletionSource<T>();
client.BaseUrl = new RestClient(ServiceBaseUrl).BaseUrl;
//client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
//request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
client.ExecuteAsync<T>(request, (response) => taskCompletionSource.SetResult(response.Data));
return taskCompletionSource.Task;
}
public T Execute<T>(IRestRequest request) where T : new()
{
var client = new RestClient(ServiceBaseUrl);
request.Method = Method.GET;
request.AddHeader("Token", Token);
var retorno = client.Execute<T>(request);
return retorno.Data;
}
#endregion
}
}