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

Method do

backend/plugin/webhook/feishu/app.go:274–332  ·  view source on GitHub ↗
(ctx context.Context, method, url string, data []byte)

Source from the content-addressed store, hash-verified

272const maxRetries = 3
273
274func (p *provider) do(ctx context.Context, method, url string, data []byte) ([]byte, error) {
275 if p.token == "" {
276 if err := p.refreshToken(ctx); err != nil {
277 return nil, errors.Wrapf(err, "failed to refresh token")
278 }
279 }
280 for i := 0; i < maxRetries; i++ {
281 if ctx.Err() != nil {
282 return nil, ctx.Err()
283 }
284
285 b, cont, err := func() ([]byte, bool, error) {
286 req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(data))
287 if err != nil {
288 return nil, false, errors.Wrapf(err, "failed to construct %s %s", method, url)
289 }
290
291 req.Header.Set("Content-Type", "application/json; charset=utf-8")
292 req.Header.Add("Authorization", "Bearer "+p.token)
293
294 resp, err := p.c.Do(req)
295 if err != nil {
296 return nil, false, errors.Wrapf(err, "%s %s", method, url)
297 }
298 defer resp.Body.Close()
299
300 if resp.StatusCode != http.StatusOK {
301 return nil, false, errors.Errorf("received non-200 HTTP code %d for %s %s", resp.StatusCode, method, url)
302 }
303
304 b, err := io.ReadAll(resp.Body)
305 if err != nil {
306 return nil, false, errors.Wrapf(err, "failed to read body of %s %s", method, url)
307 }
308
309 var response struct {
310 Code int `json:"code"`
311 }
312 if err := json.Unmarshal(b, &response); err != nil {
313 return nil, false, errors.Errorf("failed to unmarshal response")
314 }
315 if response.Code == emptyTokenRespCode || response.Code == invalidTokenRespCode {
316 if err := p.refreshToken(ctx); err != nil {
317 return nil, false, errors.Wrapf(err, "failed to refresh token")
318 }
319 return nil, true, nil
320 }
321 return b, false, nil
322 }()
323 if err != nil {
324 return nil, err
325 }
326 if cont {
327 continue
328 }
329 return b, nil
330 }
331 return nil, errors.Errorf("exceeds max retries for %s %s", method, url)

Callers 2

getIDByEmailMethod · 0.95
sendMessageMethod · 0.95

Calls 6

refreshTokenMethod · 0.95
DoMethod · 0.80
ErrorfMethod · 0.80
UnmarshalMethod · 0.80
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected