Exemplo n.º 1
0
        public async Task <WebApiResult <Rtype> > PutAsync(string apiUpdateMethod, Rtype putObject)
        {
            Rtype resultObject = null;
            HttpResponseMessage  response;
            WebApiResult <Rtype> result = new WebApiResult <Rtype>();

            result.ErrorMessages = new List <string>();
            try
            {
                if (putObject is HttpContent)
                {
                    response = await httpClient.PutAsync(apiUpdateMethod, putObject as HttpContent).ConfigureAwait(false);
                }
                else
                {
                    response = await httpClient.PutAsJsonAsync(apiUpdateMethod, putObject).ConfigureAwait(false);
                }
                response.EnsureSuccessStatusCode();
                if (response.IsSuccessStatusCode)
                {
                    await response.Content.ReadAsAsync <Rtype>().ContinueWith(x =>
                    {
                        if (!x.IsFaulted)
                        {
                            resultObject = x.Result;
                            result.Data  = resultObject;
                        }
                        else
                        {
                            result.ErrorMessages.Add(x.Exception.InnerException.Message);
                            result.HasError = true;
                        }
                    });
                }
                else
                {
                    result.ErrorMessages.Add(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase));
                    result.HasError = true;
                }
            }
            catch (Newtonsoft.Json.JsonException jsonEx)
            {
                result.ErrorMessages.Add(jsonEx.Message);
                result.HasError = true;
            }
            catch (HttpRequestException httpEx)
            {
                result.ErrorMessages.Add(httpEx.Message);
                result.HasError = true;
            }
            catch (Exception ex)
            {
                result.ErrorMessages.Add(ex.Message);
                result.HasError = true;
            }
            return(result);
        }
Exemplo n.º 2
0
        public async Task <WebApiResult <Rtype> > PostAsync(string apiPostMethod, Rtype postObject)
        {
            Rtype resultObject = null;
            HttpResponseMessage  response;
            WebApiResult <Rtype> result = new WebApiResult <Rtype>();

            result.ErrorMessages = new List <string>();
            try
            {
                bool isHttpcontent = postObject is HttpContent;
                if (isHttpcontent)
                {
                    response = await httpClient.PostAsync(apiPostMethod, postObject as HttpContent).ConfigureAwait(false);
                }
                else
                {
                    response = await httpClient.PostAsJsonAsync(apiPostMethod, postObject).ConfigureAwait(false);
                }
                if (!response.IsSuccessStatusCode)
                {
                    result.ErrorMessages.Add(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase));
                    result.HasError = true;
                }
                if (!isHttpcontent)
                {
                    await response.Content.ReadAsAsync <Rtype>().ContinueWith(x =>
                    {
                        if (!x.IsFaulted)
                        {
                            resultObject = x.Result;
                            result.Data  = resultObject;
                        }
                        else
                        {
                            result.ErrorMessages.Add(x.Exception.InnerException.Message);
                            result.HasError = true;
                        }
                    });
                }
                else
                {
                    resultObject = response.Content as Rtype;
                }
            }
            catch (Exception ex)
            {
                result.ErrorMessages.Add(ex.Message);
                result.HasError = true;
            }
            return(result);
        }
Exemplo n.º 3
0
        public async Task <WebApiResult <IEnumerable <Rtype> > > GetManyAsync(string apiMethod)
        {
            HttpResponseMessage response;
            WebApiResult <IEnumerable <Rtype> > result = new WebApiResult <IEnumerable <Rtype> >();

            result.ErrorMessages = new List <string>();
            try
            {
                response = await httpClient.GetAsync(apiMethod).ConfigureAwait(false);

                response.EnsureSuccessStatusCode();
                if (response.IsSuccessStatusCode)
                {
                    await response.Content.ReadAsAsync <IEnumerable <Rtype> >().ContinueWith(x =>
                    {
                        if (!x.IsFaulted)
                        {
                            result.Data = x.Result;
                        }
                        else
                        {
                            result.ErrorMessages.Add(x.Exception.InnerException.Message);
                            result.HasError = true;
                        }
                    });
                }
                else
                {
                    result.ErrorMessages.Add(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase));
                    result.HasError = true;
                }
            }
            catch (Newtonsoft.Json.JsonException jsonEx)
            {
                result.ErrorMessages.Add(jsonEx.Message);
                result.HasError = true;
            }
            catch (HttpRequestException httpEx)
            {
                result.ErrorMessages.Add(httpEx.Message);
                result.HasError = true;
            }
            catch (Exception ex)
            {
                result.ErrorMessages.Add(ex.Message);
                result.HasError = true;
            }
            return(result);
        }