Example #1
0
        /// <summary>
        /// Сохранение решения задачи
        /// </summary>
        /// <param name="solutionStatus">Статус решения</param>
        /// <param name="handleSolutionStream">Обработчик потока решения</param>
        /// <exception cref="RetryException">ри возникновении данной ошибки возможно повторения выхова через некоторое время</exception>
        /// <exception cref="HttpException">Если при сохранении решения возникнет сетевая ошибка</exception>
        public void Post(SolutionStatus solutionStatus, Action <Stream> handleSolutionStream)
        {
            using (var client = new HttpClient())
            {
                if (!string.IsNullOrEmpty(_authorizationHeader))
                {
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", _authorizationHeader);
                }
                client.BaseAddress = new Uri(_baseUrl);
                using (var stream = new MemoryStream())
                {
                    handleSolutionStream(stream);
                    //stream.Get
                    using (var multipartFormDataContent = new MultipartFormDataContent())
                    {
                        multipartFormDataContent.Add(new StringContent(solutionStatus.ToString()), '"' + "SolutionStatus" + '"');
                        multipartFormDataContent.Add(new ByteArrayContent(stream.ToArray()), '"' + "Solution" + '"', '"' + $"solution" + '"');
                        try
                        {
                            var res = client.PutAsync("api/problem/solution", multipartFormDataContent).GetAwaiter().GetResult();
                            if (res.IsSuccessStatusCode)
                            {
                                return;
                            }
                            if (!res.IsSuccessStatusCode && !_statusCodesToThrow.Contains((int)res.StatusCode))
                            {
                                throw new RetryException($"Invalid status code for sending request to {_baseUrl} with authorization{_authorizationHeader}",
                                                         new HttpException($"Invalid status code for sending request to {_baseUrl} with authorization{_authorizationHeader}",
                                                                           (int)res.StatusCode,
                                                                           res.ReasonPhrase,
                                                                           res.Content.ReadAsStringAsync().GetAwaiter().GetResult(),
                                                                           res.Headers.ToDictionary(x => x.Key, x => x.Value)));
                            }

                            throw new HttpException($"Invalid status code for sending request to {_baseUrl} with authorization{_authorizationHeader}",
                                                    (int)res.StatusCode,
                                                    res.ReasonPhrase,
                                                    res.Content.ReadAsStringAsync().GetAwaiter().GetResult(),
                                                    res.Headers.ToDictionary(x => x.Key, x => x.Value));
                        }
                        catch (HttpRequestException e)
                        {
                            throw new RetryException($"Invalid status code for sending request to {_baseUrl} with authorization{_authorizationHeader}", e);
                        }
                    }
                }
            }
        }
 private static void WriteStatus(this Utf8JsonWriter jsonTextWriter, SolutionStatus status) =>
 jsonTextWriter.WriteString("status", status.ToString().Underscore());