1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package neptune
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/protocol/query"
10	"github.com/aws/aws-sdk-go-v2/aws/retry"
11	"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
12	awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
13	presignedurlcust "github.com/aws/aws-sdk-go-v2/service/internal/presigned-url"
14	smithy "github.com/aws/smithy-go"
15	"github.com/aws/smithy-go/logging"
16	"github.com/aws/smithy-go/middleware"
17	smithyhttp "github.com/aws/smithy-go/transport/http"
18	"net/http"
19	"time"
20)
21
22const ServiceID = "Neptune"
23const ServiceAPIVersion = "2014-10-31"
24
25// Client provides the API client to make operations call for Amazon Neptune.
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
152func resolveDefaultLogger(o *Options) {
153	if o.Logger != nil {
154		return
155	}
156	o.Logger = logging.Nop{}
157}
158
159func addSetLoggerMiddleware(stack *middleware.Stack, o Options) error {
160	return middleware.AddSetLoggerMiddleware(stack, o.Logger)
161}
162
163// NewFromConfig returns a new client from the provided config.
164func NewFromConfig(cfg aws.Config, optFns ...func(*Options)) *Client {
165	opts := Options{
166		Region:        cfg.Region,
167		HTTPClient:    cfg.HTTPClient,
168		Credentials:   cfg.Credentials,
169		APIOptions:    cfg.APIOptions,
170		Logger:        cfg.Logger,
171		ClientLogMode: cfg.ClientLogMode,
172	}
173	resolveAWSRetryerProvider(cfg, &opts)
174	resolveAWSEndpointResolver(cfg, &opts)
175	return New(opts, optFns...)
176}
177
178func resolveHTTPClient(o *Options) {
179	if o.HTTPClient != nil {
180		return
181	}
182	o.HTTPClient = awshttp.NewBuildableClient()
183}
184
185func resolveRetryer(o *Options) {
186	if o.Retryer != nil {
187		return
188	}
189	o.Retryer = retry.NewStandard()
190}
191
192func resolveAWSRetryerProvider(cfg aws.Config, o *Options) {
193	if cfg.Retryer == nil {
194		return
195	}
196	o.Retryer = cfg.Retryer()
197}
198
199func resolveAWSEndpointResolver(cfg aws.Config, o *Options) {
200	if cfg.EndpointResolver == nil {
201		return
202	}
203	o.EndpointResolver = withEndpointResolver(cfg.EndpointResolver, NewDefaultEndpointResolver())
204}
205
206func addClientUserAgent(stack *middleware.Stack) error {
207	return awsmiddleware.AddRequestUserAgentMiddleware(stack)
208}
209
210func addHTTPSignerV4Middleware(stack *middleware.Stack, o Options) error {
211	mw := v4.NewSignHTTPRequestMiddleware(v4.SignHTTPRequestMiddlewareOptions{
212		CredentialsProvider: o.Credentials,
213		Signer:              o.HTTPSignerV4,
214		LogSigning:          o.ClientLogMode.IsSigning(),
215	})
216	return stack.Finalize.Add(mw, middleware.After)
217}
218
219type HTTPSignerV4 interface {
220	SignHTTP(ctx context.Context, credentials aws.Credentials, r *http.Request, payloadHash string, service string, region string, signingTime time.Time, optFns ...func(*v4.SignerOptions)) error
221}
222
223func resolveHTTPSignerV4(o *Options) {
224	if o.HTTPSignerV4 != nil {
225		return
226	}
227	o.HTTPSignerV4 = newDefaultV4Signer(*o)
228}
229
230func newDefaultV4Signer(o Options) *v4.Signer {
231	return v4.NewSigner(func(so *v4.SignerOptions) {
232		so.Logger = o.Logger
233		so.LogSigning = o.ClientLogMode.IsSigning()
234	})
235}
236
237func addRetryMiddlewares(stack *middleware.Stack, o Options) error {
238	mo := retry.AddRetryMiddlewaresOptions{
239		Retryer:          o.Retryer,
240		LogRetryAttempts: o.ClientLogMode.IsRetries(),
241	}
242	return retry.AddRetryMiddlewares(stack, mo)
243}
244
245func addRequestIDRetrieverMiddleware(stack *middleware.Stack) error {
246	return awsmiddleware.AddRequestIDRetrieverMiddleware(stack)
247}
248
249func addResponseErrorMiddleware(stack *middleware.Stack) error {
250	return awshttp.AddResponseErrorMiddleware(stack)
251}
252
253// HTTPPresignerV4 represents presigner interface used by presign url client
254type HTTPPresignerV4 interface {
255	PresignHTTP(
256		ctx context.Context, credentials aws.Credentials, r *http.Request,
257		payloadHash string, service string, region string, signingTime time.Time,
258		optFns ...func(*v4.SignerOptions),
259	) (url string, signedHeader http.Header, err error)
260}
261
262// PresignOptions represents the presign client options
263type PresignOptions struct {
264
265	// ClientOptions are list of functional options to mutate client options used by
266	// the presign client.
267	ClientOptions []func(*Options)
268
269	// Presigner is the presigner used by the presign url client
270	Presigner HTTPPresignerV4
271}
272
273func (o PresignOptions) copy() PresignOptions {
274	clientOptions := make([]func(*Options), len(o.ClientOptions))
275	copy(clientOptions, o.ClientOptions)
276	o.ClientOptions = clientOptions
277	return o
278}
279
280// WithPresignClientFromClientOptions is a helper utility to retrieve a function
281// that takes PresignOption as input
282func WithPresignClientFromClientOptions(optFns ...func(*Options)) func(*PresignOptions) {
283	return withPresignClientFromClientOptions(optFns).options
284}
285
286type withPresignClientFromClientOptions []func(*Options)
287
288func (w withPresignClientFromClientOptions) options(o *PresignOptions) {
289	o.ClientOptions = append(o.ClientOptions, w...)
290}
291
292// PresignClient represents the presign url client
293type PresignClient struct {
294	client  *Client
295	options PresignOptions
296}
297
298// NewPresignClient generates a presign client using provided API Client and
299// presign options
300func NewPresignClient(c *Client, optFns ...func(*PresignOptions)) *PresignClient {
301	var options PresignOptions
302	for _, fn := range optFns {
303		fn(&options)
304	}
305	if len(options.ClientOptions) != 0 {
306		c = New(c.options, options.ClientOptions...)
307	}
308
309	if options.Presigner == nil {
310		options.Presigner = newDefaultV4Signer(c.options)
311	}
312
313	return &PresignClient{
314		client:  c,
315		options: options,
316	}
317}
318
319func withNopHTTPClientAPIOption(o *Options) {
320	o.HTTPClient = smithyhttp.NopClient{}
321}
322
323type presignConverter PresignOptions
324
325func (c presignConverter) convertToPresignMiddleware(stack *middleware.Stack, options Options) (err error) {
326	stack.Finalize.Clear()
327	stack.Deserialize.Clear()
328	stack.Build.Remove((*awsmiddleware.ClientRequestID)(nil).ID())
329	stack.Build.Remove("UserAgent")
330	pmw := v4.NewPresignHTTPRequestMiddleware(v4.PresignHTTPRequestMiddlewareOptions{
331		CredentialsProvider: options.Credentials,
332		Presigner:           c.Presigner,
333		LogSigning:          options.ClientLogMode.IsSigning(),
334	})
335	err = stack.Finalize.Add(pmw, middleware.After)
336	if err != nil {
337		return err
338	}
339	// convert request to a GET request
340	err = query.AddAsGetRequestMiddleware(stack)
341	if err != nil {
342		return err
343	}
344	err = presignedurlcust.AddAsIsPresigingMiddleware(stack)
345	if err != nil {
346		return err
347	}
348	return nil
349}
350
351func addRequestResponseLogging(stack *middleware.Stack, o Options) error {
352	return stack.Deserialize.Add(&smithyhttp.RequestResponseLogger{
353		LogRequest:          o.ClientLogMode.IsRequest(),
354		LogRequestWithBody:  o.ClientLogMode.IsRequestWithBody(),
355		LogResponse:         o.ClientLogMode.IsResponse(),
356		LogResponseWithBody: o.ClientLogMode.IsResponseWithBody(),
357	}, middleware.After)
358}
359