MCPcopy Create free account
hub / github.com/cli/cli / OpenStream

Method OpenStream

pkg/cmd/event/client/client.go:214–242  ·  view source on GitHub ↗
(ctx context.Context, id string)

Source from the content-addressed store, hash-verified

212}
213
214func (c *eventClient) OpenStream(ctx context.Context, id string) (io.ReadCloser, error) {
215 if !SupportedHost(c.host) {
216 return nil, ErrUnsupportedHost
217 }
218
219 path, err := safeurl.JoinPath("event-subscriptions", id, "events")
220 if err != nil {
221 return nil, err
222 }
223
224 resp, err := c.api.RequestWithContext(ctx, c.host, http.MethodGet, path.String(), nil, api.WithHeader("Accept", "text/event-stream"))
225 if err != nil {
226 if httpErr, ok := errors.AsType[api.HTTPError](err); ok {
227 switch {
228 case httpErr.StatusCode == http.StatusNotFound:
229 return nil, fmt.Errorf("%w: %w", ErrSubscriptionNotFound, err)
230 case httpErr.StatusCode == http.StatusForbidden:
231 return nil, forbiddenError(httpErr, err)
232 }
233 }
234 return nil, err
235 }
236 mediaType, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type"))
237 if mediaType != "text/event-stream" {
238 resp.Body.Close()
239 return nil, fmt.Errorf("unexpected event stream content type %q", resp.Header.Get("Content-Type"))
240 }
241 return resp.Body, nil
242}
243
244// forbiddenError tells a 403 response for an account without preview access apart from other denials. It keeps the
245// HTTP error wrapped, so callers treat it as final rather than retrying it.

Callers

nothing calls this directly

Calls 9

JoinPathFunction · 0.92
WithHeaderFunction · 0.92
SupportedHostFunction · 0.85
forbiddenErrorFunction · 0.85
RequestWithContextMethod · 0.80
StringMethod · 0.65
ErrorfMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected