Example #1
0
        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();
            }
        }
Example #2
0
        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();
            }
        }
Example #3
0
        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();
            }
        }