Created
June 2, 2017 20:49
-
-
Save rafaelrmou/18264811b3b717f8a3e4e1f394c542fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Task<T> Delete<T>(string method, string id) | |
{ | |
throw new NotImplementedException(); | |
} | |
public async Task<T> GetOne<T>(string method, Dictionary<string, object> parameters = null) | |
{ | |
var stopWatch = System.Diagnostics.Stopwatch.StartNew(); | |
var nativeHandler = new NativeMessageHandler(); | |
#if __Gzip__ | |
if (nativeHandler.SupportsAutomaticDecompression) | |
nativeHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; | |
#endif | |
if (await this.HasConn()) | |
using (var httpClient = new HttpClient(nativeHandler)) | |
//using (var httpClient = new HttpClient()) | |
{ | |
try | |
{ | |
httpClient.BaseAddress = new Uri(Constants.BaseUrl); | |
System.Diagnostics.Debug.WriteLine(Constants.BaseUrl + method); | |
var response = await httpClient.GetAsync(Constants.BaseUrl + method); | |
if (response.IsSuccessStatusCode) | |
{ | |
var responseContent = await response.Content.ReadAsStringAsync(); | |
if (string.IsNullOrEmpty(responseContent)) | |
{ | |
return default(T); | |
} | |
try | |
{ | |
var objetoRetornado = JsonConvert.DeserializeObject<T>(responseContent); | |
System.Diagnostics.Debug.WriteLine($"Tempo da requisição: {stopWatch.Elapsed}"); | |
return objetoRetornado; | |
} | |
catch (JsonException exJ) | |
{ | |
System.Diagnostics.Debug.WriteLine(exJ.Message); | |
throw new CallRestException($"Erro de conversao {exJ.Message}"); | |
} | |
catch (Exception exG) | |
{ | |
System.Diagnostics.Debug.WriteLine(exG.Message); | |
throw new CallRestException($"Erro geral {0}"); | |
} | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) | |
{ | |
throw new CallRestException($"BadRequest"); | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError) | |
{ | |
throw new CallRestException("Internal"); | |
} | |
} | |
catch (TaskCanceledException tce) | |
{ | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(T); | |
//return await Post<T, R>(method, postObject, parameters); | |
} | |
catch (Exception ex) | |
{ | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(T); | |
} | |
} | |
return default(T); | |
} | |
public async Task<IEnumerable<T>> Get<T>(string method, Dictionary<string, object> parameters = null) | |
{ | |
var stopWatch = System.Diagnostics.Stopwatch.StartNew(); | |
var nativeHandler = new NativeMessageHandler(); | |
#if __Gzip__ | |
if (nativeHandler.SupportsAutomaticDecompression) | |
nativeHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; | |
#endif | |
if (await this.HasConn()) | |
using (var httpClient = new HttpClient(nativeHandler)) | |
//using (var httpClient = new HttpClient()) | |
{ | |
httpClient.BaseAddress = new Uri(Constants.BaseUrl); | |
if (method.Contains("?")) | |
method += $"&translateTo={App.CultureInfo.Split('-')[0]}"; | |
else | |
method += $"?translateTo={App.CultureInfo.Split('-')[0]}"; | |
System.Diagnostics.Debug.WriteLine(method); | |
try | |
{ | |
var response = await httpClient.GetAsync(method); | |
if (response.IsSuccessStatusCode) | |
{ | |
var responseContent = await response.Content.ReadAsStringAsync(); | |
if (string.IsNullOrEmpty(responseContent)) | |
throw new CallRestException(AppResources.NenhumResultadoText); | |
try | |
{ | |
System.Diagnostics.Debug.WriteLine($"Tempo da requisição: {stopWatch.Elapsed}"); | |
System.Diagnostics.Debug.WriteLine(responseContent); | |
var objetoRetornado = JsonConvert.DeserializeObject<List<T>>(responseContent); | |
return objetoRetornado; | |
} | |
catch (JsonException) | |
{ | |
throw new CallRestException(AppResources.ResultadoPesquisaInvalidoText); | |
} | |
catch (Exception ex) | |
{ | |
//throw new CallRestException(AppResources.ErroRequisicaoText); | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return null; | |
} | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) | |
{ | |
throw new CallRestException(AppResources.ServicoNaoEncontradoText); | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError) | |
{ | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return null; | |
//throw new CallRestException(AppResources.ErroRequisicaoText); | |
} | |
} | |
catch (TaskCanceledException tce) | |
{ | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return null; | |
//return await Post<T, R>(method, postObject, parameters); | |
} | |
catch (Exception exRequest) | |
{ | |
System.Diagnostics.Debug.WriteLine($"Método {method} -- Exception {exRequest.Message}"); | |
return null; | |
} | |
} | |
return null; | |
} | |
public Task<T> Get<T>(string method, string id, Dictionary<string, object> parameters = null) | |
{ | |
throw new NotImplementedException(); | |
} | |
public async Task<T> GetCustom<T>(string url, Dictionary<string, object> parameters = null) | |
{ | |
try | |
{ | |
var stopWatch = System.Diagnostics.Stopwatch.StartNew(); | |
var nativeHandler = new NativeMessageHandler(); | |
#if __Gzip__ | |
if (nativeHandler.SupportsAutomaticDecompression) | |
nativeHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; | |
#endif | |
if (await this.HasConn()) | |
using (var httpClient = new HttpClient(nativeHandler)) | |
//using (var httpClient = new HttpClient()) | |
{ | |
try | |
{ | |
//httpClient.BaseAddress = new Uri(Constants.BaseUrl); | |
if (url.Contains("?")) | |
url += $"&translateTo={App.CultureInfo.Split('-')[0]}"; | |
else | |
url += $"?translateTo={App.CultureInfo.Split('-')[0]}"; | |
System.Diagnostics.Debug.WriteLine(url); | |
var response = await httpClient.GetAsync(url); | |
if (response.IsSuccessStatusCode) | |
{ | |
var responseContent = await response.Content.ReadAsStringAsync(); | |
if (string.IsNullOrEmpty(responseContent)) | |
throw new CallRestException(AppResources.NenhumResultadoText); | |
try | |
{ | |
System.Diagnostics.Debug.WriteLine($"Tempo da requisição: {stopWatch.Elapsed}"); | |
var objetoRetornado = JsonConvert.DeserializeObject<T>(responseContent); | |
return objetoRetornado; | |
} | |
catch (JsonException) | |
{ | |
throw new CallRestException(AppResources.ResultadoPesquisaInvalidoText); | |
} | |
catch (Exception ex) | |
{ | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(T); | |
//throw new CallRestException(AppResources.ErroRequisicaoText); | |
} | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) | |
{ | |
throw new CallRestException(AppResources.ServicoNaoEncontradoText); | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError) | |
{ | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(T); | |
//throw new CallRestException(AppResources.ErroRequisicaoText); | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.NotFound) | |
{ | |
throw new CallRestException("NotFound"); | |
} | |
} | |
catch (TaskCanceledException tce) | |
{ | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(T); | |
//return await Post<T, R>(method, postObject, parameters); | |
} | |
catch (Exception expG) | |
{ | |
System.Diagnostics.Debug.WriteLine(expG); | |
System.Diagnostics.Debug.WriteLine($"Método {url} -- Exception {expG.Message}"); | |
return default(T); | |
} | |
} | |
} | |
catch (Exception expUsing) | |
{ | |
System.Diagnostics.Debug.WriteLine(expUsing); | |
} | |
return default(T); | |
} | |
public async Task<ListaPages<T>> GetPagina<T>(string method, Dictionary<string, string> parameters = null, int retryCount = 0) | |
{ | |
try | |
{ | |
var stopWatch = System.Diagnostics.Stopwatch.StartNew(); | |
var nativeHandler = new NativeMessageHandler(); | |
#if __Gzip__ | |
if (nativeHandler.SupportsAutomaticDecompression) | |
nativeHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; | |
#endif | |
if (await this.HasConn()) | |
using (var httpClient = new HttpClient(nativeHandler)) | |
//using (var httpClient = new HttpClient()) | |
{ | |
httpClient.BaseAddress = new Uri(Constants.BaseUrl); | |
HttpResponseMessage response; | |
if (parameters == null) | |
{ | |
if (method.Contains("?")) | |
method += $"&translateTo={App.CultureInfo.Split('-')[0]}"; | |
else | |
method += $"?translateTo={App.CultureInfo.Split('-')[0]}"; | |
System.Diagnostics.Debug.WriteLine(method); | |
response = await httpClient.GetAsync(method); | |
} | |
else | |
{ | |
method += @"?"; | |
string separator = ""; | |
foreach (var item in parameters) | |
{ | |
if (!string.IsNullOrEmpty(item.Value)) | |
method += $"{separator}{item.Key}={item.Value}"; | |
separator = "&"; | |
} | |
method += $"&translateTo={App.CultureInfo.Split('-')[0]}"; | |
System.Diagnostics.Debug.WriteLine(method); | |
response = await httpClient.GetAsync(method); | |
} | |
if (response.IsSuccessStatusCode) | |
{ | |
var responseContent = await response.Content.ReadAsStringAsync(); | |
if (string.IsNullOrEmpty(responseContent)) | |
throw new CallRestException(AppResources.NenhumResultadoText); | |
try | |
{ | |
var objetoRetornado = JsonConvert.DeserializeObject<ListaPages<T>>(responseContent); | |
System.Diagnostics.Debug.WriteLine($"Tempo da requisição: {stopWatch.Elapsed}"); | |
return objetoRetornado; | |
} | |
catch (JsonException) | |
{ | |
throw new CallRestException(AppResources.ResultadoPesquisaInvalidoText); | |
} | |
catch (Exception ex) | |
{ | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return null; | |
//throw new CallRestException(AppResources.ErroRequisicaoText); | |
} | |
} | |
else if (response.StatusCode == HttpStatusCode.RequestTimeout) | |
{ | |
if (retryCount <= 5) | |
{ | |
System.Diagnostics.Debug.WriteLine($"Método {method} -- TimeOut"); | |
return await GetPagina<T>(method, parameters, retryCount++); | |
} | |
return null; | |
} | |
else if (response.StatusCode == HttpStatusCode.GatewayTimeout) | |
{ | |
if (retryCount <= 5) | |
{ | |
System.Diagnostics.Debug.WriteLine($"Método {method} -- TimeOut"); | |
return await GetPagina<T>(method, parameters, retryCount++); | |
} | |
return null; | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) | |
{ | |
throw new CallRestException(AppResources.ServicoNaoEncontradoText); | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.NotFound) | |
{ | |
throw new CallRestException(AppResources.ServicoNaoEncontradoText); | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError) | |
{ | |
//throw new CallRestException(AppResources.ErroRequisicaoText); | |
//throw new CallRestException("Ocorreu um erro na sua requisição."); | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return null; | |
} | |
} | |
return null; | |
} | |
catch (TaskCanceledException tce) | |
{ | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return null; | |
//return await Post<T, R>(method, postObject, parameters); | |
} | |
catch (Exception excGet) | |
{ | |
System.Diagnostics.Debug.WriteLine(excGet.Message); | |
return null; | |
} | |
} | |
public async Task<R> Post<T, R>(string method, T postObject, Dictionary<string, object> parameters = null) | |
{ | |
var stopWatch = System.Diagnostics.Stopwatch.StartNew(); | |
var nativeHandler = new ModernHttpClient.NativeMessageHandler(); | |
#if __Gzip__ | |
if (nativeHandler.SupportsAutomaticDecompression) | |
nativeHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; | |
#endif | |
if (await this.HasConn()) | |
using (var httpClient = new HttpClient(nativeHandler)) | |
{ | |
httpClient.DefaultRequestHeaders.Accept.Clear(); | |
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
var sendObj = JsonConvert.SerializeObject(postObject); | |
var Content = new StringContent(sendObj, Encoding.UTF8, "application/json"); | |
try | |
{ | |
httpClient.Timeout = TimeSpan.FromMinutes(3); | |
System.Diagnostics.Debug.WriteLine(Constants.BaseUrl + method + "/"); | |
var response = await httpClient.PostAsync(Constants.BaseUrl + method + "/", Content); | |
if (response.IsSuccessStatusCode) | |
{ | |
var responseContent = await response.Content.ReadAsStringAsync(); | |
if (string.IsNullOrEmpty(responseContent)) | |
throw new CallRestException(AppResources.NenhumResultadoText); | |
try | |
{ | |
System.Diagnostics.Debug.WriteLine($" Chamada Finalizada as {DateTime.Now}: {Constants.BaseUrl} {method} /"); | |
System.Diagnostics.Debug.WriteLine($"Tempo da requisição: {stopWatch.Elapsed}"); | |
var objetoRetornado = JsonConvert.DeserializeObject<R>(responseContent); | |
return objetoRetornado; | |
} | |
catch (JsonException ex1) | |
{ | |
System.Diagnostics.Debug.WriteLine(ex1.Message); | |
throw new CallRestException(AppResources.ResultadoPesquisaInvalidoText); | |
} | |
catch (Exception ex2) | |
{ | |
//throw new CallRestException(AppResources.ErroRequisicaoText); | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(R); | |
} | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) | |
{ | |
try | |
{ | |
var errorContent = await response.Content.ReadAsStringAsync(); | |
System.Diagnostics.Debug.WriteLine(errorContent); | |
var erroRetornado = JsonConvert.DeserializeObject<ErrorServer>(errorContent); | |
System.Diagnostics.Debug.WriteLine(erroRetornado.Message); | |
//if (erroRetornado.Message.Count > 0) | |
//{ | |
// var exString = ""; | |
// foreach (var erroString in erroRetornado.Message) | |
// { | |
// exString += $"{erroString.Key}: {string.Join(",", erroString.Value)} \n"; | |
// } | |
// throw new CallRestException(exString); | |
//} | |
if (erroRetornado != null) | |
throw new CallRestException(erroRetornado.Message); | |
throw new CallRestException(errorContent); | |
} | |
catch (Exception) | |
{ | |
throw new CallRestException(AppResources.ServicoNaoEncontradoText); | |
} | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError) | |
{ | |
var responseError = await response.Content.ReadAsStringAsync(); | |
if (responseError.Contains("CPF")) | |
{ | |
throw new Exception("CPF"); | |
} | |
if (responseError.Contains("EMAIL")) | |
{ | |
throw new Exception("EMAIL"); | |
} | |
if (responseError.Contains("TELEFONE")) | |
{ | |
throw new Exception("TELEFONE"); | |
} | |
//throw new CallRestException(AppResources.ErroRequisicaoText); | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(R); | |
} | |
} | |
catch (TaskCanceledException tce) | |
{ | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(R); | |
//return await Post<T, R>(method, postObject, parameters); | |
} | |
catch (HttpRequestException ex3) | |
{ | |
//throw new CallRestException(AppResources.ErroRequisicaoText, ex3); | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(R); | |
} | |
catch (Exception ex4) | |
{ | |
throw new CallRestException(AppResources.FalhaCriticaText, ex4); | |
} | |
} | |
return default(R); | |
} | |
public async Task<T> Put<T>(string method, string id, T putObject, Dictionary<string, object> parameters = null) | |
{ | |
var stopWatch = System.Diagnostics.Stopwatch.StartNew(); | |
var nativeHandler = new NativeMessageHandler(); | |
#if __Gzip__ | |
if (nativeHandler.SupportsAutomaticDecompression) | |
nativeHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; | |
#endif | |
if (await this.HasConn()) | |
using (var httpClient = new HttpClient(nativeHandler)) | |
//using (var httpClient = new HttpClient()) | |
{ | |
//httpClient.BaseAddress = new Uri(Constants.BaseUrl); | |
httpClient.DefaultRequestHeaders.Accept.Clear(); | |
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
var sendObj = JsonConvert.SerializeObject(putObject); | |
var Content = new StringContent(sendObj, Encoding.UTF8, "application/json"); | |
try | |
{ | |
httpClient.Timeout = TimeSpan.FromMinutes(3); | |
System.Diagnostics.Debug.WriteLine(Constants.BaseUrl + method + "/"); | |
var response = await httpClient.PutAsync(Constants.BaseUrl + method + "/" + id + "/", Content); | |
if (response.IsSuccessStatusCode) | |
{ | |
var responseContent = await response.Content.ReadAsStringAsync(); | |
if (string.IsNullOrEmpty(responseContent)) | |
throw new CallRestException(AppResources.NenhumResultadoText); | |
try | |
{ | |
System.Diagnostics.Debug.WriteLine($" Chamada Finalizada as {DateTime.Now}: {Constants.BaseUrl} {method} /"); | |
System.Diagnostics.Debug.WriteLine($"Tempo da requisição: {stopWatch.Elapsed}"); | |
var objetoRetornado = JsonConvert.DeserializeObject<T>(responseContent); | |
return objetoRetornado; | |
} | |
catch (JsonException ex1) | |
{ | |
System.Diagnostics.Debug.WriteLine(ex1.Message); | |
throw new CallRestException(AppResources.ResultadoPesquisaInvalidoText); | |
} | |
catch (Exception ex2) | |
{ | |
//throw new CallRestException(AppResources.ErroRequisicaoText); | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(T); | |
} | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) | |
{ | |
throw new CallRestException(AppResources.ServicoNaoEncontradoText); | |
} | |
else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError) | |
{ | |
//throw new CallRestException(AppResources.ErroRequisicaoText); | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(T); | |
} | |
} | |
catch (TaskCanceledException tce) | |
{ | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(T); | |
//return await Post<T, R>(method, postObject, parameters); | |
} | |
catch (HttpRequestException ex3) | |
{ | |
//throw new CallRestException(AppResources.ErroRequisicaoText); | |
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => | |
{ | |
}); | |
return default(T); | |
} | |
catch (Exception ex4) | |
{ | |
System.Diagnostics.Debug.WriteLine(ex4.Message); | |
throw new CallRestException(AppResources.FalhaCriticaText); | |
} | |
} | |
return default(T); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment