fix: P0 — add User-Agent + retry to bash urllib, retry to service_spec_gen, empty-list guard
This commit is contained in:
@@ -388,30 +388,59 @@ func (c *apiClient) getServiceOperation(svcOperationId int) (serviceOperationInf
|
||||
}
|
||||
|
||||
func (c *apiClient) getViaProxy(endpoint string, out interface{}) error {
|
||||
// The API uses a proxy query parameter called "endpoint".
|
||||
req, err := http.NewRequest("GET", c.endpoint, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
const maxRetries = 3
|
||||
baseDelay := 2 * time.Second
|
||||
|
||||
var lastErr error
|
||||
for attempt := 0; attempt <= maxRetries; attempt++ {
|
||||
if attempt > 0 {
|
||||
time.Sleep(baseDelay * time.Duration(1<<(attempt-1)))
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", c.endpoint, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
q := req.URL.Query()
|
||||
q.Set("endpoint", endpoint)
|
||||
req.URL.RawQuery = q.Encode()
|
||||
if c.token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; Terraform-Provider-Nubes/Generator)")
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
if attempt < maxRetries {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
body, readErr := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if readErr != nil {
|
||||
lastErr = readErr
|
||||
if attempt < maxRetries {
|
||||
continue
|
||||
}
|
||||
return readErr
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
err := fmt.Errorf("status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
|
||||
// Retry on transient errors
|
||||
if attempt < maxRetries && (resp.StatusCode == 429 || resp.StatusCode >= 500) {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(body, out)
|
||||
}
|
||||
q := req.URL.Query()
|
||||
q.Set("endpoint", endpoint)
|
||||
req.URL.RawQuery = q.Encode()
|
||||
if c.token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
// User-Agent: браузерный, чтобы пройти DDoS-Guard (см. docs/ops/API_TOKENS.md).
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; Terraform-Provider-Nubes/Generator)")
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
|
||||
}
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
return dec.Decode(out)
|
||||
|
||||
return fmt.Errorf("getViaProxy failed after %d retries: %w", maxRetries, lastErr)
|
||||
}
|
||||
|
||||
func (c *apiClient) collectOperations(ops []operationInfo) ([]OperationSpec, bool, bool, bool, error) {
|
||||
|
||||
Reference in New Issue
Block a user