MCPcopy Create free account
hub / github.com/GetStream/stream-go2 / request

Method request

client.go:482–533  ·  view source on GitHub ↗
(ctx context.Context, method string, endpoint endpoint, data any, authFn authFunc)

Source from the content-addressed store, hash-verified

480}
481
482func (c *Client) request(ctx context.Context, method string, endpoint endpoint, data any, authFn authFunc) ([]byte, error) {
483 var reader io.Reader
484 if data != nil {
485 payload, err := json.Marshal(data)
486 if err != nil {
487 return nil, fmt.Errorf("cannot marshal request: %w", err)
488 }
489 reader = bytes.NewReader(payload)
490 }
491
492 req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), reader)
493 if err != nil {
494 return nil, fmt.Errorf("cannot create request: %w", err)
495 }
496 c.setBaseHeaders(req)
497
498 if authFn != nil {
499 if err := authFn(req); err != nil {
500 return nil, err
501 }
502 }
503
504 resp, err := c.requester.Do(req)
505 if err != nil {
506 return nil, fmt.Errorf("cannot perform request: %w", err)
507 }
508 defer resp.Body.Close()
509
510 rate := NewRate(resp.Header)
511
512 if resp.StatusCode/100 != 2 {
513 return nil, c.makeStreamError(resp.StatusCode, rate, resp.Body)
514 }
515
516 body, err := io.ReadAll(resp.Body)
517 if err != nil {
518 return nil, fmt.Errorf("cannot read response: %w", err)
519 }
520
521 out := map[string]any{}
522 if err := json.Unmarshal(body, &out); err != nil {
523 return nil, fmt.Errorf("cannot read response: %w", err)
524 }
525
526 out["ratelimit"] = rate
527
528 if body, err = json.Marshal(out); err != nil {
529 return nil, fmt.Errorf("cannot read response: %w", err)
530 }
531
532 return body, nil
533}
534
535func (c *Client) addActivity(ctx context.Context, feed Feed, activity Activity) (*AddActivityResponse, error) {
536 endpoint := c.makeEndpoint("feed/%s/%s/", feed.Slug(), feed.UserID())

Callers 5

Test_requestErrorsFunction · 0.95
getMethod · 0.95
postMethod · 0.95
putMethod · 0.95
deleteMethod · 0.95

Calls 5

setBaseHeadersMethod · 0.95
makeStreamErrorMethod · 0.95
NewRateFunction · 0.85
DoMethod · 0.65
StringMethod · 0.45

Tested by 1

Test_requestErrorsFunction · 0.76