// Code generated by smithy-go-codegen DO NOT EDIT.

package cloudwatchlogs

import (
	"context"
	"fmt"
	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
	"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
	"github.com/aws/smithy-go/middleware"
	smithysync "github.com/aws/smithy-go/sync"
	smithyhttp "github.com/aws/smithy-go/transport/http"
	"sync"
)

// Retrieves a large logging object (LLO) and streams it back. This API is used to
// fetch the content of large portions of log events that have been ingested
// through the PutOpenTelemetryLogs API. When log events contain fields that would
// cause the total event size to exceed 1MB, CloudWatch Logs automatically
// processes up to 10 fields, starting with the largest fields. Each field is
// truncated as needed to keep the total event size as close to 1MB as possible.
// The excess portions are stored as Large Log Objects (LLOs) and these fields are
// processed separately and LLO reference system fields (in the format
// @ptr.$[path.to.field] ) are added. The path in the reference field reflects the
// original JSON structure where the large field was located. For example, this
// could be @ptr.$['input']['message'] , @ptr.$['AAA']['BBB']['CCC']['DDD'] ,
// @ptr.$['AAA'] , or any other path matching your log structure.
//
// The GetLogObject API routes requests using SDK host prefix injection. SDK
// versions released before April 1, 2026 route to
// streaming-logs.Region.amazonaws.com , which does not support VPC endpoints. SDK
// versions released on or after April 1, 2026 route to
// stream-logs.Region.amazonaws.com , which supports VPC endpoints. To set up a VPC
// endpoint for this API, see [Creating a VPC endpoint for CloudWatch Logs].
//
// [Creating a VPC endpoint for CloudWatch Logs]: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch-logs-and-interface-VPC.html#create-VPC-endpoint-for-CloudWatchLogs
func (c *Client) GetLogObject(ctx context.Context, params *GetLogObjectInput, optFns ...func(*Options)) (*GetLogObjectOutput, error) {
	if params == nil {
		params = &GetLogObjectInput{}
	}

	result, metadata, err := c.invokeOperation(ctx, "GetLogObject", params, optFns, c.addOperationGetLogObjectMiddlewares)
	if err != nil {
		return nil, err
	}

	out := result.(*GetLogObjectOutput)
	out.ResultMetadata = metadata
	return out, nil
}

// The parameters for the GetLogObject operation.
type GetLogObjectInput struct {

	// A pointer to the specific log object to retrieve. This is a required parameter
	// that uniquely identifies the log object within CloudWatch Logs. The pointer is
	// typically obtained from a previous query or filter operation.
	//
	// This member is required.
	LogObjectPointer *string

	// A boolean flag that indicates whether to unmask sensitive log data. When set to
	// true, any masked or redacted data in the log object will be displayed in its
	// original form. Default is false.
	Unmask bool

	noSmithyDocumentSerde
}

// The response from the GetLogObject operation.
type GetLogObjectOutput struct {
	eventStream *GetLogObjectEventStream

	// Metadata pertaining to the operation's result.
	ResultMetadata middleware.Metadata

	noSmithyDocumentSerde
}

// GetStream returns the type to interact with the event stream.
func (o *GetLogObjectOutput) GetStream() *GetLogObjectEventStream {
	return o.eventStream
}

func (c *Client) addOperationGetLogObjectMiddlewares(stack *middleware.Stack, options Options) (err error) {
	if err := stack.Serialize.Add(&setOperationInputMiddleware{}, middleware.After); err != nil {
		return err
	}
	err = stack.Serialize.Add(&awsAwsjson11_serializeOpGetLogObject{}, middleware.After)
	if err != nil {
		return err
	}
	err = stack.Deserialize.Add(&awsAwsjson11_deserializeOpGetLogObject{}, middleware.After)
	if err != nil {
		return err
	}
	if err := addProtocolFinalizerMiddlewares(stack, options, "GetLogObject"); err != nil {
		return fmt.Errorf("add protocol finalizers: %v", err)
	}

	if err = addlegacyEndpointContextSetter(stack, options); err != nil {
		return err
	}
	if err = addEventStreamGetLogObjectMiddleware(stack, options); err != nil {
		return err
	}
	if err = addSetLoggerMiddleware(stack, options); err != nil {
		return err
	}
	if err = addClientRequestID(stack); err != nil {
		return err
	}
	if err = addComputeContentLength(stack); err != nil {
		return err
	}
	if err = addResolveEndpointMiddleware(stack, options); err != nil {
		return err
	}
	if err = addComputePayloadSHA256(stack); err != nil {
		return err
	}
	if err = addRetry(stack, options); err != nil {
		return err
	}
	if err = addRawResponseToMetadata(stack); err != nil {
		return err
	}
	if err = addRecordResponseTiming(stack); err != nil {
		return err
	}
	if err = addSpanRetryLoop(stack, options); err != nil {
		return err
	}
	if err = addClientUserAgent(stack, options); err != nil {
		return err
	}
	if err = addSetLegacyContextSigningOptionsMiddleware(stack); err != nil {
		return err
	}
	if err = addTimeOffsetBuild(stack, c); err != nil {
		return err
	}
	if err = addUserAgentRetryMode(stack, options); err != nil {
		return err
	}
	if err = addCredentialSource(stack, options); err != nil {
		return err
	}
	if err = addEndpointPrefix_opGetLogObjectMiddleware(stack); err != nil {
		return err
	}
	if err = addOpGetLogObjectValidationMiddleware(stack); err != nil {
		return err
	}
	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opGetLogObject(options.Region), middleware.Before); err != nil {
		return err
	}
	if err = addRecursionDetection(stack); err != nil {
		return err
	}
	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
		return err
	}
	if err = addResponseErrorMiddleware(stack); err != nil {
		return err
	}
	if err = addRequestResponseLogging(stack, options); err != nil {
		return err
	}
	if err = addDisableHTTPSMiddleware(stack, options); err != nil {
		return err
	}
	if err = addInterceptBeforeRetryLoop(stack, options); err != nil {
		return err
	}
	if err = addInterceptAttempt(stack, options); err != nil {
		return err
	}
	if err = addInterceptors(stack, options); err != nil {
		return err
	}
	return nil
}

type endpointPrefix_opGetLogObjectMiddleware struct {
}

func (*endpointPrefix_opGetLogObjectMiddleware) ID() string {
	return "EndpointHostPrefix"
}

func (m *endpointPrefix_opGetLogObjectMiddleware) HandleFinalize(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (
	out middleware.FinalizeOutput, metadata middleware.Metadata, err error,
) {
	if smithyhttp.GetHostnameImmutable(ctx) || smithyhttp.IsEndpointHostPrefixDisabled(ctx) {
		return next.HandleFinalize(ctx, in)
	}

	req, ok := in.Request.(*smithyhttp.Request)
	if !ok {
		return out, metadata, fmt.Errorf("unknown transport type %T", in.Request)
	}

	req.URL.Host = "streaming-" + req.URL.Host

	return next.HandleFinalize(ctx, in)
}
func addEndpointPrefix_opGetLogObjectMiddleware(stack *middleware.Stack) error {
	return stack.Finalize.Insert(&endpointPrefix_opGetLogObjectMiddleware{}, "ResolveEndpointV2", middleware.After)
}

func newServiceMetadataMiddleware_opGetLogObject(region string) *awsmiddleware.RegisterServiceMetadata {
	return &awsmiddleware.RegisterServiceMetadata{
		Region:        region,
		ServiceID:     ServiceID,
		OperationName: "GetLogObject",
	}
}

// GetLogObjectEventStream provides the event stream handling for the GetLogObject operation.
//
// For testing and mocking the event stream this type should be initialized via
// the NewGetLogObjectEventStream constructor function. Using the functional options
// to pass in nested mock behavior.
type GetLogObjectEventStream struct {
	// GetLogObjectResponseStreamReader is the EventStream reader for the
	// GetLogObjectResponseStream events. This value is automatically set by the SDK
	// when the API call is made Use this member when unit testing your code with the
	// SDK to mock out the EventStream Reader.
	//
	// Must not be nil.
	Reader GetLogObjectResponseStreamReader

	done      chan struct{}
	closeOnce sync.Once
	err       *smithysync.OnceErr
}

// NewGetLogObjectEventStream initializes an GetLogObjectEventStream.
// This function should only be used for testing and mocking the GetLogObjectEventStream
// stream within your application.
//
// The Reader member must be set before reading events from the stream.
func NewGetLogObjectEventStream(optFns ...func(*GetLogObjectEventStream)) *GetLogObjectEventStream {
	es := &GetLogObjectEventStream{
		done: make(chan struct{}),
		err:  smithysync.NewOnceErr(),
	}
	for _, fn := range optFns {
		fn(es)
	}
	return es
}

// Events returns a channel to read events from.
func (es *GetLogObjectEventStream) Events() <-chan types.GetLogObjectResponseStream {
	return es.Reader.Events()
}

// Close closes the stream. This will also cause the stream to be closed.
// Close must be called when done using the stream API. Not calling Close
// may result in resource leaks.
//
// Will close the underlying EventStream writer and reader, and no more events can be
// sent or received.
func (es *GetLogObjectEventStream) Close() error {
	es.closeOnce.Do(es.safeClose)
	return es.Err()
}

func (es *GetLogObjectEventStream) safeClose() {
	close(es.done)

	es.Reader.Close()
}

// Err returns any error that occurred while reading or writing EventStream Events
// from the service API's response. Returns nil if there were no errors.
func (es *GetLogObjectEventStream) Err() error {
	if err := es.err.Err(); err != nil {
		return err
	}

	if err := es.Reader.Err(); err != nil {
		return err
	}

	return nil
}

func (es *GetLogObjectEventStream) waitStreamClose() {
	type errorSet interface {
		ErrorSet() <-chan struct{}
	}

	var outputErrCh <-chan struct{}
	if v, ok := es.Reader.(errorSet); ok {
		outputErrCh = v.ErrorSet()
	}
	var outputClosedCh <-chan struct{}
	if v, ok := es.Reader.(interface{ Closed() <-chan struct{} }); ok {
		outputClosedCh = v.Closed()
	}

	select {
	case <-es.done:
	case <-outputErrCh:
		es.err.SetError(es.Reader.Err())
		es.Close()

	case <-outputClosedCh:
		if err := es.Reader.Err(); err != nil {
			es.err.SetError(es.Reader.Err())
		}
		es.Close()

	}
}
