コード例 #1
0
        public async Task <bool> CreateTaskAsync(TaskModel taskModel)
        {
            _logger.LogTrace("POSTing new task model");
            try
            {
                HttpResponseMessage response = await _client.PostAsJsonAsync("tasks/", taskModel, new JsonSerializerOptions
                {
                    IgnoreNullValues = true
                });

                if (!response.IsSuccessStatusCode)
                {
                    string stringJsonError = await response.Content.ReadAsStringAsync();

                    var jsonOptions = new JsonSerializerOptions()
                    {
                        PropertyNameCaseInsensitive = true
                    };
                    RestError restError = JsonSerializer.Deserialize <RestError>(stringJsonError, jsonOptions);
                    _logger.LogTrace("Task POST failed {code} {errors}", (int)response.StatusCode, restError.GetErrors());
                    return(false);
                }
                _logger.LogTrace("NewTask POST was suceessful");
                return(true);
            }
            catch (Exception e)
            {
                _logger.LogWarning("Create task POST failed {error}", e);
            }
            return(false);
        }
コード例 #2
0
ファイル: SubmissionService.cs プロジェクト: cppseminar/APC
        public async Task <Submission> CreateSubmissionAsync(Submission submission)
        {
            try
            {
                _logger.LogTrace("Posting new {submission}", submission);
                // TODO: We should distinguish deadline errors from operation errors
                HttpResponseMessage message = await _client.PostAsJsonAsync("/submission/", submission);

                if (message.IsSuccessStatusCode)
                {
                    _logger.LogTrace("Submission POST returned success");
                    return(await message.Content.ReadAsAsync <Submission>());
                }
                else
                {
                    string stringJsonError = await message.Content.ReadAsStringAsync();

                    var jsonOptions = new JsonSerializerOptions
                    {
                        PropertyNameCaseInsensitive = true
                    };
                    RestError restError = JsonSerializer.Deserialize <RestError>(stringJsonError, jsonOptions);
                    _logger.LogWarning("Post submission returned {status}, errors: {errorList}", (int)message.StatusCode, restError.GetErrors());
                    throw new OperationFailedException();
                }
            }
            catch (Exception e)
            {
                _logger.LogWarning("Create submission failed {e}", e);
                throw new OperationFailedException();
            }
        }