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

package query

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/internal/protocoltest/query/schemas"
	smithy "github.com/aws/smithy-go"
	"github.com/aws/smithy-go/middleware"
	smithyhttp "github.com/aws/smithy-go/transport/http"
	"strings"
)

func (c *Client) EndpointWithHostLabelOperation(ctx context.Context, params *EndpointWithHostLabelOperationInput, optFns ...func(*Options)) (*EndpointWithHostLabelOperationOutput, error) {
	if params == nil {
		params = &EndpointWithHostLabelOperationInput{}
	}

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

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

type EndpointWithHostLabelOperationInput struct {

	// This member is required.
	Label *string

	noSmithyDocumentSerde
}

func (v *EndpointWithHostLabelOperationInput) Serialize(s smithy.ShapeSerializer) {
	s.WriteStruct(schemas.HostLabelInput)
	v.SerializeMembers(s)
	s.CloseStruct()
}

func (v *EndpointWithHostLabelOperationInput) SerializeMembers(s smithy.ShapeSerializer) {
	if v.Label != nil {
		s.WriteString(schemas.HostLabelInput_label, *v.Label)
	}
}
func (v *EndpointWithHostLabelOperationInput) Deserialize(d smithy.ShapeDeserializer) error {
	return smithy.ReadStruct(d, schemas.HostLabelInput, func(s *smithy.Schema) error {
		switch s {
		case schemas.HostLabelInput_label:
			v.Label = new(string)
			return d.ReadString(schemas.HostLabelInput_label, v.Label)
		}
		return nil
	})
}

type EndpointWithHostLabelOperationOutput struct {
	// Metadata pertaining to the operation's result.
	ResultMetadata middleware.Metadata

	noSmithyDocumentSerde
}

func (v *EndpointWithHostLabelOperationOutput) Serialize(s smithy.ShapeSerializer) {
	s.WriteStruct(nil)
	v.SerializeMembers(s)
	s.CloseStruct()
}

func (v *EndpointWithHostLabelOperationOutput) SerializeMembers(s smithy.ShapeSerializer) {
}
func (v *EndpointWithHostLabelOperationOutput) Deserialize(d smithy.ShapeDeserializer) error {
	return smithy.ReadStruct(d, nil, func(s *smithy.Schema) error {
		switch s {
		}
		return nil
	})
}
func (c *Client) addOperationEndpointWithHostLabelOperationMiddlewares(stack *middleware.Stack, options Options) (err error) {
	if err := stack.Serialize.Add(&serializeRequestMiddleware{options: &options, operationSchema: smithy.NewOperationSchema(schemas.EndpointWithHostLabelOperation, schemas.HostLabelInput, nil)}, middleware.After); err != nil {
		return err
	}
	if err := stack.Deserialize.Add(&deserializeResponseMiddleware{options: &options, operationSchema: smithy.NewOperationSchema(schemas.EndpointWithHostLabelOperation, schemas.HostLabelInput, nil), output: &EndpointWithHostLabelOperationOutput{}}, middleware.After); err != nil {
		return err
	}

	if err = addlegacyEndpointContextSetter(stack, options); 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 = addRecordResponseTiming(stack); err != nil {
		return err
	}
	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = addCredentialSource(stack, options); err != nil {
		return err
	}
	if err = addEndpointPrefix_opEndpointWithHostLabelOperationMiddleware(stack); err != nil {
		return err
	}
	if err = addOpEndpointWithHostLabelOperationValidationMiddleware(stack); err != nil {
		return err
	}
	if err = stack.Initialize.Add(newServiceMetadataMiddleware(options.Region, "EndpointWithHostLabelOperation"), middleware.Before); 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 = addInterceptors(stack, options); err != nil {
		return err
	}
	return nil
}

type endpointPrefix_opEndpointWithHostLabelOperationMiddleware struct {
}

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

func (m *endpointPrefix_opEndpointWithHostLabelOperationMiddleware) 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)
	}

	opaqueInput := getOperationInput(ctx)
	input, ok := opaqueInput.(*EndpointWithHostLabelOperationInput)
	if !ok {
		return out, metadata, fmt.Errorf("unknown input type %T", opaqueInput)
	}

	var prefix strings.Builder
	prefix.WriteString("foo.")
	if input.Label == nil {
		return out, metadata, &smithy.SerializationError{Err: fmt.Errorf("Label forms part of the endpoint host and so may not be nil")}
	} else if !smithyhttp.ValidHostLabel(*input.Label) {
		return out, metadata, &smithy.SerializationError{Err: fmt.Errorf("Label forms part of the endpoint host and so must match \"[a-zA-Z0-9-]{1,63}\", but was \"%s\"", *input.Label)}
	} else {
		prefix.WriteString(*input.Label)
	}
	prefix.WriteString(".")
	req.URL.Host = prefix.String() + req.URL.Host

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