Exemplo n.º 1
0
        /// <summary>
        /// Get API call response.
        /// </summary>
        /// <param name="request">The <see cref="HttpRequestMessage"/>.</param>
        /// <returns>API Call response.</returns>
        protected virtual async Task <HttpResponseMessage> GetResponseAsync(HttpRequestMessage request)
        {
            var response = await Http.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.Unauthorized:
                    await JsRuntime.RemoveUserTokenAsync();

                    var token = await GetToken();

                    if (token == null)
                    {
                        NavigationManager.NavigateTo("login");
                        return(response);
                    }

                    return(await GetResponseAsync(request));

                case HttpStatusCode.BadRequest:
                    break;

                default:
                    throw new RestApiDataSourceException(GetExceptionMessage(request.RequestUri, request,
                                                                             response));
                }
            }

            var responseContent = await response.Content.ReadAsStringAsync();

            if (responseContent == "[]")
            {
                response.Content = WrapBody(string.Empty);
            }

            return(response);
        }