Skip to content

Instantly share code, notes, and snippets.

@aybarsyalcin
Created December 13, 2017 08:14
Show Gist options
  • Save aybarsyalcin/7a5f8dccb90443ce263a4c952fadc2d0 to your computer and use it in GitHub Desktop.
Save aybarsyalcin/7a5f8dccb90443ce263a4c952fadc2d0 to your computer and use it in GitHub Desktop.
When start a new project, I need to BaseClient snippet
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace yournamespaces
{
public class BaseClient
{
public BaseClient()
{
}
public static async Task<T> GET<T>(string uri, bool isHudShow = true)
{
try
{
//if (isHudShow)
//DependencyService.Get<IHud>().Show();
HttpClientHandler handler = new HttpClientHandler();
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri(Constants.RestUrl);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.MaxResponseContentBufferSize = 256000;
Debug.WriteLine(uri);
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//client.DefaultRequestHeaders.Add("Authorization", "Bearer " + SharedSystem.Current.Token);
HttpResponseMessage response = await client.GetAsync(uri);
if (response.StatusCode != HttpStatusCode.OK)
return default(T);
string jsonString = await response.Content.ReadAsStringAsync();
T responseData = JsonConvert.DeserializeObject<T>(jsonString);
//DependencyService.Get<IHud>().Dismiss();
return responseData;
}
catch (Exception e)
{
//DependencyService.Get<IHud>().Dismiss();
return default(T);
}
}
public static async Task<T> POST<T>(string uri, String content)
{
try
{
using (HttpClient client = new HttpClient())
{
//DependencyService.Get<IHud>().Show();
HttpClientHandler handler = new HttpClientHandler();
client.BaseAddress = new Uri(Constants.RestUrl);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.MaxResponseContentBufferSize = 256000;
var stringContent = new StringContent(string.Empty);
if (!string.IsNullOrEmpty(content))
{
stringContent = new StringContent(content, System.Text.Encoding.UTF8, "application/json");
}
Debug.WriteLine(uri);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//client.DefaultRequestHeaders.Add("Authorization", "Bearer " + SharedSystem.Current.Token);
HttpResponseMessage response = await client.PostAsync(uri, stringContent);
string jsonString = await response.Content.ReadAsStringAsync();
if (response.StatusCode != HttpStatusCode.OK)
return default(T);
//DependencyService.Get<IHud>().Dismiss();
T responseData = JsonConvert.DeserializeObject<T>(jsonString);
return responseData;
}
}
catch (Exception e)
{
//DependencyService.Get<IHud>().Dismiss();
return default(T);
}
}
public static async Task<T> POST<T>(string uriParams, List<KeyValuePair<string, string>> body)
{
try
{
//DependencyService.Get<IHud>().Show();
HttpClientHandler handler = new HttpClientHandler();
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri(Constants.RestUrl);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.MaxResponseContentBufferSize = 256000;
var content = new FormUrlEncodedContent(body);
Uri uri = new Uri(client.BaseAddress + uriParams);
System.Diagnostics.Debug.WriteLine(uri.AbsolutePath);
HttpResponseMessage response = await client.PostAsync(uri, content);
string jsonString = await response.Content.ReadAsStringAsync();
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
return await POST<T>(uriParams, body);
}
T responseData = JsonConvert.DeserializeObject<T>(jsonString);
//DependencyService.Get<IHud>().Dismiss();
return responseData;
}
catch (Exception e)
{
//DependencyService.Get<IHud>().Dismiss();
return default(T);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment