/******************************************************************************* * Copyright 2012-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"). You may not use * this file except in compliance with the License. A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * ***************************************************************************** * * AWS Tools for Windows (TM) PowerShell (TM) * */ using System; using System.Collections.Generic; using System.Linq; using System.Management.Automation; using System.Text; using Amazon.PowerShell.Common; using Amazon.Runtime; using Amazon.Comprehend; using Amazon.Comprehend.Model; namespace Amazon.PowerShell.Cmdlets.COMP { /// /// Creates a new document classification request to analyze a single document in real-time, /// using a previously created and trained custom model and an endpoint. /// /// /// /// You can input plain text or you can upload a single-page input document (text, PDF, /// Word, or image). /// /// If the system detects errors while processing a page in the input document, the API /// response includes an entry in Errors that describes the errors. /// /// If the system detects a document-level error in your input document, the API returns /// an InvalidRequestException error response. For details about this exception, /// see /// Errors in semi-structured documents in the Comprehend Developer Guide. /// /// [Cmdlet("Invoke", "COMPDocumentClassification", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)] [OutputType("Amazon.Comprehend.Model.DocumentClass")] [AWSCmdlet("Calls the Amazon Comprehend ClassifyDocument API operation.", Operation = new[] {"ClassifyDocument"}, SelectReturnType = typeof(Amazon.Comprehend.Model.ClassifyDocumentResponse))] [AWSCmdletOutput("Amazon.Comprehend.Model.DocumentClass or Amazon.Comprehend.Model.ClassifyDocumentResponse", "This cmdlet returns a collection of Amazon.Comprehend.Model.DocumentClass objects.", "The service call response (type Amazon.Comprehend.Model.ClassifyDocumentResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack." )] public partial class InvokeCOMPDocumentClassificationCmdlet : AmazonComprehendClientCmdlet, IExecutor { protected override bool IsSensitiveRequest { get; set; } = true; #region Parameter Byte /// /// /// Use the Bytes parameter to input a text, PDF, Word or image file. You /// can also use the Bytes parameter to input an Amazon Textract DetectDocumentText /// or AnalyzeDocument output file.Provide the input document as a sequence of base64-encoded bytes. If your code uses /// an Amazon Web Services SDK to classify documents, the SDK may encode the document /// file bytes for you. The maximum length of this field depends on the input document type. For details, /// see /// Inputs for real-time custom analysis in the Comprehend Developer Guide. If you use the Bytes parameter, do not use the Text parameter. /// /// The cmdlet will automatically convert the supplied parameter of type string, string[], System.IO.FileInfo or System.IO.Stream to byte[] before supplying it to the service. /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [Alias("Bytes")] [Amazon.PowerShell.Common.MemoryStreamParameterConverter] public byte[] Byte { get; set; } #endregion #region Parameter DocumentReaderConfig_DocumentReadAction /// /// /// This field defines the Amazon Textract API operation that Amazon Comprehend uses to /// extract text from PDF files and image files. Enter one of the following values: /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [AWSConstantClassSource("Amazon.Comprehend.DocumentReadAction")] public Amazon.Comprehend.DocumentReadAction DocumentReaderConfig_DocumentReadAction { get; set; } #endregion #region Parameter DocumentReaderConfig_DocumentReadMode /// /// /// Determines the text extraction actions for PDF files. Enter one of the following values: /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [AWSConstantClassSource("Amazon.Comprehend.DocumentReadMode")] public Amazon.Comprehend.DocumentReadMode DocumentReaderConfig_DocumentReadMode { get; set; } #endregion #region Parameter EndpointArn /// /// /// The Amazon Resource Number (ARN) of the endpoint. For information about endpoints, /// see Managing /// endpoints. /// /// #if !MODULAR [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] #else [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true)] [System.Management.Automation.AllowEmptyString] [System.Management.Automation.AllowNull] #endif [Amazon.PowerShell.Common.AWSRequiredParameter] public System.String EndpointArn { get; set; } #endregion #region Parameter DocumentReaderConfig_FeatureType /// /// /// Specifies the type of Amazon Textract features to apply. If you chose TEXTRACT_ANALYZE_DOCUMENT /// as the read action, you must specify one or both of the following values: /// /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] [Alias("DocumentReaderConfig_FeatureTypes")] public System.String[] DocumentReaderConfig_FeatureType { get; set; } #endregion #region Parameter Text /// /// /// The document text to be analyzed. If you enter text using this parameter, do not use /// the Bytes parameter. /// /// [System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)] public System.String Text { get; set; } #endregion #region Parameter Select /// /// Use the -Select parameter to control the cmdlet output. The default value is 'Classes'. /// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.Comprehend.Model.ClassifyDocumentResponse). /// Specifying the name of a property of type Amazon.Comprehend.Model.ClassifyDocumentResponse will result in that property being returned. /// Specifying -Select '^ParameterName' will result in the cmdlet returning the selected cmdlet parameter value. /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public string Select { get; set; } = "Classes"; #endregion #region Parameter PassThru /// /// Changes the cmdlet behavior to return the value passed to the Text parameter. /// The -PassThru parameter is deprecated, use -Select '^Text' instead. This parameter will be removed in a future version. /// [System.Obsolete("The -PassThru parameter is deprecated, use -Select '^Text' instead. This parameter will be removed in a future version.")] [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public SwitchParameter PassThru { get; set; } #endregion #region Parameter Force /// /// This parameter overrides confirmation prompts to force /// the cmdlet to continue its operation. This parameter should always /// be used with caution. /// [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)] public SwitchParameter Force { get; set; } #endregion protected override void ProcessRecord() { this._AWSSignerType = "v4"; base.ProcessRecord(); var resourceIdentifiersText = FormatParameterValuesForConfirmationMsg(nameof(this.EndpointArn), MyInvocation.BoundParameters); if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "Invoke-COMPDocumentClassification (ClassifyDocument)")) { return; } var context = new CmdletContext(); // allow for manipulation of parameters prior to loading into context PreExecutionContextLoad(context); #pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute if (ParameterWasBound(nameof(this.Select))) { context.Select = CreateSelectDelegate(Select) ?? throw new System.ArgumentException("Invalid value for -Select parameter.", nameof(this.Select)); if (this.PassThru.IsPresent) { throw new System.ArgumentException("-PassThru cannot be used when -Select is specified.", nameof(this.Select)); } } else if (this.PassThru.IsPresent) { context.Select = (response, cmdlet) => this.Text; } #pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute context.Byte = this.Byte; context.DocumentReaderConfig_DocumentReadAction = this.DocumentReaderConfig_DocumentReadAction; context.DocumentReaderConfig_DocumentReadMode = this.DocumentReaderConfig_DocumentReadMode; if (this.DocumentReaderConfig_FeatureType != null) { context.DocumentReaderConfig_FeatureType = new List(this.DocumentReaderConfig_FeatureType); } context.EndpointArn = this.EndpointArn; #if MODULAR if (this.EndpointArn == null && ParameterWasBound(nameof(this.EndpointArn))) { WriteWarning("You are passing $null as a value for parameter EndpointArn which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues."); } #endif context.Text = this.Text; // allow further manipulation of loaded context prior to processing PostExecutionContextLoad(context); var output = Execute(context) as CmdletOutput; ProcessOutput(output); } #region IExecutor Members public object Execute(ExecutorContext context) { System.IO.MemoryStream _ByteStream = null; try { var cmdletContext = context as CmdletContext; // create request var request = new Amazon.Comprehend.Model.ClassifyDocumentRequest(); if (cmdletContext.Byte != null) { _ByteStream = new System.IO.MemoryStream(cmdletContext.Byte); request.Bytes = _ByteStream; } // populate DocumentReaderConfig var requestDocumentReaderConfigIsNull = true; request.DocumentReaderConfig = new Amazon.Comprehend.Model.DocumentReaderConfig(); Amazon.Comprehend.DocumentReadAction requestDocumentReaderConfig_documentReaderConfig_DocumentReadAction = null; if (cmdletContext.DocumentReaderConfig_DocumentReadAction != null) { requestDocumentReaderConfig_documentReaderConfig_DocumentReadAction = cmdletContext.DocumentReaderConfig_DocumentReadAction; } if (requestDocumentReaderConfig_documentReaderConfig_DocumentReadAction != null) { request.DocumentReaderConfig.DocumentReadAction = requestDocumentReaderConfig_documentReaderConfig_DocumentReadAction; requestDocumentReaderConfigIsNull = false; } Amazon.Comprehend.DocumentReadMode requestDocumentReaderConfig_documentReaderConfig_DocumentReadMode = null; if (cmdletContext.DocumentReaderConfig_DocumentReadMode != null) { requestDocumentReaderConfig_documentReaderConfig_DocumentReadMode = cmdletContext.DocumentReaderConfig_DocumentReadMode; } if (requestDocumentReaderConfig_documentReaderConfig_DocumentReadMode != null) { request.DocumentReaderConfig.DocumentReadMode = requestDocumentReaderConfig_documentReaderConfig_DocumentReadMode; requestDocumentReaderConfigIsNull = false; } List requestDocumentReaderConfig_documentReaderConfig_FeatureType = null; if (cmdletContext.DocumentReaderConfig_FeatureType != null) { requestDocumentReaderConfig_documentReaderConfig_FeatureType = cmdletContext.DocumentReaderConfig_FeatureType; } if (requestDocumentReaderConfig_documentReaderConfig_FeatureType != null) { request.DocumentReaderConfig.FeatureTypes = requestDocumentReaderConfig_documentReaderConfig_FeatureType; requestDocumentReaderConfigIsNull = false; } // determine if request.DocumentReaderConfig should be set to null if (requestDocumentReaderConfigIsNull) { request.DocumentReaderConfig = null; } if (cmdletContext.EndpointArn != null) { request.EndpointArn = cmdletContext.EndpointArn; } if (cmdletContext.Text != null) { request.Text = cmdletContext.Text; } CmdletOutput output; // issue call var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint); try { var response = CallAWSServiceOperation(client, request); object pipelineOutput = null; pipelineOutput = cmdletContext.Select(response, this); output = new CmdletOutput { PipelineOutput = pipelineOutput, ServiceResponse = response }; } catch (Exception e) { output = new CmdletOutput { ErrorResponse = e }; } return output; } finally { if( _ByteStream != null) { _ByteStream.Dispose(); } } } public ExecutorContext CreateContext() { return new CmdletContext(); } #endregion #region AWS Service Operation Call private Amazon.Comprehend.Model.ClassifyDocumentResponse CallAWSServiceOperation(IAmazonComprehend client, Amazon.Comprehend.Model.ClassifyDocumentRequest request) { Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Comprehend", "ClassifyDocument"); try { #if DESKTOP return client.ClassifyDocument(request); #elif CORECLR return client.ClassifyDocumentAsync(request).GetAwaiter().GetResult(); #else #error "Unknown build edition" #endif } catch (AmazonServiceException exc) { var webException = exc.InnerException as System.Net.WebException; if (webException != null) { throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException); } throw; } } #endregion internal partial class CmdletContext : ExecutorContext { public byte[] Byte { get; set; } public Amazon.Comprehend.DocumentReadAction DocumentReaderConfig_DocumentReadAction { get; set; } public Amazon.Comprehend.DocumentReadMode DocumentReaderConfig_DocumentReadMode { get; set; } public List DocumentReaderConfig_FeatureType { get; set; } public System.String EndpointArn { get; set; } public System.String Text { get; set; } public System.Func Select { get; set; } = (response, cmdlet) => response.Classes; } } }