MCPcopy Create free account
hub / github.com/bytebase/bytebase / doGraphRequest

Method doGraphRequest

backend/plugin/webhook/teams/app.go:367–426  ·  view source on GitHub ↗
(ctx context.Context, method, apiURL string, data []byte)

Source from the content-addressed store, hash-verified

365}
366
367func (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

Callers 4

getUserIDByEmailMethod · 0.95
getTeamsAppIDMethod · 0.95
getChatIDForUserMethod · 0.95

Calls 5

refreshGraphTokenMethod · 0.95
DoMethod · 0.80
ErrorfMethod · 0.80
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected