public override ApiTaskResult <TResult, TFailureResult> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
            {
                using var jsonDocument = JsonDocument.ParseValue(ref reader);
                var taskId = jsonDocument.RootElement.GetProperty(propNames.IdPropName.AsUtf8()).GetGuid();

                var taskStateValue = jsonDocument.RootElement.GetProperty(propNames.TaskStatePropName.AsUtf8()).GetString();
                var taskState      = (ApiTaskState)Enum.Parse(typeof(ApiTaskState), taskStateValue !, true);

                var taskTypeValue = jsonDocument.RootElement.GetProperty(propNames.TaskTypePropName.AsUtf8()).GetString();
                var taskType      = taskTypeValue == null ? null : Urn.Parse(taskTypeValue);

                switch (taskState)
                {
                case ApiTaskState.Running:
                    return(ApiTaskResult <TResult, TFailureResult> .Running(taskId, taskType));

                case ApiTaskState.Succeed:
                    return(DeserializeResult(jsonDocument, taskStateValue, taskId, taskType, options));

                case ApiTaskState.Failed:
                    var errorJson = jsonDocument.RootElement.GetProperty(propNames.ApiErrorPropName.AsUtf8()).GetRawText();
                    var error     = DeserializeAs <ApiError>(errorJson, propNames.ApiErrorPropName, taskStateValue, options);
                    return(ApiTaskResult <TResult, TFailureResult> .TaskFailure(error, taskId, taskType));

                default:
                    throw Errors.UnexpectedEnumMember(nameof(taskState), taskState);
                }
            }
コード例 #2
0
        public void TryGetFailureResult_should_return_error_if_api_task_result_is_api_error()
        {
            var taskResult = ApiTaskResult <SuccessResult, FailureResult> .TaskFailure(AnApiError, Guid.NewGuid(), new Urn("nid", "nss"));

            var success = taskResult.TryGetFailureResult(out var result);

            success.Should().BeFalse();
            result.Should().BeNull();
        }
コード例 #3
0
        public void TryGetTaskError_should_return_task_error_if_api_task_result_is_api_error()
        {
            var apiError   = AnApiError;
            var taskResult = ApiTaskResult <SuccessResult, FailureResult> .TaskFailure(apiError, Guid.NewGuid(), new Urn("nid", "nss"));

            var success = taskResult.TryGetTaskError(out var result);

            success.Should().BeTrue();
            result.Should().Be(apiError);
        }
コード例 #4
0
        public void TaskFailure_should_create_result_in_the_failure_state()
        {
            var id          = Guid.NewGuid();
            var taskType    = new Urn("nid", "nss");
            var expectedDto = new ExpectedResult
            {
                Id        = id,
                TaskType  = taskType,
                TaskState = ApiTaskState.Failed
            };

            var result = ApiTaskResult <SuccessResult, FailureResult> .TaskFailure(AnApiError, id, taskType);

            result.Should().BeEquivalentTo(expectedDto);
        }
コード例 #5
0
            public override ApiTaskResult <TResult> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
            {
                using var jsonDocument = JsonDocument.ParseValue(ref reader);
                var taskId = jsonDocument.RootElement.GetProperty(propNames.IdPropName.AsUtf8()).GetGuid();

                var taskStateValue = jsonDocument.RootElement.GetProperty(propNames.TaskStatePropName.AsUtf8()).GetString();
                var taskState      = (ApiTaskState)Enum.Parse(typeof(ApiTaskState), taskStateValue !, true);

                var taskTypeValue = jsonDocument.RootElement.GetProperty(propNames.TaskTypePropName.AsUtf8()).GetString();
                var taskType      = taskTypeValue == null ? null : Urn.Parse(taskTypeValue);

                switch (taskState)
                {
                case ApiTaskState.Running:
                    return(ApiTaskResult <TResult> .Running(taskId, taskType));

                case ApiTaskState.Succeed:
                    var resultJson    = jsonDocument.RootElement.GetProperty(propNames.TaskResultPropName.AsUtf8()).GetRawText();
                    var successResult = DeserializeAs <TResult>(resultJson, propNames.TaskResultPropName, taskStateValue);
                    return(ApiTaskResult <TResult> .Success(successResult, taskId, taskType));

                case ApiTaskState.Failed:
                    var errorJson = jsonDocument.RootElement.GetProperty(propNames.ApiErrorPropName.AsUtf8()).GetRawText();
                    var error     = DeserializeAs <ApiError>(errorJson, propNames.ApiErrorPropName, taskStateValue);
                    return(ApiTaskResult <TResult> .TaskFailure(error, taskId, taskType));

                default:
                    throw Errors.UnexpectedEnumMember(nameof(taskState), taskState);
                }

                T DeserializeAs <T>(string json, in Utf8String propName, string?taskStatePropValue)
                {
                    return(JsonSerializer.Deserialize <T>(json, options) ??
                           throw Errors.JsonPropertyCannotBeNullIfAnotherPropertyHasValue(
                               propName.ToString(),
                               propNames.TaskStatePropName.ToString(),
                               taskStatePropValue));
                }
            }
コード例 #6
0
        public void ConvertToSingleApiResult_should_convert_task_error_result_to_api_error()
        {
            var id          = Guid.NewGuid();
            var taskType    = new Urn("nid", "nss");
            var expectedDto = new ExpectedResult
            {
                Id        = id,
                TaskType  = taskType,
                TaskState = ApiTaskState.Failed
            };
            var expectedApiError = new ApiError(HttpStatusCode.Forbidden, "an error");
            var taskResult       = ApiTaskResult <SuccessResult, FailureResult> .TaskFailure(expectedApiError, id, taskType);

            var oneResultApiResult = taskResult.ConvertToSingleApiResult(
                x => x.Value,
                x => new ApiError(HttpStatusCode.Forbidden, x.Value.ToString())
                );

            oneResultApiResult.Should().BeEquivalentTo(expectedDto);
            oneResultApiResult.TryGetTaskError(out var apiError).Should().BeTrue();
            apiError.Should().BeEquivalentTo(expectedApiError);
        }