示例#1
0
        /// <summary>
        /// Calls the server to get updated status of the long-running operation.
        /// </summary>
        /// <param name="async">When <c>true</c>, the method will be executed asynchronously; otherwise, it will execute synchronously.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the service call.</param>
        /// <returns>The HTTP response received from the server.</returns>
        private async ValueTask <Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
        {
            if (!_hasCompleted)
            {
                // Include keys is always set to true -- the service does not have a use case for includeKeys: false.
                Response <Model_internal> update = async
                    ? await _serviceClient.GetCustomModelAsync(new Guid(Id), includeKeys : true, cancellationToken).ConfigureAwait(false)
                    : _serviceClient.GetCustomModel(new Guid(Id), includeKeys: true, cancellationToken);

                _response = update.GetRawResponse();

                if (update.Value.ModelInfo.Status == CustomFormModelStatus.Ready)
                {
                    // We need to first assign a value and then mark the operation as completed to avoid a race condition with the getter in Value
                    _value        = new CustomFormModel(update.Value);
                    _hasCompleted = true;
                }
                else if (update.Value.ModelInfo.Status == CustomFormModelStatus.Invalid)
                {
                    _requestFailedException = await ClientCommon.CreateExceptionForFailedOperationAsync(
                        async,
                        _diagnostics,
                        _response,
                        update.Value.TrainResult.Errors,
                        $"Invalid model created with ID {update.Value.ModelInfo.ModelId}").ConfigureAwait(false);

                    _hasCompleted = true;
                    throw _requestFailedException;
                }
            }

            return(GetRawResponse());
        }
示例#2
0
        public virtual async Task <Response <CustomFormModel> > GetCustomModelAsync(string modelId, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(modelId, nameof(modelId));

            Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId));

            Response <Model_internal> response = await ServiceClient.GetCustomModelAsync(guid, includeKeys : true, cancellationToken).ConfigureAwait(false);

            return(Response.FromValue(new CustomFormModel(response.Value), response.GetRawResponse()));
        }
示例#3
0
        /// <summary>
        /// Calls the server to get updated status of the long-running operation.
        /// </summary>
        /// <param name="async">When <c>true</c>, the method will be executed asynchronously; otherwise, it will execute synchronously.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
        /// <returns>The HTTP response from the service.</returns>
        private async ValueTask <Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
        {
            if (!_hasCompleted)
            {
                // Include keys is always set to true -- the service does not have a use case for includeKeys: false.
                Response <Model_internal> update = async
                    ? await _serviceClient.GetCustomModelAsync(new Guid(Id), includeKeys : true, cancellationToken).ConfigureAwait(false)
                    : _serviceClient.GetCustomModel(new Guid(Id), includeKeys: true, cancellationToken);

                if (update.Value.ModelInfo.Status != CustomFormModelStatus.Training)
                {
                    _hasCompleted = true;
                    _value        = new CustomFormModel(update.Value);
                }

                _response = update.GetRawResponse();
            }

            return(GetRawResponse());
        }
示例#4
0
        /// <summary>
        /// Gets a description of a custom model, including the types of forms it can recognize and the fields it will extract for each form type.
        /// </summary>
        /// <param name="modelId">The ID of the model to retrieve.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
        /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to a <see cref="CustomFormModel"/> containing
        /// information about the requested model.</returns>
        public virtual async Task <Response <CustomFormModel> > GetCustomModelAsync(string modelId, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(modelId, nameof(modelId));

            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomModel)}");
            scope.Start();

            try
            {
                Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId));

                Response <Model> response = await ServiceClient.GetCustomModelAsync(guid, includeKeys : true, cancellationToken).ConfigureAwait(false);

                return(Response.FromValue(new CustomFormModel(response.Value), response.GetRawResponse()));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }