1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package cloudsearch
4
5import (
6	"context"
7	"github.com/aws/aws-sdk-go-v2/aws"
8	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
9	"github.com/aws/aws-sdk-go-v2/aws/retry"
10	"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
11	awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
12	smithy "github.com/aws/smithy-go"
13	"github.com/aws/smithy-go/logging"
14	"github.com/aws/smithy-go/middleware"
15	smithyhttp "github.com/aws/smithy-go/transport/http"
16	"net/http"
17	"time"
18)
19
20const ServiceID = "CloudSearch"
21const ServiceAPIVersion = "2013-01-01"
22
23// Client provides the API client to make operations call for Amazon CloudSearch.
24type Client struct {
25	options Options
26}
27
28// New returns an initialized Client based on the functional options. Provide
29// additional functional options to further configure the behavior of the client,
30// such as changing the client's endpoint or adding custom middleware behavior.
31func New(options Options, optFns ...func(*Options)) *Client {
32	options = options.Copy()
33
34	resolveDefaultLogger(&options)
35
36	resolveRetryer(&options)
37
38	resolveHTTPClient(&options)
39
40	resolveHTTPSignerV4(&options)
41
42	resolveDefaultEndpointConfiguration(&options)
43
44	for _, fn := range optFns {
45		fn(&options)
46	}
47
48	client := &Client{
49		options: options,
50	}
51
52	return client
53}
54
55type Options struct {
56	// Set of options to modify how an operation is invoked. These apply to all
57	// operations invoked for this client. Use functional options on operation call to
58	// modify this list for per operation behavior.
59	APIOptions []func(*middleware.Stack) error
60
61	// Configures the events that will be sent to the configured logger.
62	ClientLogMode aws.ClientLogMode
63
64	// The credentials object to use when signing requests.
65	Credentials aws.CredentialsProvider
66
67	// The endpoint options to be used when attempting to resolve an endpoint.
68	EndpointOptions EndpointResolverOptions
69
70	// The service endpoint resolver.
71	EndpointResolver EndpointResolver
72
73	// Signature Version 4 (SigV4) Signer
74	HTTPSignerV4 HTTPSignerV4
75
76	// The logger writer interface to write logging messages to.
77	Logger logging.Logger
78
79	// The region to send requests to. (Required)
80	Region string
81
82	// Retryer guides how HTTP requests should be retried in case of recoverable
83	// failures. When nil the API client will use a default retryer.
84	Retryer aws.Retryer
85
86	// The HTTP client to invoke API calls with. Defaults to client's default HTTP
87	// implementation if nil.
88	HTTPClient HTTPClient
89}
90
91// WithAPIOptions returns a functional option for setting the Client's APIOptions
92// option.
93func WithAPIOptions(optFns ...func(*middleware.Stack) error) func(*Options) {
94	return func(o *Options) {
95		o.APIOptions = append(o.APIOptions, optFns...)
96	}
97}
98
99// WithEndpointResolver returns a functional option for setting the Client's
100// EndpointResolver option.
101func WithEndpointResolver(v EndpointResolver) func(*Options) {
102	return func(o *Options) {
103		o.EndpointResolver = v
104	}
105}
106
107type HTTPClient interface {
108	Do(*http.Request) (*http.Response, error)
109}
110
111// Copy creates a clone where the APIOptions list is deep copied.
112func (o Options) Copy() Options {
113	to := o
114	to.APIOptions = make([]func(*middleware.Stack) error, len(o.APIOptions))
115	copy(to.APIOptions, o.APIOptions)
116	return to
117}
118func (c *Client) invokeOperation(ctx context.Context, opID string, params interface{}, optFns []func(*Options), stackFns ...func(*middleware.Stack, Options) error) (result interface{}, metadata middleware.Metadata, err error) {
119	ctx = middleware.ClearStackValues(ctx)
120	stack := middleware.NewStack(opID, smithyhttp.NewStackRequest)
121	options := c.options.Copy()
122	for _, fn := range optFns {
123		fn(&options)
124	}
125
126	for _, fn := range stackFns {
127		if err := fn(stack, options); err != nil {
128			return nil, metadata, err
129		}
130	}
131
132	for _, fn := range options.APIOptions {
133		if err := fn(stack); err != nil {
134			return nil, metadata, err
135		}
136	}
137
138	handler := middleware.DecorateHandler(smithyhttp.NewClientHandler(options.HTTPClient), stack)
139	result, metadata, err = handler.Handle(ctx, params)
140	if err != nil {
141		err = &smithy.OperationError{
142			ServiceID:     ServiceID,
143			OperationName: opID,
144			Err:           err,
145		}
146	}
147	return result, metadata, err
148}
149
150func resolveDefaultLogger(o *Options) {
151	if o.Logger != nil {
152		return
153	}
154	o.Logger = logging.Nop{}
155}
156
157func addSetLoggerMiddleware(stack *middleware.Stack, o Options) error {
158	return middleware.AddSetLoggerMiddleware(stack, o.Logger)
159}
160
161// NewFromConfig returns a new client from the provided config.
162func NewFromConfig(cfg aws.Config, optFns ...func(*Options)) *Client {
163	opts := Options{
164		Region:        cfg.Region,
165		HTTPClient:    cfg.HTTPClient,
166		Credentials:   cfg.Credentials,
167		APIOptions:    cfg.APIOptions,
168		Logger:        cfg.Logger,
169		ClientLogMode: cfg.ClientLogMode,
170	}
171	resolveAWSRetryerProvider(cfg, &opts)
172	resolveAWSEndpointResolver(cfg, &opts)
173	return New(opts, optFns...)
174}
175
176func resolveHTTPClient(o *Options) {
177	if o.HTTPClient != nil {
178		return
179	}
180	o.HTTPClient = awshttp.NewBuildableClient()
181}
182
183func resolveRetryer(o *Options) {
184	if o.Retryer != nil {
185		return
186	}
187	o.Retryer = retry.NewStandard()
188}
189
190func resolveAWSRetryerProvider(cfg aws.Config, o *Options) {
191	if cfg.Retryer == nil {
192		return
193	}
194	o.Retryer = cfg.Retryer()
195}
196
197func resolveAWSEndpointResolver(cfg aws.Config, o *Options) {
198	if cfg.EndpointResolver == nil {
199		return
200	}
201	o.EndpointResolver = withEndpointResolver(cfg.EndpointResolver, NewDefaultEndpointResolver())
202}
203
204func addClientUserAgent(stack *middleware.Stack) error {
205	return awsmiddleware.AddSDKAgentKeyValue(awsmiddleware.APIMetadata, "cloudsearch", goModuleVersion)(stack)
206}
207
208func addHTTPSignerV4Middleware(stack *middleware.Stack, o Options) error {
209	mw := v4.NewSignHTTPRequestMiddleware(v4.SignHTTPRequestMiddlewareOptions{
210		CredentialsProvider: o.Credentials,
211		Signer:              o.HTTPSignerV4,
212		LogSigning:          o.ClientLogMode.IsSigning(),
213	})
214	return stack.Finalize.Add(mw, middleware.After)
215}
216
217type HTTPSignerV4 interface {
218	SignHTTP(ctx context.Context, credentials aws.Credentials, r *http.Request, payloadHash string, service string, region string, signingTime time.Time, optFns ...func(*v4.SignerOptions)) error
219}
220
221func resolveHTTPSignerV4(o *Options) {
222	if o.HTTPSignerV4 != nil {
223		return
224	}
225	o.HTTPSignerV4 = newDefaultV4Signer(*o)
226}
227
228func newDefaultV4Signer(o Options) *v4.Signer {
229	return v4.NewSigner(func(so *v4.SignerOptions) {
230		so.Logger = o.Logger
231		so.LogSigning = o.ClientLogMode.IsSigning()
232	})
233}
234
235func addRetryMiddlewares(stack *middleware.Stack, o Options) error {
236	mo := retry.AddRetryMiddlewaresOptions{
237		Retryer:          o.Retryer,
238		LogRetryAttempts: o.ClientLogMode.IsRetries(),
239	}
240	return retry.AddRetryMiddlewares(stack, mo)
241}
242
243func addRequestIDRetrieverMiddleware(stack *middleware.Stack) error {
244	return awsmiddleware.AddRequestIDRetrieverMiddleware(stack)
245}
246
247func addResponseErrorMiddleware(stack *middleware.Stack) error {
248	return awshttp.AddResponseErrorMiddleware(stack)
249}
250
251func addRequestResponseLogging(stack *middleware.Stack, o Options) error {
252	return stack.Deserialize.Add(&smithyhttp.RequestResponseLogger{
253		LogRequest:          o.ClientLogMode.IsRequest(),
254		LogRequestWithBody:  o.ClientLogMode.IsRequestWithBody(),
255		LogResponse:         o.ClientLogMode.IsResponse(),
256		LogResponseWithBody: o.ClientLogMode.IsResponseWithBody(),
257	}, middleware.After)
258}
259