|
8 | 8 | "reflect"
|
9 | 9 | "strings"
|
10 | 10 | "time"
|
11 |
| - |
12 |
| - "github.com/algolia/algoliasearch-client-go/v4/algolia/errs" |
13 | 11 | )
|
14 | 12 |
|
15 | 13 | // ToPtr is a helper routine that returns a pointer to the given value.
|
@@ -68,60 +66,6 @@ func IsNilOrEmpty(i any) bool {
|
68 | 66 | }
|
69 | 67 | }
|
70 | 68 |
|
71 |
| -type IterableError struct { |
72 |
| - Validate func(any, error) bool |
73 |
| - Message func(any, error) string |
74 |
| -} |
75 |
| - |
76 |
| -func CreateIterable[T any](execute func(*T, error) (*T, error), validate func(*T, error) bool, opts ...IterableOption) (*T, error) { |
77 |
| - options := Options{ |
78 |
| - MaxRetries: 50, |
79 |
| - Timeout: func(_ int) time.Duration { |
80 |
| - return 1 * time.Second |
81 |
| - }, |
82 |
| - } |
83 |
| - |
84 |
| - for _, opt := range opts { |
85 |
| - opt.Apply(&options) |
86 |
| - } |
87 |
| - |
88 |
| - var executor func(*T, error) (*T, error) |
89 |
| - |
90 |
| - retryCount := 0 |
91 |
| - |
92 |
| - executor = func(previousResponse *T, previousError error) (*T, error) { |
93 |
| - response, responseErr := execute(previousResponse, previousError) |
94 |
| - |
95 |
| - retryCount++ |
96 |
| - |
97 |
| - if options.Aggregator != nil { |
98 |
| - options.Aggregator(response, responseErr) |
99 |
| - } |
100 |
| - |
101 |
| - if validate(response, responseErr) { |
102 |
| - return response, responseErr |
103 |
| - } |
104 |
| - |
105 |
| - if retryCount >= options.MaxRetries { |
106 |
| - return nil, errs.NewWaitError(fmt.Sprintf("The maximum number of retries exceeded. (%d/%d)", retryCount, options.MaxRetries)) |
107 |
| - } |
108 |
| - |
109 |
| - if options.IterableError != nil && options.IterableError.Validate(response, responseErr) { |
110 |
| - if options.IterableError.Message != nil { |
111 |
| - return nil, errs.NewWaitError(options.IterableError.Message(response, responseErr)) |
112 |
| - } |
113 |
| - |
114 |
| - return nil, errs.NewWaitError("an error occurred") |
115 |
| - } |
116 |
| - |
117 |
| - time.Sleep(options.Timeout(retryCount)) |
118 |
| - |
119 |
| - return executor(response, responseErr) |
120 |
| - } |
121 |
| - |
122 |
| - return executor(nil, nil) |
123 |
| -} |
124 |
| - |
125 | 69 | // QueryParameterToString convert any query parameters to string.
|
126 | 70 | func QueryParameterToString(obj any) string {
|
127 | 71 | return strings.ReplaceAll(url.QueryEscape(ParameterToString(obj)), "+", "%20")
|
|
0 commit comments