async ValueTask <OperationState <CustomFormModel> > IOperation <CustomFormModel> .UpdateStateAsync(bool async, CancellationToken cancellationToken) { // Include keys is always set to true -- the service does not have a use case for includeKeys: false. Response <Model> response = async ? await _serviceClient.GetCustomModelAsync(new Guid(Id), includeKeys : true, cancellationToken).ConfigureAwait(false) : _serviceClient.GetCustomModel(new Guid(Id), includeKeys: true, cancellationToken); CustomFormModelStatus status = response.Value.ModelInfo.Status; Response rawResponse = response.GetRawResponse(); if (status == CustomFormModelStatus.Ready) { return(OperationState <CustomFormModel> .Success(rawResponse, new CustomFormModel(response.Value, _serviceVersion))); } else if (status == CustomFormModelStatus.Invalid) { RequestFailedException requestFailedException = await ClientCommon.CreateExceptionForFailedOperationAsync( async, _diagnostics, rawResponse, response.Value.TrainResult.Errors, $"Invalid model created with ID {response.Value.ModelInfo.ModelId}").ConfigureAwait(false); return(OperationState <CustomFormModel> .Failure(rawResponse, requestFailedException)); } return(OperationState <CustomFormModel> .Pending(rawResponse)); }
/// <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) { using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(CreateCustomFormModelOperation)}.{nameof(UpdateStatus)}"); scope.Start(); try { // Include keys is always set to true -- the service does not have a use case for includeKeys: false. Response <Model> 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; } } catch (Exception e) { scope.Failed(e); throw; } } return(GetRawResponse()); }
/// <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 Response <CustomFormModel> GetCustomModel(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 = ServiceClient.GetCustomModel(guid, includeKeys: true, cancellationToken); return(Response.FromValue(new CustomFormModel(response.Value), response.GetRawResponse())); } catch (Exception e) { scope.Failed(e); throw; } }