Add following AppUtils.cs class in your project.
Note: methods of this class depends on "ResponseModel" class, so please add this class in your project as well. I have pasted ResponseModel class in the bottom.
Note: methods of this class depends on "ResponseModel" class, so please add this class in your project as well. I have pasted ResponseModel class in the bottom.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace test
{
public class AppUtils
{
#if DEBUG
static string BaseAddress = ConfigurationManager.AppSettings["ApiBaseUri"];
#else
static string BaseAddress = ConfigurationManager.AppSettings["ApiBaseUriLive"];
#endif
public static async Task<ResponseModel<T>> MakePOSTAPICall<T, JsonBodyData>(string apiUrl, JsonBodyData json, string accessToken = "", string timeZone = "")
{
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(BaseAddress);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (!string.IsNullOrWhiteSpace(timeZone))
{
client.DefaultRequestHeaders.Add("HTTP_X_TIMEZONE", timeZone);
}
if (!string.IsNullOrWhiteSpace(accessToken))
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
}
//send request
var content = new StringContent(json != null ? JsonConvert.SerializeObject(json) : "", Encoding.UTF8, "application/json");
var result = await client.PostAsync(apiUrl, content);
if (result.IsSuccessStatusCode)
{
return new ResponseModel<T>
{
Status = true,
Data = JsonConvert.DeserializeObject<T>(await result.Content.ReadAsStringAsync())
};
}
else
{
string errorJson = await new StreamReader(await result.Content.ReadAsStreamAsync()).ReadToEndAsync();
ModelStateErrors errors = JsonConvert.DeserializeObject<ModelStateErrors>(errorJson);
return new ResponseModel<T>
{
Status = false,
Message = errors.ModelState?.Errors?[0] ?? errors.Message
};
}
}
}
public static async Task<ResponseModel<T>> MakeGetAPICall<T>(string apiUrl, string accessToken = "", string timeZone = "")
{
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(BaseAddress);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (!string.IsNullOrWhiteSpace(timeZone))
{
client.DefaultRequestHeaders.Add("HTTP_X_TIMEZONE", timeZone);
}
if (!string.IsNullOrWhiteSpace(accessToken))
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
}
//send request
var result = await client.GetAsync(apiUrl);
if (result.IsSuccessStatusCode)
{
return new ResponseModel<T>
{
Status = true,
Data = JsonConvert.DeserializeObject<T>(await result.Content.ReadAsStringAsync())
};
}
else
{
string errorJson = await new StreamReader(await result.Content.ReadAsStreamAsync()).ReadToEndAsync();
ModelStateErrors errors = JsonConvert.DeserializeObject<ModelStateErrors>(errorJson);
return new ResponseModel<T>
{
Status = false,
Message = errors.ModelState?.Errors?[0] ?? errors.Message
};
}
}
}
}
}
public class ResponseModel<T>
{
public bool Status { get; set; }
public string Message { get; set; }
public T Data { get; set; }
}
No comments:
Post a Comment