1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package braket
4
5import (
6	"context"
7	"fmt"
8	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
9	"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
10	"github.com/aws/aws-sdk-go-v2/service/braket/types"
11	"github.com/aws/smithy-go/middleware"
12	smithyhttp "github.com/aws/smithy-go/transport/http"
13)
14
15// Searches for devices using the specified filters.
16func (c *Client) SearchDevices(ctx context.Context, params *SearchDevicesInput, optFns ...func(*Options)) (*SearchDevicesOutput, error) {
17	if params == nil {
18		params = &SearchDevicesInput{}
19	}
20
21	result, metadata, err := c.invokeOperation(ctx, "SearchDevices", params, optFns, addOperationSearchDevicesMiddlewares)
22	if err != nil {
23		return nil, err
24	}
25
26	out := result.(*SearchDevicesOutput)
27	out.ResultMetadata = metadata
28	return out, nil
29}
30
31type SearchDevicesInput struct {
32
33	// The filter values to use to search for a device.
34	//
35	// This member is required.
36	Filters []types.SearchDevicesFilter
37
38	// The maximum number of results to return in the response.
39	MaxResults *int32
40
41	// A token used for pagination of results returned in the response. Use the token
42	// returned from the previous request continue results where the previous request
43	// ended.
44	NextToken *string
45}
46
47type SearchDevicesOutput struct {
48
49	// An array of DeviceSummary objects for devices that match the specified filter
50	// values.
51	//
52	// This member is required.
53	Devices []types.DeviceSummary
54
55	// A token used for pagination of results, or null if there are no additional
56	// results. Use the token value in a subsequent request to continue results where
57	// the previous request ended.
58	NextToken *string
59
60	// Metadata pertaining to the operation's result.
61	ResultMetadata middleware.Metadata
62}
63
64func addOperationSearchDevicesMiddlewares(stack *middleware.Stack, options Options) (err error) {
65	err = stack.Serialize.Add(&awsRestjson1_serializeOpSearchDevices{}, middleware.After)
66	if err != nil {
67		return err
68	}
69	err = stack.Deserialize.Add(&awsRestjson1_deserializeOpSearchDevices{}, middleware.After)
70	if err != nil {
71		return err
72	}
73	if err = addSetLoggerMiddleware(stack, options); err != nil {
74		return err
75	}
76	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
77		return err
78	}
79	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
80		return err
81	}
82	if err = addResolveEndpointMiddleware(stack, options); err != nil {
83		return err
84	}
85	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
86		return err
87	}
88	if err = addRetryMiddlewares(stack, options); err != nil {
89		return err
90	}
91	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
92		return err
93	}
94	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
95		return err
96	}
97	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
98		return err
99	}
100	if err = addClientUserAgent(stack); err != nil {
101		return err
102	}
103	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
104		return err
105	}
106	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
107		return err
108	}
109	if err = addOpSearchDevicesValidationMiddleware(stack); err != nil {
110		return err
111	}
112	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
113		return err
114	}
115	if err = addResponseErrorMiddleware(stack); err != nil {
116		return err
117	}
118	if err = addRequestResponseLogging(stack, options); err != nil {
119		return err
120	}
121	return nil
122}
123
124// SearchDevicesAPIClient is a client that implements the SearchDevices operation.
125type SearchDevicesAPIClient interface {
126	SearchDevices(context.Context, *SearchDevicesInput, ...func(*Options)) (*SearchDevicesOutput, error)
127}
128
129var _ SearchDevicesAPIClient = (*Client)(nil)
130
131// SearchDevicesPaginatorOptions is the paginator options for SearchDevices
132type SearchDevicesPaginatorOptions struct {
133	// The maximum number of results to return in the response.
134	Limit int32
135
136	// Set to true if pagination should stop if the service returns a pagination token
137	// that matches the most recent token provided to the service.
138	StopOnDuplicateToken bool
139}
140
141// SearchDevicesPaginator is a paginator for SearchDevices
142type SearchDevicesPaginator struct {
143	options   SearchDevicesPaginatorOptions
144	client    SearchDevicesAPIClient
145	params    *SearchDevicesInput
146	nextToken *string
147	firstPage bool
148}
149
150// NewSearchDevicesPaginator returns a new SearchDevicesPaginator
151func NewSearchDevicesPaginator(client SearchDevicesAPIClient, params *SearchDevicesInput, optFns ...func(*SearchDevicesPaginatorOptions)) *SearchDevicesPaginator {
152	if params == nil {
153		params = &SearchDevicesInput{}
154	}
155
156	options := SearchDevicesPaginatorOptions{}
157	if params.MaxResults != nil {
158		options.Limit = *params.MaxResults
159	}
160
161	for _, fn := range optFns {
162		fn(&options)
163	}
164
165	return &SearchDevicesPaginator{
166		options:   options,
167		client:    client,
168		params:    params,
169		firstPage: true,
170	}
171}
172
173// HasMorePages returns a boolean indicating whether more pages are available
174func (p *SearchDevicesPaginator) HasMorePages() bool {
175	return p.firstPage || p.nextToken != nil
176}
177
178// NextPage retrieves the next SearchDevices page.
179func (p *SearchDevicesPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*SearchDevicesOutput, error) {
180	if !p.HasMorePages() {
181		return nil, fmt.Errorf("no more pages available")
182	}
183
184	params := *p.params
185	params.NextToken = p.nextToken
186
187	var limit *int32
188	if p.options.Limit > 0 {
189		limit = &p.options.Limit
190	}
191	params.MaxResults = limit
192
193	result, err := p.client.SearchDevices(ctx, &params, optFns...)
194	if err != nil {
195		return nil, err
196	}
197	p.firstPage = false
198
199	prevToken := p.nextToken
200	p.nextToken = result.NextToken
201
202	if p.options.StopOnDuplicateToken && prevToken != nil && p.nextToken != nil && *prevToken == *p.nextToken {
203		p.nextToken = nil
204	}
205
206	return result, nil
207}
208