コード例 #1
0
ファイル: APIClient.cs プロジェクト: faacumoral/Worter
        public async Task <Tresult> Send <Tresult>(Request request)
            where Tresult : IBaseErrorResult, new()
        {
            var jwtToken = await sessionStorage.GetStringAsync(Constants.JWT_TOKEN);

            // TODO add expiration validation too
            if (string.IsNullOrEmpty(jwtToken) && request.CheckAuth)
            {
                // TODO save state
                navigationManager.GoLogin();
                return(new Tresult()
                {
                    ResultError = ErrorResult.Build("JWT cannot be blank"),
                    Success = false,
                    ResultOperation = ResultOperation.Unauthorized,
                });
            }

            var requestMessage = new HttpRequestMessage()
            {
                Method     = new HttpMethod(request.Method),
                RequestUri = new Uri(client.BaseAddress.OriginalString + request.Url)
            };

            if (request.Parameter != null)
            {
                requestMessage.Content = JsonContent.Create(request.Parameter, request.Parameter.GetType());
            }

            if (request.CheckAuth)
            {
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
            }

            var response = await client.SendAsync(requestMessage);

            // var responseStatusCode = response.StatusCode;
            return(await response.Content.ReadFromJsonAsync <Tresult>());
        }