Exemplo n.º 1
0
        public static async Task <HalRemoteException> Create(HttpResponseMessage response)
        {
            var uri = response.RequestMessage.RequestUri;

            try
            {
                //Try to read the response as a string
                var responseString = await response.Content.ReadAsStringAsync();

                if (responseString == null)
                {
                    return(new HalRemoteException(uri, $"An unknown server error occured with error code: {(int)response.StatusCode}", response.StatusCode, responseString, null));
                }
                try
                {
                    //Try to read the message as an understandable error.
                    var responseData = JObject.Parse(responseString);
                    var error        = responseData["message"]?.Value <String>() ?? $"An unknown server error occured with error code: {(int)response.StatusCode}";
                    var exception    = new HalRemoteException(uri, error, response.StatusCode, responseString, responseData);
                    return(exception);
                }
                catch (Exception)
                {
                    return(new HalRemoteException(uri, $"An unknown server error occured with error code: {(int)response.StatusCode}", response.StatusCode, responseString, null));
                }
            }
            catch (Exception)
            {
                return(new HalRemoteException(uri, $"An unknown server error occured with error code: {(int)response.StatusCode}", response.StatusCode, null, null));
            }
        }
        private async Task HandleResponse(HttpResponseMessage response)
        {
            StatusCode = response.StatusCode;
            if ((int)StatusCode > 299)
            {
                throw await HalRemoteException.Create(response);
            }
            var responseString = await response.Content.ReadAsStringAsync();

            var responseData = JObject.Parse(responseString);

            if (responseData != null)
            {
                data = responseData.Root;
            }
            links  = responseData["_links"];
            embeds = responseData["_embedded"];
        }