I need advice for this implementation that I made for a service access portable class library referenced to Xamarin.Android and Xamarin.Ios projects. For now, it works fine, but I have doubts about its performance or if it would crash on simultaneous calls.
public class HttpClientBase : HttpClient
{
private static readonly HttpClientBase instance = new HttpClientBase();
static HttpClientBase() { }
private HttpClientBase() : base()
{
Timeout = TimeSpan.FromMilliseconds(15000);
MaxResponseContentBufferSize = 256000;
}
public static HttpClientBase Instance
{
get
{
return instance;
}
}
public async Task<List<T>> GetListItems<T>(string Url)
{
var uri = new Uri(string.Format(Constantes.BaseUrlApi, Url));
var response = await GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var Items = JsonConvert.DeserializeObject<List<T>>(content);
return Items;
}
throw new Exception(response.ReasonPhrase);
}
public async Task<T> GetItem<T>(string Url)
{
var uri = new Uri(string.Format(Constantes.BaseUrlApi, Url));
var response = await GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var Item = JsonConvert.DeserializeObject<T>(content);
return Item;
}
throw new Exception(response.ReasonPhrase);
}
public async Task PostItem<T>(T item, string url)
{
var uri = new Uri(string.Format(Constantes.BaseUrlApi, ""));
var json = JsonConvert.SerializeObject(item);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
response = await PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
return;
}
throw new Exception(response.ReasonPhrase);
}
}