(ctx context.Context, method, apiURL string, data []byte)
| 365 | } |
| 366 | |
| 367 | func (p *provider) doGraphRequest(ctx context.Context, method, apiURL string, data []byte) ([]byte, error) { |
| 368 | const maxRetries = 3 |
| 369 | |
| 370 | for i := 0; i < maxRetries; i++ { |
| 371 | if ctx.Err() != nil { |
| 372 | return nil, ctx.Err() |
| 373 | } |
| 374 | |
| 375 | b, retry, err := func() ([]byte, bool, error) { |
| 376 | var body io.Reader |
| 377 | if data != nil { |
| 378 | body = bytes.NewReader(data) |
| 379 | } |
| 380 | |
| 381 | req, err := http.NewRequestWithContext(ctx, method, apiURL, body) |
| 382 | if err != nil { |
| 383 | return nil, false, errors.Wrapf(err, "failed to construct request") |
| 384 | } |
| 385 | |
| 386 | req.Header.Set("Authorization", "Bearer "+p.graphToken) |
| 387 | if data != nil { |
| 388 | req.Header.Set("Content-Type", "application/json") |
| 389 | } |
| 390 | |
| 391 | resp, err := p.c.Do(req) |
| 392 | if err != nil { |
| 393 | return nil, false, errors.Wrapf(err, "request failed") |
| 394 | } |
| 395 | defer resp.Body.Close() |
| 396 | |
| 397 | respBody, err := io.ReadAll(resp.Body) |
| 398 | if err != nil { |
| 399 | return nil, false, errors.Wrapf(err, "failed to read response") |
| 400 | } |
| 401 | |
| 402 | if resp.StatusCode == http.StatusUnauthorized { |
| 403 | if err := p.refreshGraphToken(ctx); err != nil { |
| 404 | return nil, false, errors.Wrapf(err, "failed to refresh token") |
| 405 | } |
| 406 | return nil, true, nil |
| 407 | } |
| 408 | |
| 409 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 410 | return nil, false, errors.Errorf("request failed with status %d: %s", resp.StatusCode, string(respBody)) |
| 411 | } |
| 412 | |
| 413 | return respBody, false, nil |
| 414 | }() |
| 415 | |
| 416 | if err != nil { |
| 417 | return nil, err |
| 418 | } |
| 419 | if retry { |
| 420 | continue |
| 421 | } |
| 422 | return b, nil |
| 423 | } |
| 424 |
no test coverage detected