public virtual VM Get(Int64 id) { using (Httpclient = new HttpClient()) { Httpclient.BaseAddress = url; Httpclient.DefaultRequestHeaders.Clear(); Httpclient.DefaultRequestHeaders.Add("Accept", "application/json"); if (!String.IsNullOrEmpty(ApplicationsVariables.Token)) { Httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApplicationsVariables.Token); } var response = Task.Run(() => Httpclient.GetAsync(new Uri(url + "/" + id))).Result; if (response.IsSuccessStatusCode) { return(this.Fill(response.Content.ReadAsStringAsync().Result)); } else { JsonErrorHandler.HandleError(response); } throw new Exception(); } }
public virtual void Delete(VM model) { using (Httpclient = new HttpClient()) { Httpclient.BaseAddress = url; Httpclient.DefaultRequestHeaders.Clear(); Httpclient.DefaultRequestHeaders.Add("Accept", "application/json"); if (!String.IsNullOrEmpty(ApplicationsVariables.Token)) { Httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApplicationsVariables.Token); } string postBody = JsonConvert.SerializeObject(model); Task taskDownload = Httpclient.DeleteAsync((url + "/" + model.Id)) .ContinueWith(task => { if (task.Status == TaskStatus.RanToCompletion) { var response = task.Result; if (response.IsSuccessStatusCode) { return; } else { JsonErrorHandler.HandleError(response); } } }); taskDownload.Wait(); } }
private void FillExceptions() { Handlers = new List <BaseExceptionClientHandler>(); Handlers.Add(SecurityExceptionClientHandler.GetInstance()); Handlers.Add(JsonErrorHandler.GetInstance()); //Handlers.Add(AggregateExceptionHandler.GetInstance()); BaseExceptionClientHandler _handler; for (int i = 0; i <= Handlers.Count(); i++) { _handler = Handlers.ElementAt(i); if (i + 1 == Handlers.Count()) { break; } _handler.Mychainhandler = Handlers.ElementAt(i + 1); } }
public virtual async Task <List <VM> > GetAll(string filters) { using (Httpclient = new HttpClient()) { Httpclient.BaseAddress = url; Httpclient.DefaultRequestHeaders.Clear(); Httpclient.DefaultRequestHeaders.Add("Accept", "application/json"); if (!String.IsNullOrEmpty(ApplicationsVariables.Token)) { Httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApplicationsVariables.Token); } var response = Task.Run(() => Httpclient.GetAsync(new Uri(url + filters))).Result; if (response.IsSuccessStatusCode) { // get the self link of the root resource //var selfUri = response.Resource.Links["self"].First().Href; //var findUri = response.Resource.Links["find"].First().Href; //var next = response.Resource.Links.ContainsKey("next") ? // response.Resource.Links["next"].FirstOrDefault().Href : new Uri("Http://N/A"); //var prev = response.Resource.Links.ContainsKey("prev") ? // response.Resource.Links["prev"].FirstOrDefault().Href : new Uri("Http://N/A"); //var first = response.Resource.Links.ContainsKey("first") ? // response.Resource.Links["first"].FirstOrDefault().Href : new Uri("Http://N/A"); //var last = response.Resource.Links.ContainsKey("last") ? // response.Resource.Links["last"].FirstOrDefault().Href : new Uri("Http://N/A"); var listdto = FillCollection(response.Content.ReadAsStringAsync().Result); return(listdto); } else { JsonErrorHandler.HandleError(response); } throw new Exception(); } }
/// <summary> /// Creates a new instance of an ErrorController object. /// </summary> /// <param name="errorHandler"></param> public ErrorController(JsonErrorHandler errorHandler) { _errorHandler = errorHandler; }
public ApiExceptionFilter(JsonErrorHandler jsonErrorHandler, ILogger <ApiExceptionFilter> logger) { this.jsonErrorHandler = jsonErrorHandler; this.logger = logger; }
private void HandleJsonError(Exception e) { JsonErrorHandler?.Invoke(e); ErrorHandler?.Invoke(e); RequestCompletedHandler?.Invoke(); }