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