callAPI calls an API endpoint, checks the response, and extracts the resource name. Handles permission errors, HTTP errors, and JSON parsing in one place.
(ctx context.Context, path, operation, permission string, body map[string]any)
| 540 | // callAPI calls an API endpoint, checks the response, and extracts the resource name. |
| 541 | // Handles permission errors, HTTP errors, and JSON parsing in one place. |
| 542 | func (s *Server) callAPI(ctx context.Context, path, operation, permission string, body map[string]any) (string, error) { |
| 543 | resp, err := s.apiRequest(ctx, path, body) |
| 544 | if err != nil { |
| 545 | return "", errors.Wrapf(err, "%s request failed", operation) |
| 546 | } |
| 547 | if err := checkAPIResponse(resp, operation, permission); err != nil { |
| 548 | return "", err |
| 549 | } |
| 550 | var result struct { |
| 551 | Name string `json:"name"` |
| 552 | } |
| 553 | if err := json.Unmarshal(resp.Body, &result); err != nil { |
| 554 | return "", errors.Wrapf(err, "failed to parse %s response", operation) |
| 555 | } |
| 556 | return result.Name, nil |
| 557 | } |
| 558 | |
| 559 | // checkAPIResponse checks an API response for permission or general errors. |
| 560 | // Returns nil if the response is OK, or a structured error otherwise. |
no test coverage detected