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