Exemplo n.º 1
0
        /// <summary>
        /// Parse a response and return occured errors
        /// </summary>
        public RequestReport HandleResponse(ResponseEx response)
        {
            RequestReport result = null;

            // get the HTTP status code
            var status = response.StatusHttp;

            // if success or 'no content'
            if ((status == HttpStatusCode.OK) ||
                (status == HttpStatusCode.Created) ||
                (status == HttpStatusCode.NoContent))
            {
                // then do nothing, it's not an error
            }
            // if any other case
            else
            {
                // create a simple error report
                result = new RequestReport(status)
                {
                    Code    = "Api.Client.Response.Failure",
                    Message = status.ToString()
                };
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parse a response and return occured errors
        /// </summary>
        public RequestReport HandleResponse <TContent>(out TContent content, ResponseEx response)
            where TContent : class
        {
            RequestReport result = null;

            content = null;

            // if we have a response
            if (response != null)
            {
                // get the HTTP status code
                var status = response.StatusHttp;

                // if we have a body
                if (response.HasBody)
                {
                    // if succeeded
                    if ((status == HttpStatusCode.OK) ||
                        (status == HttpStatusCode.Created))
                    {
                        // deserialize
                        content = response.GetBodyAsJson <TContent>();
                        // if parsing failed
                        if (content == null)
                        {
                            // wrap in an error report
                            result = new RequestReport(status)
                            {
                                Code    = "Api.Client.Response.ParsingFailed.Content",
                                Message = string.Format("Could not parse response body after a success [{0}]", response.GetBodyAsString())
                            };
                        }
                    }
                    // if failed
                    else
                    {
                        // deserialize an error
                        result = response.GetBodyAsJson <RequestReport>();
                        // if parsing succeeded
                        if (result != null)
                        {
                            // update the status
                            result.StatusCode = (int)status;
                        }
                        // if failed
                        else
                        {
                            // wrap in an error report
                            result = new RequestReport(status)
                            {
                                Code    = "Api.Client.Response.ParsingFailed.Error",
                                Message = string.Format("Could not parse response body after a failure [{0}]", response.GetBodyAsString())
                            };
                        }
                    }
                }
                // if no body in the response
                else
                {
                    // if it's an error
                    if (status >= HttpStatusCode.BadRequest)
                    {
                        // create a simple error report
                        result = new RequestReport(status)
                        {
                            Code    = "Api.Client.Response.Failure",
                            Message = status.ToString()
                        };
                    }
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a request
        /// </summary>
        protected RequestReport CreateRequest(out RequestFluent request, string endpoint)
        {
            RequestReport result = null;

            request = null;

            // if we have a valid root uri
            if (UriRoot != null)
            {
                // if we have a given url
                if (!string.IsNullOrEmpty(endpoint))
                {
                    try
                    {
                        // combine the urls
                        endpoint = new Uri(UriRoot, new Uri(endpoint, UriKind.Relative)).ToString();

                        // if we got a provider
                        if (this._provider != null)
                        {
                            request = this._provider.CreateRequest(endpoint);
                        }
                        // if no provided
                        else
                        {
                            request = RequestFluent.Create(endpoint);
                        }
                    }
                    catch (Exception exc)
                    {
                        // error
                        result = new RequestReport
                        {
                            Code      = "Api.Client.CreateRequest.Exception",
                            Message   = "ProAbono client library internal error - " + exc.Message,
                            Exception = exc.ToString()
                        };
                    }
                }
                // if no url
                else
                {
                    // error
                    result = new RequestReport
                    {
                        Code    = "Api.Client.CreateRequest.NoEndpoint",
                        Message = "ProAbono client library internal error suspected - Endpoint is missing"
                    };
                }
            }
            // if no root URI
            else
            {
                // error
                result = new RequestReport
                {
                    Code    = "Api.Client.CreateRequest.NoRootUrl",
                    Message = "Configuration error suspected - ProAbono API root url not found"
                };
            }
            return(result);
        }