Example #1
0
        public static ResponseObject TraiteResponse(HttpResponseMessage response, IDaoConvertible toObject, bool isArray)
        {
            ResponseObject contentResponse = new ResponseObject();
            //try
            //{
            String stringResult = response.Content.ReadAsStringAsync().Result;

            if (response.IsSuccessStatusCode)
            {
                if (stringResult != null)
                {
                    if (isArray)
                    {
                        var result = JArray.Parse(stringResult);
                        contentResponse.Content = result.Children().Select(t => toObject.ToObjectDao(t).ToObjectModel()).ToList();
                    }
                    else
                    {
                        var result = JObject.Parse(stringResult);
                        contentResponse.Content = toObject.ToObjectDao(result).ToObjectModel();
                    }
                }

                contentResponse.Success = true;
            }

            else if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                var rawError = JArray.Parse(stringResult);
                contentResponse.Content = rawError.Children().Select(e => new Error()
                {
                    Code        = e["code"].Value <String>(),
                    Description = e["description"].Value <String>()
                }).ToList();
                contentResponse.Success = false;
            }
            else if (response.StatusCode == HttpStatusCode.GatewayTimeout)
            {
                contentResponse.Content = new List <Error>()
                {
                    new Error()
                    {
                        Code = "ErrorTimeout", Description = "Erreur 504 Timeout"
                    }
                };
                contentResponse.Success = false;
            }
            else if (response.StatusCode == HttpStatusCode.InternalServerError)
            {
                contentResponse.Content = new List <Error>()
                {
                    new Error()
                    {
                        Code = "ErrorServer", Description = "Erreur 500 " + response.ReasonPhrase
                    }
                };
                contentResponse.Success = false;
            }
            else
            {
                contentResponse.Content = new List <Error>()
                {
                    new Error()
                    {
                        Code = "ErrorUnknow", Description = "Erreur inconnue (" + response.StatusCode + ")" + response.ReasonPhrase
                    }
                };
                contentResponse.Success = false;
            }
            // }
            //catch(Exception e)
            //{
            //   contentResponse.Content = new List<Error>() { new Error() { Code = "ErrorException", Description = "Une exeption est apparue ! ("+e.GetType()+") "+e.Message} };
            //  contentResponse.Success = false;
            //}
            return(contentResponse);
        }
Example #2
0
        public static Object TraiteResponse(HttpResponseMessage response, IDaoConvertible toObject, bool isArray)
        {
            String stringResult = response.Content.ReadAsStringAsync().Result;

            if (response.IsSuccessStatusCode)
            {
                if (stringResult != null && toObject != null)
                {
                    if (isArray)
                    {
                        var result = JArray.Parse(stringResult);
                        return(result.Children().Select(t => toObject.ToObjectDao(t).ToObjectModel()).ToList());
                    }
                    else
                    {
                        var result = JObject.Parse(stringResult);
                        return(toObject.ToObjectDao(result).ToObjectModel());
                    }
                }
                else
                {
                    return(null);
                }
            }
            else if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new Model.ModelException.NotConnectedException();
            }
            else if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                try
                {
                    var rawError = JArray.Parse(stringResult);
                    IEnumerable <Error> errors = rawError.Children().Select(e => new Error()
                    {
                        Code        = e["code"].Value <String>(),
                        Description = e["description"].Value <String>()
                    });
                    throw new Model.ModelException.BadRequestException("Bad request", errors);
                }
                catch (JsonReaderException)
                {
                    IEnumerable <Error> errors = new List <Error>()
                    {
                        new Error()
                        {
                            Code        = "442",
                            Description = "Donnée non valide"
                        }
                    };

                    throw new Model.ModelException.BadRequestException("Bad request", errors);
                }
            }
            else if (response.StatusCode == HttpStatusCode.GatewayTimeout)
            {
                throw new TimeoutException("Le serveur n'a pas renvoyer de réponse dans le temp imparti");
            }
            else if (response.StatusCode == HttpStatusCode.InternalServerError)
            {
                throw new Model.ModelException.ServerException("Erreur sur le serveur", "Erreur 500 " + response.ReasonPhrase);
            }
            else
            {
                throw new Model.ModelException.ServiceException("Erreur inconnue (" + response.StatusCode + ")", "Erreur inconnue (" + response.StatusCode + ")" + response.ReasonPhrase);
            }
        }
Example #3
0
        public static Object HandleResponse(HttpResponseMessage response, IDaoConvertible toObject, bool isArray)
        {
            String stringResult = response.Content.ReadAsStringAsync().Result;

            if (response.IsSuccessStatusCode)
            {
                if (stringResult != null && toObject != null)
                {
                    if (isArray)
                    {
                        var result = JArray.Parse(stringResult);
                        return(result.Children().Select(t => toObject.ToObjectDao(t).ToObjectModel()).ToList());
                    }
                    else
                    {
                        var result = JObject.Parse(stringResult);
                        return(toObject.ToObjectDao(result).ToObjectModel());
                    }
                }
                else
                {
                    return(null);
                }
            }

            else
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.BadRequest:
                    try
                    {
                        var rawError = JArray.Parse(stringResult);
                        IEnumerable <Error> errors = rawError.Children().Select(e => new Error()
                        {
                            Code        = e["code"].Value <String>(),
                            Description = e["description"].Value <String>()
                        });
                        throw new Model.ModelException.BadRequestException("Bad request", errors);
                    }
                    catch (JsonReaderException)
                    {
                        IEnumerable <Error> errors = new List <Error>()
                        {
                            new Error()
                            {
                                Code        = "442",
                                Description = "Data cannot be processed"
                            }
                        };

                        throw new Model.ModelException.BadRequestException("Bad request", errors);
                    }

                case HttpStatusCode.GatewayTimeout:
                    throw new TimeoutException("Server not responding in time");

                case HttpStatusCode.InternalServerError:
                    throw new Model.ModelException.ServerException("Server Error", "500 " + response.ReasonPhrase);;

                default:
                    throw new Model.ModelException.ServiceException("Unhandled Error", "Unhandled Error (" + response.StatusCode + ")" + response.ReasonPhrase);
                }
            }
        }