< Summary

Class:Azure.AI.FormRecognizer.Training.FormTrainingClient
Assembly:Azure.AI.FormRecognizer
File(s):C:\Git\azure-sdk-for-net\sdk\formrecognizer\Azure.AI.FormRecognizer\src\FormTrainingClient.cs
Covered lines:153
Uncovered lines:30
Coverable lines:183
Total lines:571
Line coverage:83.6% (153 of 183)
Covered branches:4
Total branches:4
Branch coverage:100% (4 of 4)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
.ctor(...)-50%100%
.ctor(...)-100%100%
StartTraining(...)-72.73%100%
StartTrainingAsync()-72.73%100%
GetCustomModel(...)-100%100%
GetCustomModelAsync()-100%100%
DeleteModel(...)-100%100%
DeleteModelAsync()-100%100%
GetCustomModels(...)-100%100%
<GetCustomModelsAsync()-62.5%100%
<GetCustomModelsAsync()-62.5%100%
GetCustomModelsAsync(...)-100%100%
GetAccountProperties(...)-100%100%
GetAccountPropertiesAsync()-100%100%
StartCopyModel(...)-100%100%
StartCopyModelAsync()-100%100%
GetCopyAuthorization(...)-70%100%
GetCopyAuthorizationAsync()-70%100%
GetFormRecognizerClient()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\formrecognizer\Azure.AI.FormRecognizer\src\FormTrainingClient.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Azure.AI.FormRecognizer.Models;
 8using Azure.Core;
 9using Azure.Core.Pipeline;
 10
 11namespace Azure.AI.FormRecognizer.Training
 12{
 13    /// <summary>
 14    /// The client to use to connect with the Form Recognizer Azure Cognitive Service to train models from
 15    /// custom forms.  It also supports listing and deleting trained models, as well as accessing account
 16    /// properties.
 17    /// </summary>
 18    public class FormTrainingClient
 19    {
 20        /// <summary>Provides communication with the Form Recognizer Azure Cognitive Service through its REST API.</summ
 21        internal readonly ServiceRestClient ServiceClient;
 22
 23        /// <summary>Provides tools for exception creation in case of failure.</summary>
 24        internal readonly ClientDiagnostics Diagnostics;
 25
 26        /// <summary>
 27        /// Initializes a new instance of the <see cref="FormTrainingClient"/> class.
 28        /// </summary>
 20029        protected FormTrainingClient()
 30        {
 20031        }
 32
 33        /// <summary>
 34        /// Initializes a new instance of the <see cref="FormTrainingClient"/> class.
 35        /// </summary>
 36        /// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service.</p
 37        /// <param name="credential">A credential used to authenticate to an Azure Service.</param>
 38        /// <remarks>
 39        /// Both the <paramref name="endpoint"/> URI string and the <paramref name="credential"/> <c>string</c> key
 40        /// can be found in the Azure Portal.
 41        /// </remarks>
 42        /// <seealso href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecogn
 3243        public FormTrainingClient(Uri endpoint, AzureKeyCredential credential) : this(endpoint, credential, new FormReco
 44        {
 2845        }
 46
 47        /// <summary>
 48        /// Initializes a new instance of the <see cref="FormTrainingClient"/> class.
 49        /// </summary>
 50        /// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service.</p
 51        /// <param name="credential">A credential used to authenticate to an Azure Service.</param>
 52        /// <param name="options">A set of options to apply when configuring the client.</param>
 53        /// <remarks>
 54        /// Both the <paramref name="endpoint"/> URI string and the <paramref name="credential"/> <c>string</c> key
 55        /// can be found in the Azure Portal.
 56        /// </remarks>
 57        /// <seealso href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecogn
 22458        public FormTrainingClient(Uri endpoint, AzureKeyCredential credential, FormRecognizerClientOptions options)
 59        {
 22460            Argument.AssertNotNull(endpoint, nameof(endpoint));
 21661            Argument.AssertNotNull(credential, nameof(credential));
 21662            Argument.AssertNotNull(options, nameof(options));
 63
 21664            Diagnostics = new ClientDiagnostics(options);
 21665            HttpPipeline pipeline = HttpPipelineBuilder.Build(options, new AzureKeyCredentialPolicy(credential, Constant
 21666            ServiceClient = new ServiceRestClient(Diagnostics, pipeline, endpoint.AbsoluteUri);
 21667        }
 68
 69        /// <summary>
 70        /// Initializes a new instance of the <see cref="FormTrainingClient"/> class.
 71        /// </summary>
 72        /// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service.</p
 73        /// <param name="credential">A credential used to authenticate to an Azure Service.</param>
 74        /// <remarks>
 75        /// The <paramref name="endpoint"/> URI string can be found in the Azure Portal.
 76        /// </remarks>
 77        /// <seealso href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecogn
 78        public FormTrainingClient(Uri endpoint, TokenCredential credential)
 879            : this(endpoint, credential, new FormRecognizerClientOptions())
 80        {
 081        }
 82
 83        /// <summary>
 84        /// Initializes a new instance of the <see cref="FormTrainingClient"/> class.
 85        /// </summary>
 86        /// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service.</p
 87        /// <param name="credential">A credential used to authenticate to an Azure Service.</param>
 88        /// <param name="options">A set of options to apply when configuring the client.</param>
 89        /// <remarks>
 90        /// The <paramref name="endpoint"/> URI string can be found in the Azure Portal.
 91        /// </remarks>
 92        /// <seealso href="https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecogn
 2093        public FormTrainingClient(Uri endpoint, TokenCredential credential, FormRecognizerClientOptions options)
 94        {
 2095            Argument.AssertNotNull(endpoint, nameof(endpoint));
 1296            Argument.AssertNotNull(credential, nameof(credential));
 497            Argument.AssertNotNull(options, nameof(options));
 98
 499            Diagnostics = new ClientDiagnostics(options);
 4100            var pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, Constants.
 4101            ServiceClient = new ServiceRestClient(Diagnostics, pipeline, endpoint.AbsoluteUri);
 4102        }
 103
 104        #region Training
 105
 106        /// <summary>
 107        /// Trains a model from a collection of custom forms in a blob storage container.
 108        /// </summary>
 109        /// <param name="trainingFilesUri">An externally accessible Azure storage blob container Uri.
 110        /// For more information see <a href="https://docs.microsoft.com/azure/cognitive-services/form-recognizer/build-
 111        /// <param name="useTrainingLabels">If <c>true</c>, use a label file created in the &lt;link-to-label-tool-doc&g
 112        /// <param name="trainingOptions">A set of options available for configuring the training request.</param>
 113        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 114        /// <returns>
 115        /// <para>A <see cref="TrainingOperation"/> to wait on this long-running operation. Its <see cref="TrainingOpera
 116        /// completion will contain meta-data about the trained model.</para>
 117        /// <para>Even if training fails, a model is created in the Form Recognizer account with an "invalid" status.
 118        /// A <see cref="RequestFailedException"/> will be raised containing the modelId to access this invalid model.</
 119        /// </returns>
 120        public virtual TrainingOperation StartTraining(Uri trainingFilesUri, bool useTrainingLabels, TrainingOptions tra
 121        {
 74122            Argument.AssertNotNull(trainingFilesUri, nameof(trainingFilesUri));
 72123            trainingOptions ??= new TrainingOptions();
 124
 72125            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(StartTraining)}
 72126            scope.Start();
 127
 128            try
 129            {
 72130                var trainRequest = new TrainRequest(trainingFilesUri.AbsoluteUri) { SourceFilter = trainingOptions.Train
 131
 72132                ResponseWithHeaders<ServiceTrainCustomModelAsyncHeaders> response = ServiceClient.TrainCustomModelAsync(
 72133                return new TrainingOperation(response.Headers.Location, ServiceClient, Diagnostics);
 134            }
 0135            catch (Exception e)
 136            {
 0137                scope.Failed(e);
 0138                throw;
 139            }
 72140        }
 141
 142        /// <summary>
 143        /// Trains a model from a collection of custom forms in a blob storage container.
 144        /// </summary>
 145        /// <param name="trainingFilesUri">An externally accessible Azure storage blob container Uri.
 146        /// For more information see <a href="https://docs.microsoft.com/azure/cognitive-services/form-recognizer/build-
 147        /// <param name="useTrainingLabels">If <c>true</c>, use a label file created in the &lt;link-to-label-tool-doc&g
 148        /// <param name="trainingOptions">A set of options available for configuring the training request.</param>
 149        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 150        /// <returns>
 151        /// <para>A <see cref="TrainingOperation"/> to wait on this long-running operation. Its <see cref="TrainingOpera
 152        /// completion will contain meta-data about the trained model.</para>
 153        /// <para>Even if training fails, a model is created in the Form Recognizer account with an "invalid" status.
 154        /// A <see cref="RequestFailedException"/> will be raised containing the modelId to access this invalid model.</
 155        /// </returns>
 156        public virtual async Task<TrainingOperation> StartTrainingAsync(Uri trainingFilesUri, bool useTrainingLabels, Tr
 157        {
 78158            Argument.AssertNotNull(trainingFilesUri, nameof(trainingFilesUri));
 76159            trainingOptions ??= new TrainingOptions();
 160
 76161            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(StartTraining)}
 76162            scope.Start();
 163
 164            try
 165            {
 76166                var trainRequest = new TrainRequest(trainingFilesUri.AbsoluteUri) { SourceFilter = trainingOptions.Train
 167
 76168                ResponseWithHeaders<ServiceTrainCustomModelAsyncHeaders> response = await ServiceClient.TrainCustomModel
 76169                return new TrainingOperation(response.Headers.Location, ServiceClient, Diagnostics);
 170            }
 0171            catch (Exception e)
 172            {
 0173                scope.Failed(e);
 0174                throw;
 175            }
 76176        }
 177
 178        #endregion
 179
 180        #region Management Ops
 181
 182        /// <summary>
 183        /// Gets a description of a custom model, including the types of forms it can recognize and the fields it will e
 184        /// </summary>
 185        /// <param name="modelId">The ID of the model to retrieve.</param>
 186        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 187        /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to a <see cref
 188        /// information about the requested model.</returns>
 189        public virtual Response<CustomFormModel> GetCustomModel(string modelId, CancellationToken cancellationToken = de
 190        {
 16191            Argument.AssertNotNullOrEmpty(modelId, nameof(modelId));
 192
 12193            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomModel)
 12194            scope.Start();
 195
 196            try
 197            {
 12198                Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId));
 199
 10200                Response<Model> response = ServiceClient.GetCustomModel(guid, includeKeys: true, cancellationToken);
 6201                return Response.FromValue(new CustomFormModel(response.Value), response.GetRawResponse());
 202            }
 6203            catch (Exception e)
 204            {
 6205                scope.Failed(e);
 6206                throw;
 207            }
 6208        }
 209
 210        /// <summary>
 211        /// Gets a description of a custom model, including the types of forms it can recognize and the fields it will e
 212        /// </summary>
 213        /// <param name="modelId">The ID of the model to retrieve.</param>
 214        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 215        /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to a <see cref
 216        /// information about the requested model.</returns>
 217        public virtual async Task<Response<CustomFormModel>> GetCustomModelAsync(string modelId, CancellationToken cance
 218        {
 16219            Argument.AssertNotNullOrEmpty(modelId, nameof(modelId));
 220
 12221            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomModel)
 12222            scope.Start();
 223
 224            try
 225            {
 12226                Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId));
 227
 10228                Response<Model> response = await ServiceClient.GetCustomModelAsync(guid, includeKeys: true, cancellation
 6229                return Response.FromValue(new CustomFormModel(response.Value), response.GetRawResponse());
 230            }
 6231            catch (Exception e)
 232            {
 6233                scope.Failed(e);
 6234                throw;
 235            }
 6236        }
 237
 238        /// <summary>
 239        /// Deletes the model with the specified model ID.
 240        /// </summary>
 241        /// <param name="modelId">The ID of the model to delete.</param>
 242        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 243        /// <returns>A <see cref="Response"/> representing the result of the operation.</returns>
 244        public virtual Response DeleteModel(string modelId, CancellationToken cancellationToken = default)
 245        {
 60246            Argument.AssertNotNullOrEmpty(modelId, nameof(modelId));
 247
 56248            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(DeleteModel)}")
 56249            scope.Start();
 250
 251            try
 252            {
 56253                Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId));
 54254                return ServiceClient.DeleteCustomModel(guid, cancellationToken);
 255            }
 4256            catch (Exception e)
 257            {
 4258                scope.Failed(e);
 4259                throw;
 260            }
 52261        }
 262
 263        /// <summary>
 264        /// Deletes the model with the specified model ID.
 265        /// </summary>
 266        /// <param name="modelId">The ID of the model to delete.</param>
 267        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 268        /// <returns>A <see cref="Response"/> representing the result of the operation.</returns>
 269        public virtual async Task<Response> DeleteModelAsync(string modelId, CancellationToken cancellationToken = defau
 270        {
 60271            Argument.AssertNotNullOrEmpty(modelId, nameof(modelId));
 272
 56273            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(DeleteModel)}")
 56274            scope.Start();
 275
 276            try
 277            {
 56278                Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId));
 54279                return await ServiceClient.DeleteCustomModelAsync(guid, cancellationToken).ConfigureAwait(false);
 280            }
 4281            catch (Exception e)
 282            {
 4283                scope.Failed(e);
 4284                throw;
 285            }
 52286        }
 287
 288        /// <summary>
 289        /// Gets a collection of items describing the models trained on this Cognitive Services Account
 290        /// and their training status.
 291        /// </summary>
 292        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 293        /// <returns>A collection of <see cref="CustomFormModelInfo"/> items.</returns>
 294        public virtual Pageable<CustomFormModelInfo> GetCustomModels(CancellationToken cancellationToken = default)
 295        {
 296            Page<CustomFormModelInfo> FirstPageFunc(int? pageSizeHint)
 297            {
 4298                using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomMo
 4299                scope.Start();
 300
 301                try
 302                {
 4303                    Response<Models.Models> response = ServiceClient.ListCustomModels(cancellationToken);
 4304                    return Page.FromValues(response.Value.ModelList, response.Value.NextLink, response.GetRawResponse())
 305                }
 0306                catch (Exception e)
 307                {
 0308                    scope.Failed(e);
 0309                    throw;
 310                }
 4311            }
 312
 313            Page<CustomFormModelInfo> NextPageFunc(string nextLink, int? pageSizeHint)
 314            {
 0315                using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomMo
 0316                scope.Start();
 317
 318                try
 319                {
 0320                    Response<Models.Models> response = ServiceClient.ListCustomModelsNextPage(nextLink, cancellationToke
 0321                    return Page.FromValues(response.Value.ModelList, response.Value.NextLink, response.GetRawResponse())
 322                }
 0323                catch (Exception e)
 324                {
 0325                    scope.Failed(e);
 0326                    throw;
 327                }
 0328            }
 329
 4330            return PageableHelpers.CreateEnumerable(FirstPageFunc, NextPageFunc);
 331        }
 332
 333        /// <summary>
 334        /// Gets a collection of items describing the models trained on this Cognitive Services Account
 335        /// and their training status.
 336        /// </summary>
 337        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 338        /// <returns>A collection of <see cref="CustomFormModelInfo"/> items.</returns>
 339        public virtual AsyncPageable<CustomFormModelInfo> GetCustomModelsAsync(CancellationToken cancellationToken = def
 340        {
 341            async Task<Page<CustomFormModelInfo>> FirstPageFunc(int? pageSizeHint)
 342            {
 4343                using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomMo
 4344                scope.Start();
 345
 346                try
 347                {
 4348                    Response<Models.Models> response = await ServiceClient.ListCustomModelsAsync(cancellationToken).Conf
 4349                    return Page.FromValues(response.Value.ModelList, response.Value.NextLink, response.GetRawResponse())
 350                }
 0351                catch (Exception e)
 352                {
 0353                    scope.Failed(e);
 0354                    throw;
 355                }
 4356            }
 357
 358            async Task<Page<CustomFormModelInfo>> NextPageFunc(string nextLink, int? pageSizeHint)
 359            {
 4360                using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCustomMo
 4361                scope.Start();
 362
 363                try
 364                {
 4365                    Response<Models.Models> response = await ServiceClient.ListCustomModelsNextPageAsync(nextLink, cance
 4366                    return Page.FromValues(response.Value.ModelList, response.Value.NextLink, response.GetRawResponse())
 367                }
 0368                catch (Exception e)
 369                {
 0370                    scope.Failed(e);
 0371                    throw;
 372                }
 4373            }
 374
 4375            return PageableHelpers.CreateAsyncEnumerable(FirstPageFunc, NextPageFunc);
 376        }
 377
 378        /// <summary>
 379        /// Gets the number of models trained on this Cognitive Services Account and the account limits.
 380        /// </summary>
 381        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 382        /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to an <see cre
 383        /// the account properties.</returns>
 384        public virtual Response<AccountProperties> GetAccountProperties(CancellationToken cancellationToken = default)
 385        {
 8386            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetAccountPrope
 8387            scope.Start();
 388
 389            try
 390            {
 8391                Response<Models.Models> response = ServiceClient.GetCustomModels(cancellationToken);
 4392                return Response.FromValue(new AccountProperties(response.Value.Summary), response.GetRawResponse());
 393            }
 4394            catch (Exception e)
 395            {
 4396                scope.Failed(e);
 4397                throw;
 398            }
 4399        }
 400
 401        /// <summary>
 402        /// Gets the number of models trained on this Cognitive Services Account and the account limits.
 403        /// </summary>
 404        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 405        /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to an <see cre
 406        /// the account properties.</returns>
 407        public virtual async Task<Response<AccountProperties>> GetAccountPropertiesAsync(CancellationToken cancellationT
 408        {
 8409            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetAccountPrope
 8410            scope.Start();
 411
 412            try
 413            {
 8414                Response<Models.Models> response = await ServiceClient.GetCustomModelsAsync(cancellationToken).Configure
 4415                return Response.FromValue(new AccountProperties(response.Value.Summary), response.GetRawResponse());
 416            }
 4417            catch (Exception e)
 418            {
 4419                scope.Failed(e);
 4420                throw;
 421            }
 4422        }
 423
 424        #endregion
 425
 426        #region Copy
 427        /// <summary>
 428        /// Copy a custom model stored in this resource (the source) to the user specified
 429        /// target Form Recognizer resource.
 430        /// </summary>
 431        /// <param name="modelId">Model identifier of the model to copy to the target Form Recognizer resource.</param>
 432        /// <param name="target">A <see cref="CopyAuthorization"/> with the copy authorization to the target Form Recogn
 433        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 434        /// <returns>A <see cref="CopyModelOperation"/> to wait on this long-running operation.  Its <see cref="CopyMode
 435        /// completion will contain meta-data about the model copied.</returns>
 436        public virtual CopyModelOperation StartCopyModel(string modelId, CopyAuthorization target, CancellationToken can
 437        {
 12438            Argument.AssertNotNullOrEmpty(modelId, nameof(modelId));
 8439            Argument.AssertNotNull(target, nameof(target));
 440
 6441            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(StartCopyModel)
 6442            scope.Start();
 443
 444            try
 445            {
 6446                Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId));
 4447                var request = new CopyRequest(target.ResourceId,
 4448                                              target.Region,
 4449                                              new CopyAuthorizationResult(target.ModelId, target.AccessToken, target.Exp
 450
 4451                Response response = ServiceClient.CopyCustomModel(guid, request, cancellationToken);
 4452                string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);
 453
 4454                return new CopyModelOperation(ServiceClient, Diagnostics, location, target.ModelId);
 455            }
 2456            catch (Exception e)
 457            {
 2458                scope.Failed(e);
 2459                throw;
 460            }
 4461        }
 462
 463        /// <summary>
 464        /// Copy a custom model stored in this resource (the source) to the user specified
 465        /// target Form Recognizer resource.
 466        /// </summary>
 467        /// <param name="modelId">Model identifier of the model to copy to the target Form Recognizer resource.</param>
 468        /// <param name="target">A <see cref="CopyAuthorization"/> with the copy authorization to the target Form Recogn
 469        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 470        /// <returns>A <see cref="CopyModelOperation"/> to wait on this long-running operation.  Its <see cref="CopyMode
 471        /// completion will contain meta-data about the model copied.</returns>
 472        public virtual async Task<CopyModelOperation> StartCopyModelAsync(string modelId, CopyAuthorization target, Canc
 473        {
 16474            Argument.AssertNotNullOrEmpty(modelId, nameof(modelId));
 12475            Argument.AssertNotNull(target, nameof(target));
 476
 10477            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(StartCopyModel)
 10478            scope.Start();
 479
 480            try
 481            {
 10482                Guid guid = ClientCommon.ValidateModelId(modelId, nameof(modelId));
 8483                var request = new CopyRequest(target.ResourceId,
 8484                                              target.Region,
 8485                                              new CopyAuthorizationResult(target.ModelId, target.AccessToken, target.Exp
 486
 8487                Response response = await ServiceClient.CopyCustomModelAsync(guid, request, cancellationToken).Configure
 8488                string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);
 489
 8490                return new CopyModelOperation(ServiceClient, Diagnostics, location, target.ModelId);
 491            }
 2492            catch (Exception e)
 493            {
 2494                scope.Failed(e);
 2495                throw;
 496            }
 8497        }
 498
 499        /// <summary>
 500        /// Generate authorization for copying a custom model into the target Form Recognizer resource.
 501        /// </summary>
 502        /// <param name="resourceId">Azure Resource Id of the target Form Recognizer resource where the model will be co
 503        /// This information can be found in the Properties section of the Form Recognizer resource in the Azure Portal.
 504        /// <param name="resourceRegion">Location of the target Form Recognizer resource.
 505        /// This information can be found in the Keys and Endpoint section of the Form Recognizer resource in the Azure 
 506        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 507        /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to <see cref="
 508        /// the authorization information necessary to copy a custom model into a target Form Recognizer resource.</retu
 509        public virtual Response<CopyAuthorization> GetCopyAuthorization(string resourceId, string resourceRegion, Cancel
 510        {
 16511            Argument.AssertNotNullOrEmpty(resourceId, nameof(resourceId));
 12512            Argument.AssertNotNullOrEmpty(resourceRegion, nameof(resourceRegion));
 513
 8514            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCopyAuthoriz
 8515            scope.Start();
 516
 517            try
 518            {
 8519                Response<CopyAuthorizationResult> response = ServiceClient.GenerateModelCopyAuthorization(cancellationTo
 8520                return Response.FromValue(new CopyAuthorization(response.Value, resourceId, resourceRegion), response.Ge
 521            }
 0522            catch (Exception e)
 523            {
 0524                scope.Failed(e);
 0525                throw;
 526            }
 8527        }
 528
 529        /// <summary>
 530        /// Generate authorization for copying a custom model into the target Form Recognizer resource.
 531        /// </summary>
 532        /// <param name="resourceId">Azure Resource Id of the target Form Recognizer resource where the model will be co
 533        /// This information can be found in the Properties section of the Form Recognizer resource in the Azure Portal.
 534        /// <param name="resourceRegion">Location of the target Form Recognizer resource.
 535        /// This information can be found in the Keys and Endpoint section of the Form Recognizer resource in the Azure 
 536        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 537        /// <returns>A <see cref="Response{T}"/> representing the result of the operation. It can be cast to <see cref="
 538        /// the authorization information necessary to copy a custom model into a target Form Recognizer resource.</retu
 539        public virtual async Task<Response<CopyAuthorization>> GetCopyAuthorizationAsync(string resourceId, string resou
 540        {
 20541            Argument.AssertNotNullOrEmpty(resourceId, nameof(resourceId));
 16542            Argument.AssertNotNullOrEmpty(resourceRegion, nameof(resourceRegion));
 543
 12544            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(GetCopyAuthoriz
 12545            scope.Start();
 546
 547            try
 548            {
 12549                Response<CopyAuthorizationResult> response = await ServiceClient.GenerateModelCopyAuthorizationAsync(can
 12550                return Response.FromValue(new CopyAuthorization(response.Value, resourceId, resourceRegion), response.Ge
 551            }
 0552            catch (Exception e)
 553            {
 0554                scope.Failed(e);
 0555                throw;
 556            }
 12557        }
 558        #endregion Copy
 559
 560        #region Form Recognizer Client
 561
 562        /// <summary>
 563        /// Gets an instance of a <see cref="FormRecognizerClient"/> that shares the same endpoint, the same
 564        /// credentials and the same set of <see cref="FormRecognizerClientOptions"/> this client has.
 565        /// </summary>
 566        /// <returns>A new instance of a <see cref="FormRecognizerClient"/>.</returns>
 4567        public virtual FormRecognizerClient GetFormRecognizerClient() => new FormRecognizerClient(Diagnostics, ServiceCl
 568
 569        #endregion Form Recognizer Client
 570    }
 571}