@@ -4,21 +4,21 @@ package {{packageName}}
4
4
{ {#operations} }
5
5
import (
6
6
"bytes"
7
- _context "context"
8
- _ioutil "io/ioutil "
9
- _nethttp "net/http"
10
- _neturl "net/url"
7
+ "context"
8
+ "io"
9
+ "net/http"
10
+ "net/url"
11
11
"time"
12
12
{ {#imports} } "{ {import} }"
13
13
{ {/imports} }
14
14
15
- internalutils "{ {gitHost} }/{ {gitUserId} }/{ {gitRepoId} }/internal/utils"
16
- telemetry "{ {gitHost} }/{ {gitUserId} }/{ {gitRepoId} }/telemetry"
15
+ "{ {gitHost} }/{ {gitUserId} }/{ {gitRepoId} }/internal/utils/retryutils "
16
+ "{ {gitHost} }/{ {gitUserId} }/{ {gitRepoId} }/telemetry"
17
17
)
18
18
19
19
// Linger please
20
20
var (
21
- _ _context .Context
21
+ _ context .Context
22
22
)
23
23
{ {#generateInterfaces} }
24
24
@@ -30,17 +30,17 @@ type {{classname}} interface {
30
30
{ {#notes} }
31
31
* { {{unescapedNotes} }}
32
32
{ {/notes} }
33
- * @param ctx _context .Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{ {#pathParams} }
33
+ * @param ctx context .Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{ {#pathParams} }
34
34
* @param { {paramName} }{ {#description} } { {{.} }}{ {/description} }{ {/pathParams} }
35
35
* @return { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request
36
36
*/
37
- { {{nickname} }}(ctx _context .Context{ {#pathParams} }, { {paramName} } { {{dataType} }}{ {/pathParams} }) { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request
37
+ { {{nickname} }}(ctx context .Context{ {#pathParams} }, { {paramName} } { {{dataType} }}{ {/pathParams} }) { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request
38
38
39
39
/*
40
40
* { {nickname} }Execute executes the request{ {#returnType} }
41
41
* @return { {{.} }}{ {/returnType} }
42
42
*/
43
- { {nickname} }Execute(r { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request) ({ {#returnType} }{ {{.} }}, { {/returnType} }*_nethttp .Response, error)
43
+ { {nickname} }Execute(r { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request) ({ {#returnType} }{ {{.} }}, { {/returnType} }*http .Response, error)
44
44
{ {/operation} }
45
45
}
46
46
{ {/generateInterfaces} }
@@ -51,7 +51,7 @@ type {{classname}}Service service
51
51
{ {#operation} }
52
52
53
53
type { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request struct {
54
- ctx _context .Context{{#generateInterfaces} }
54
+ ctx context .Context{{#generateInterfaces} }
55
55
ApiService { {classname} }
56
56
{ {/generateInterfaces} }{ {^generateInterfaces} }
57
57
ApiService *{ {classname} }Service
@@ -66,7 +66,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Reques
66
66
return r
67
67
}{ {/isPathParam} }{ {/allParams} }
68
68
69
- func (r { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request) Execute() ({ {#returnType} }{ {{.} }}, { {/returnType} }*_nethttp .Response, error) {
69
+ func (r { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request) Execute() ({ {#returnType} }{ {{.} }}, { {/returnType} }*http .Response, error) {
70
70
return r.ApiService.{{nickname} }Execute(r)
71
71
}
72
72
@@ -75,11 +75,11 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Reques
75
75
{ {#notes} }
76
76
* { {{unescapedNotes} }}
77
77
{ {/notes} }
78
- * @param ctx _context .Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{ {#pathParams} }
78
+ * @param ctx context .Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{ {#pathParams} }
79
79
* @param { {paramName} }{ {#description} } { {{.} }}{ {/description} }{ {/pathParams} }
80
80
* @return { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request
81
81
*/
82
- func (a *{ {{classname} }}Service) { {{nickname} }}(ctx _context .Context{ {#pathParams} }, { {paramName} } { {{dataType} }}{ {/pathParams} }) { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request {
82
+ func (a *{ {{classname} }}Service) { {{nickname} }}(ctx context .Context{ {#pathParams} }, { {paramName} } { {{dataType} }}{ {/pathParams} }) { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request {
83
83
return {{#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request{
84
84
ApiService: a,
85
85
ctx: ctx,{{#pathParams} }
@@ -91,22 +91,13 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#pathParam
91
91
* Execute executes the request{ {#returnType} }
92
92
* @return { {{.} }}{ {/returnType} }
93
93
*/
94
- func (a *{ {{classname} }}Service) { {nickname} }Execute(r { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request) ({ {#returnType} }{ {{.} }}, { {/returnType} }*_nethttp.Response, error) {
95
- var maxRetry int
96
- var minWaitInMs int
97
- var requestStarted time.Time = time.Now()
98
-
99
- if (a.RetryParams != nil) {
100
- maxRetry = a.RetryParams.MinWaitInMs
101
- minWaitInMs = a.RetryParams.MinWaitInMs
102
- } else {
103
- maxRetry = 0
104
- minWaitInMs = 0
105
- }
94
+ func (a *{ {{classname} }}Service) { {nickname} }Execute(r { {#structPrefix} }{ {&classname} }{ {/structPrefix} }Api{ {operationId} }Request) ({ {#returnType} }{ {{.} }}, { {/returnType} }*http.Response, error) {
95
+ var requestStarted = time.Now()
106
96
107
- for i := 0; i < maxRetry+1; i++ {
97
+ retryParams := a.client.cfg.RetryParams
98
+ for i := 0; i < retryParams.MaxRetry+1; i++ {
108
99
var (
109
- localVarHTTPMethod = _nethttp .Method{{httpMethod} }
100
+ localVarHTTPMethod = http .Method{{httpMethod} }
110
101
localVarPostBody interface{ }
111
102
{ {#returnType} }
112
103
localVarReturnValue { {{.} }}
@@ -118,10 +109,10 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
118
109
return {{#returnType} }localVarReturnValue, { {/returnType} }nil, reportError("{ {paramName} } is required and must be specified")
119
110
}
120
111
121
- localVarPath = strings.Replace(localVarPath, "{ " +" {{baseName} }"+"}", _neturl .PathEscape(parameterToString(r.{ {paramName} }, "{ {#collectionFormat} }{ {collectionFormat} }{ {/collectionFormat} }")), -1){ {/pathParams} }
112
+ localVarPath = strings.Replace(localVarPath, "{ " +" {{baseName} }"+"}", url .PathEscape(parameterToString(r.{ {paramName} }, "{ {#collectionFormat} }{ {collectionFormat} }{ {/collectionFormat} }")), -1){ {/pathParams} }
122
113
123
114
localVarHeaderParams := make(map[string]string)
124
- localVarQueryParams := _neturl .Values{ }
115
+ localVarQueryParams := url .Values{ }
125
116
{ {#allParams} }
126
117
{ {#required} }
127
118
{ {^isPathParam} }
@@ -256,7 +247,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
256
247
}
257
248
{ {/required} }
258
249
if localVarFile != nil {
259
- fbs, _ := _ioutil .ReadAll(localVarFile)
250
+ fbs, _ := io .ReadAll(localVarFile)
260
251
localVarFileBytes = fbs
261
252
localVarFileName = localVarFile.Name()
262
253
localVarFile.Close()
@@ -331,19 +322,33 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
331
322
332
323
localVarHTTPResponse, err := a.client.callAPI(req)
333
324
if err != nil || localVarHTTPResponse == nil {
325
+ if i < retryParams.MaxRetry {
326
+ timeToWait := retryutils.GetTimeToWait(i, retryParams.MaxRetry, retryParams.MinWaitInMs, http.Header{} , "{ {nickname} }")
327
+ if timeToWait > 0 {
328
+ time.Sleep(timeToWait)
329
+ continue
330
+ }
331
+ }
334
332
return { {#returnType} }localVarReturnValue, { {/returnType} }localVarHTTPResponse, err
335
333
}
336
334
337
- localVarBody, err := _ioutil .ReadAll(localVarHTTPResponse.Body)
335
+ localVarBody, err := io .ReadAll(localVarHTTPResponse.Body)
338
336
localVarHTTPResponse.Body.Close()
339
- localVarHTTPResponse.Body = _ioutil .NopCloser(bytes.NewBuffer(localVarBody))
337
+ localVarHTTPResponse.Body = io .NopCloser(bytes.NewBuffer(localVarBody))
340
338
if err != nil {
339
+ if i < retryParams.MaxRetry {
340
+ timeToWait := retryutils.GetTimeToWait(i, retryParams.MaxRetry, retryParams.MinWaitInMs, localVarHTTPResponse.Header, " {{nickname}}" )
341
+ if timeToWait > 0 {
342
+ time.Sleep(timeToWait)
343
+ continue
344
+ }
345
+ }
341
346
return { {#returnType} }localVarReturnValue, { {/returnType} }localVarHTTPResponse, err
342
347
}
343
348
344
- if localVarHTTPResponse.StatusCode >= _nethttp .StatusMultipleChoices {
349
+ if localVarHTTPResponse.StatusCode >= http .StatusMultipleChoices {
345
350
346
- if localVarHTTPResponse.StatusCode == _nethttp .StatusBadRequest || localVarHTTPResponse.StatusCode == _nethttp .StatusUnprocessableEntity {
351
+ if localVarHTTPResponse.StatusCode == http .StatusBadRequest || localVarHTTPResponse.StatusCode == http .StatusUnprocessableEntity {
347
352
newErr := FgaApiValidationError{
348
353
body: localVarBody,{{#pathParams.0} }
349
354
storeId: r.storeId,{ {/pathParams.0} }
@@ -369,7 +374,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
369
374
return { {#returnType} }localVarReturnValue, { {/returnType} }localVarHTTPResponse, newErr
370
375
}
371
376
372
- if localVarHTTPResponse.StatusCode == _nethttp .StatusUnauthorized || localVarHTTPResponse.StatusCode == _nethttp .StatusForbidden {
377
+ if localVarHTTPResponse.StatusCode == http .StatusUnauthorized || localVarHTTPResponse.StatusCode == http .StatusForbidden {
373
378
newErr := FgaApiAuthenticationError{
374
379
body: localVarBody,{{#pathParams.0} }
375
380
storeId: r.storeId,{ {/pathParams.0} }
@@ -384,7 +389,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
384
389
return { {#returnType} }localVarReturnValue, { {/returnType} }localVarHTTPResponse, newErr
385
390
}
386
391
387
- if localVarHTTPResponse.StatusCode == _nethttp .StatusNotFound {
392
+ if localVarHTTPResponse.StatusCode == http .StatusNotFound {
388
393
newErr := FgaApiNotFoundError{
389
394
body: localVarBody,{{#pathParams.0} }
390
395
storeId: r.storeId,{ {/pathParams.0} }
@@ -410,10 +415,13 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
410
415
return { {#returnType} }localVarReturnValue, { {/returnType} }localVarHTTPResponse, newErr
411
416
}
412
417
413
- if localVarHTTPResponse.StatusCode == _nethttp.StatusTooManyRequests {
414
- if i < maxRetry {
415
- time.Sleep(time.Duration(internalutils.RandomTime(i, minWaitInMs)) * time.Millisecond)
416
- continue
418
+ if localVarHTTPResponse.StatusCode == http.StatusTooManyRequests {
419
+ if i < retryParams.MaxRetry {
420
+ timeToWait := retryutils.GetTimeToWait(i, retryParams.MaxRetry, retryParams.MinWaitInMs, localVarHTTPResponse.Header, " {{nickname}}" )
421
+ if timeToWait > 0 {
422
+ time.Sleep(timeToWait)
423
+ continue
424
+ }
417
425
}
418
426
// maximum number of retry reached
419
427
newErr := FgaApiRateLimitExceededError{
@@ -433,10 +441,13 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
433
441
return { {#returnType} }localVarReturnValue, { {/returnType} }localVarHTTPResponse, newErr
434
442
}
435
443
436
- if localVarHTTPResponse.StatusCode >= _nethttp.StatusInternalServerError {
437
- if localVarHTTPResponse.StatusCode != _nethttp.StatusNotImplemented && i < maxRetry {
438
- time.Sleep(time.Duration(internalutils.RandomTime(i, minWaitInMs)) * time.Millisecond)
439
- continue
444
+ if localVarHTTPResponse.StatusCode >= http.StatusInternalServerError {
445
+ if localVarHTTPResponse.StatusCode != http.StatusNotImplemented && i < retryParams.MaxRetry {
446
+ timeToWait := retryutils.GetTimeToWait(i, retryParams.MaxRetry, retryParams.MinWaitInMs, localVarHTTPResponse.Header, " {{nickname}}" )
447
+ if timeToWait > 0 {
448
+ time.Sleep(timeToWait)
449
+ continue
450
+ }
440
451
}
441
452
newErr := FgaApiInternalError{
442
453
body: localVarBody,{{#pathParams.0} }
0 commit comments